Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ from datetime import datetime
|
|
10 |
from typing import List, Dict, Optional, Tuple
|
11 |
from enum import Enum
|
12 |
|
13 |
-
from fastapi import FastAPI, HTTPException, UploadFile, File, Query
|
14 |
from fastapi.responses import StreamingResponse, JSONResponse
|
15 |
from fastapi.middleware.cors import CORSMiddleware
|
16 |
from pydantic import BaseModel
|
@@ -440,6 +440,45 @@ async def synthesize_voice(request: VoiceOutputRequest):
|
|
440 |
logger.error(f"Error in voice synthesis: {e}")
|
441 |
raise HTTPException(status_code=500, detail="Error generating voice output")
|
442 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
443 |
@app.post("/voice/chat")
|
444 |
async def voice_chat_endpoint(
|
445 |
audio: UploadFile = File(...),
|
|
|
10 |
from typing import List, Dict, Optional, Tuple
|
11 |
from enum import Enum
|
12 |
|
13 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File, Query, Form
|
14 |
from fastapi.responses import StreamingResponse, JSONResponse
|
15 |
from fastapi.middleware.cors import CORSMiddleware
|
16 |
from pydantic import BaseModel
|
|
|
440 |
logger.error(f"Error in voice synthesis: {e}")
|
441 |
raise HTTPException(status_code=500, detail="Error generating voice output")
|
442 |
|
443 |
+
@app.post("/upload")
|
444 |
+
async def upload_and_analyze_file(file: UploadFile = File(...), format: str = Form("clean")):
|
445 |
+
try:
|
446 |
+
# 1. Read file content
|
447 |
+
contents = await file.read()
|
448 |
+
filename = file.filename.lower()
|
449 |
+
|
450 |
+
# 2. Extract text depending on file type
|
451 |
+
if filename.endswith(".pdf"):
|
452 |
+
import fitz # PyMuPDF
|
453 |
+
doc = fitz.open(stream=contents, filetype="pdf")
|
454 |
+
text = "\n".join([page.get_text() for page in doc])
|
455 |
+
elif filename.endswith(".txt"):
|
456 |
+
text = contents.decode("utf-8", errors="ignore")
|
457 |
+
elif filename.endswith(".csv"):
|
458 |
+
import pandas as pd
|
459 |
+
df = pd.read_csv(io.BytesIO(contents))
|
460 |
+
text = df.to_string()
|
461 |
+
elif filename.endswith(".xlsx"):
|
462 |
+
import pandas as pd
|
463 |
+
df = pd.read_excel(io.BytesIO(contents))
|
464 |
+
text = df.to_string()
|
465 |
+
else:
|
466 |
+
raise HTTPException(status_code=400, detail="Unsupported file type")
|
467 |
+
|
468 |
+
# 3. Send to TxAgent
|
469 |
+
message = (
|
470 |
+
f"You are a clinical assistant AI.\n\n"
|
471 |
+
f"Analyze the following patient document:\n\n{text[:10000]}"
|
472 |
+
)
|
473 |
+
response = agent.chat(message=message, history=[], temperature=0.7, max_new_tokens=1024)
|
474 |
+
|
475 |
+
return {"status": "success", "filename": file.filename, "response": response}
|
476 |
+
|
477 |
+
except Exception as e:
|
478 |
+
logger.error(f"File upload + analysis failed: {e}")
|
479 |
+
raise HTTPException(status_code=500, detail="Failed to process and analyze the file")
|
480 |
+
|
481 |
+
|
482 |
@app.post("/voice/chat")
|
483 |
async def voice_chat_endpoint(
|
484 |
audio: UploadFile = File(...),
|