Update app.py
Browse files
app.py
CHANGED
@@ -51,7 +51,19 @@ pdf_service = PDFService(model_service)
|
|
51 |
faq_service = FAQService(model_service)
|
52 |
chat_service = ChatService(model_service, data_service, pdf_service, faq_service)
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
@app.post("/api/chat")
|
56 |
async def chat_endpoint(
|
57 |
background_tasks: BackgroundTasks,
|
@@ -59,19 +71,31 @@ async def chat_endpoint(
|
|
59 |
api_key: str = Depends(get_api_key)
|
60 |
):
|
61 |
try:
|
|
|
62 |
response, updated_history, search_results = await chat_service.chat(
|
63 |
user_input.user_input,
|
64 |
user_input.chat_history
|
65 |
)
|
66 |
-
|
|
|
|
|
67 |
"status": "success",
|
68 |
"response": response,
|
69 |
"chat_history": updated_history,
|
70 |
"search_results": search_results
|
71 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
except Exception as e:
|
|
|
73 |
logger.error(f"Error in chat endpoint: {e}")
|
74 |
-
raise HTTPException(status_code=500, detail=
|
|
|
75 |
|
76 |
@app.post("/api/search")
|
77 |
async def search_endpoint(
|
@@ -80,7 +104,12 @@ async def search_endpoint(
|
|
80 |
):
|
81 |
try:
|
82 |
results = await data_service.search(query.query, query.top_k)
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
84 |
except Exception as e:
|
85 |
logger.error(f"Error in search endpoint: {e}")
|
86 |
raise HTTPException(status_code=500, detail=str(e))
|
@@ -153,7 +182,7 @@ if __name__ == "__main__":
|
|
153 |
|
154 |
# Create and launch Gradio interface
|
155 |
demo = create_gradio_interface()
|
156 |
-
demo.launch(server_name="0.0.0.0", server_port=
|
157 |
|
158 |
# Start FastAPI server
|
159 |
#uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
51 |
faq_service = FAQService(model_service)
|
52 |
chat_service = ChatService(model_service, data_service, pdf_service, faq_service)
|
53 |
|
54 |
+
import math
|
55 |
+
from fastapi.responses import JSONResponse
|
56 |
+
|
57 |
+
# Helper function to sanitize data
|
58 |
+
def sanitize_response(data):
|
59 |
+
if isinstance(data, dict):
|
60 |
+
return {k: sanitize_response(v) for k, v in data.items()}
|
61 |
+
elif isinstance(data, list):
|
62 |
+
return [sanitize_response(item) for item in data]
|
63 |
+
elif isinstance(data, float) and (math.isnan(data) or math.isinf(data)):
|
64 |
+
return None # Replace NaN/Infinity with None or another default value
|
65 |
+
return data
|
66 |
+
|
67 |
@app.post("/api/chat")
|
68 |
async def chat_endpoint(
|
69 |
background_tasks: BackgroundTasks,
|
|
|
71 |
api_key: str = Depends(get_api_key)
|
72 |
):
|
73 |
try:
|
74 |
+
# Call the chat service to get the data
|
75 |
response, updated_history, search_results = await chat_service.chat(
|
76 |
user_input.user_input,
|
77 |
user_input.chat_history
|
78 |
)
|
79 |
+
|
80 |
+
# Build the response dictionary
|
81 |
+
response_data = {
|
82 |
"status": "success",
|
83 |
"response": response,
|
84 |
"chat_history": updated_history,
|
85 |
"search_results": search_results
|
86 |
}
|
87 |
+
|
88 |
+
# Sanitize the response to ensure JSON compliance
|
89 |
+
sanitized_data = sanitize_response(response_data)
|
90 |
+
|
91 |
+
# Return the sanitized response
|
92 |
+
return JSONResponse(content=sanitized_data)
|
93 |
+
|
94 |
except Exception as e:
|
95 |
+
# Log and raise an error with details
|
96 |
logger.error(f"Error in chat endpoint: {e}")
|
97 |
+
raise HTTPException(status_code=500, detail="An internal server error occurred.")
|
98 |
+
|
99 |
|
100 |
@app.post("/api/search")
|
101 |
async def search_endpoint(
|
|
|
104 |
):
|
105 |
try:
|
106 |
results = await data_service.search(query.query, query.top_k)
|
107 |
+
# Sanitize the response to ensure JSON compliance
|
108 |
+
sanitized_data = sanitize_response(results)
|
109 |
+
|
110 |
+
# Return the sanitized response
|
111 |
+
return JSONResponse(content=sanitized_data)
|
112 |
+
|
113 |
except Exception as e:
|
114 |
logger.error(f"Error in search endpoint: {e}")
|
115 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
182 |
|
183 |
# Create and launch Gradio interface
|
184 |
demo = create_gradio_interface()
|
185 |
+
demo.launch(server_name="0.0.0.0", server_port=8080)
|
186 |
|
187 |
# Start FastAPI server
|
188 |
#uvicorn.run(app, host="0.0.0.0", port=8000)
|