donb-hf commited on
Commit
c29b3b9
·
1 Parent(s): 8c3a73e

add api changes

Browse files
Files changed (5) hide show
  1. .gitignore +2 -0
  2. api.py +39 -0
  3. app.py +17 -2
  4. requirements.txt +5 -1
  5. test_api.sh +11 -0
.gitignore CHANGED
@@ -1,2 +1,4 @@
1
  .venv/
2
  .env
 
 
 
1
  .venv/
2
  .env
3
+ __pycache__/
4
+
api.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # File: api.py
2
+ from fastapi import FastAPI, HTTPException
3
+ from pydantic import BaseModel
4
+ from data_ingestion import run_ingestion_pipeline
5
+ from retrieval import rag_query
6
+ import logging
7
+
8
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
9
+
10
+ app = FastAPI()
11
+
12
+ class IngestRequest(BaseModel):
13
+ query: str
14
+ max_results: int = 10
15
+
16
+ class QueryRequest(BaseModel):
17
+ query: str
18
+
19
+ @app.post("/ingest")
20
+ async def ingest(request: IngestRequest):
21
+ try:
22
+ result = run_ingestion_pipeline(request.query, request.max_results)
23
+ return {"message": result}
24
+ except Exception as e:
25
+ logging.error(f"Ingestion error: {str(e)}")
26
+ raise HTTPException(status_code=500, detail=str(e))
27
+
28
+ @app.post("/query")
29
+ async def query(request: QueryRequest):
30
+ try:
31
+ result = rag_query(request.query)
32
+ return {"result": result}
33
+ except Exception as e:
34
+ logging.error(f"Query error: {str(e)}")
35
+ raise HTTPException(status_code=500, detail=str(e))
36
+
37
+ if __name__ == "__main__":
38
+ import uvicorn
39
+ uvicorn.run(app, host="0.0.0.0", port=8000)
app.py CHANGED
@@ -1,9 +1,20 @@
1
  # File: app.py
2
  import gradio as gr
3
  from retrieval import rag_query
 
 
 
 
4
 
5
  def gradio_interface(query: str) -> str:
6
- return rag_query(query)
 
 
 
 
 
 
 
7
 
8
  iface = gr.Interface(
9
  fn=gradio_interface,
@@ -14,4 +25,8 @@ iface = gr.Interface(
14
  )
15
 
16
  if __name__ == "__main__":
17
- iface.launch()
 
 
 
 
 
1
  # File: app.py
2
  import gradio as gr
3
  from retrieval import rag_query
4
+ import logging
5
+
6
+ # Set up logging
7
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
8
 
9
  def gradio_interface(query: str) -> str:
10
+ try:
11
+ logging.info(f"Received query: {query}")
12
+ result = rag_query(query)
13
+ logging.info("Query processed successfully")
14
+ return result
15
+ except Exception as e:
16
+ logging.error(f"Error processing query: {str(e)}")
17
+ return f"An error occurred: {str(e)}"
18
 
19
  iface = gr.Interface(
20
  fn=gradio_interface,
 
25
  )
26
 
27
  if __name__ == "__main__":
28
+ try:
29
+ logging.info("Starting Gradio interface")
30
+ iface.launch()
31
+ except Exception as e:
32
+ logging.error(f"Failed to start Gradio interface: {str(e)}")
requirements.txt CHANGED
@@ -48,4 +48,8 @@ arxiv
48
  pymupdf
49
  datasets
50
  gradio
51
- langchain-community
 
 
 
 
 
48
  pymupdf
49
  datasets
50
  gradio
51
+ langchain-community
52
+
53
+ fastapi==0.95.0
54
+ uvicorn==0.21.1
55
+ pydantic==1.10.7
test_api.sh ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ echo "Testing ingestion endpoint..."
4
+ curl -X POST "http://localhost:8000/ingest" \
5
+ -H "Content-Type: application/json" \
6
+ -d '{"query": "quantum computing", "max_results": 5}'
7
+
8
+ echo -e "\n\nTesting query endpoint..."
9
+ curl -X POST "http://localhost:8000/query" \
10
+ -H "Content-Type: application/json" \
11
+ -d '{"query": "What are the recent advancements in quantum computing?"}'