Spaces:
Sleeping
Sleeping
Vineela Gampa
commited on
fixing chat
Browse files- backend.py +19 -7
backend.py
CHANGED
|
@@ -189,17 +189,27 @@ def get_past_reports_from_sqllite(user_id: str):
|
|
| 189 |
@app.post("/chat/", response_model=ChatResponse)
|
| 190 |
async def chat_endpoint(request: ChatRequest):
|
| 191 |
global result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
try:
|
| 193 |
-
document_text = json.dumps(
|
| 194 |
full_prompt = system_prompt_chat.format(
|
| 195 |
document_text=document_text,
|
| 196 |
user_question=request.question
|
| 197 |
)
|
| 198 |
-
|
| 199 |
-
question = data.get("question")
|
| 200 |
-
report_text = data.get("report_text")
|
| 201 |
-
user_id = data.get("user_id")
|
| 202 |
-
|
| 203 |
response = model.generate_content(full_prompt)
|
| 204 |
return ChatResponse(answer=response.text)
|
| 205 |
except Exception as e:
|
|
@@ -207,11 +217,13 @@ async def chat_endpoint(request: ChatRequest):
|
|
| 207 |
|
| 208 |
@app.post("/analyze")
|
| 209 |
async def analyze_endpoint(file: UploadFile = File(...), prompt: str = Form(None)):
|
| 210 |
-
|
| 211 |
"""
|
| 212 |
Upload an image file (field name `file`) and optional text `prompt`.
|
| 213 |
Returns parsed JSON (or raw model output if JSON couldn't be parsed).
|
| 214 |
"""
|
|
|
|
|
|
|
| 215 |
contents = await file.read() # <-- this gets the uploaded file bytes
|
| 216 |
mime = file.content_type or "image/png"
|
| 217 |
result = await analyze_image(contents, mime, prompt)
|
|
|
|
| 189 |
@app.post("/chat/", response_model=ChatResponse)
|
| 190 |
async def chat_endpoint(request: ChatRequest):
|
| 191 |
global result
|
| 192 |
+
print(f"Received chat request for user: {request.user_id}")
|
| 193 |
+
"""
|
| 194 |
+
Chatbot endpoint that answers questions based on the last analyzed document and user history.
|
| 195 |
+
"""
|
| 196 |
+
|
| 197 |
+
#history_text = get_past_reports_from_firestore(request.user_id)
|
| 198 |
+
full_document_text = get_past_reports_from_sqllite(request.user_id)
|
| 199 |
+
|
| 200 |
+
#full_document_text = +"\n\n" + "PAST REPORTS:\n" + history_text
|
| 201 |
+
|
| 202 |
+
if not full_document_text:
|
| 203 |
+
raise HTTPException(status_code=400, detail="No past reports or current data exists for this user")
|
| 204 |
+
|
| 205 |
+
|
| 206 |
try:
|
| 207 |
+
document_text = json.dumps(full_document_text)
|
| 208 |
full_prompt = system_prompt_chat.format(
|
| 209 |
document_text=document_text,
|
| 210 |
user_question=request.question
|
| 211 |
)
|
| 212 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
response = model.generate_content(full_prompt)
|
| 214 |
return ChatResponse(answer=response.text)
|
| 215 |
except Exception as e:
|
|
|
|
| 217 |
|
| 218 |
@app.post("/analyze")
|
| 219 |
async def analyze_endpoint(file: UploadFile = File(...), prompt: str = Form(None)):
|
| 220 |
+
|
| 221 |
"""
|
| 222 |
Upload an image file (field name `file`) and optional text `prompt`.
|
| 223 |
Returns parsed JSON (or raw model output if JSON couldn't be parsed).
|
| 224 |
"""
|
| 225 |
+
|
| 226 |
+
global result
|
| 227 |
contents = await file.read() # <-- this gets the uploaded file bytes
|
| 228 |
mime = file.content_type or "image/png"
|
| 229 |
result = await analyze_image(contents, mime, prompt)
|