Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,119 +1,163 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
"
|
| 73 |
-
"
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import FileResponse, StreamingResponse
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
import os
|
| 6 |
+
from one_shot_bot import generate_advice
|
| 7 |
+
from prediction_helper import predict
|
| 8 |
+
from chatbot_advisor import ask_chatbot
|
| 9 |
+
from utility import STT, TTS
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# ---------------- CORS ---------------- #
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"],
|
| 17 |
+
allow_credentials=True,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# ---------------- MODELS ---------------- #
|
| 23 |
+
|
| 24 |
+
class HealthInput(BaseModel):
|
| 25 |
+
gender: str
|
| 26 |
+
marital_status: str
|
| 27 |
+
age: int
|
| 28 |
+
number_of_dependants: int
|
| 29 |
+
income_lakhs: float
|
| 30 |
+
genetical_risk: int
|
| 31 |
+
insurance_plan: str
|
| 32 |
+
employment_status: str
|
| 33 |
+
bmi_category: str
|
| 34 |
+
smoking_status: str
|
| 35 |
+
region: str
|
| 36 |
+
medical_history: str
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class HealthOutput(BaseModel):
|
| 40 |
+
yearly: float
|
| 41 |
+
monthly: float
|
| 42 |
+
advice: str
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class ChatMessage(BaseModel):
|
| 46 |
+
thread_id: str
|
| 47 |
+
message: str
|
| 48 |
+
yearly_cost: float
|
| 49 |
+
monthly_cost: float
|
| 50 |
+
ai_summary: str
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class TTSRequest(BaseModel):
|
| 54 |
+
text: str
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# ---------------- BASIC ROUTE ---------------- #
|
| 58 |
+
|
| 59 |
+
@app.get("/")
|
| 60 |
+
def home():
|
| 61 |
+
return {"message": "Healthcare AI API is running."}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# ---------------- PREDICTION ---------------- #
|
| 65 |
+
|
| 66 |
+
@app.post("/predict", response_model=HealthOutput)
|
| 67 |
+
def predict_output(input_data: HealthInput):
|
| 68 |
+
try:
|
| 69 |
+
data = input_data.model_dump()
|
| 70 |
+
|
| 71 |
+
converted_data = {
|
| 72 |
+
"Gender": data["gender"].title(),
|
| 73 |
+
"Marital Status": data["marital_status"].title(),
|
| 74 |
+
"Age": data["age"],
|
| 75 |
+
"Number of Dependants": data["number_of_dependants"],
|
| 76 |
+
"Income in Lakhs": data["income_lakhs"],
|
| 77 |
+
"Genetical Risk": data["genetical_risk"],
|
| 78 |
+
"Insurance Plan": data["insurance_plan"].title(),
|
| 79 |
+
"Employment Status": data["employment_status"],
|
| 80 |
+
"BMI Category": data["bmi_category"],
|
| 81 |
+
"Smoking Status": data["smoking_status"],
|
| 82 |
+
"Region": data["region"],
|
| 83 |
+
"Medical History": data["medical_history"]
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
yearly_prediction = float(predict(converted_data))
|
| 87 |
+
monthly = round(yearly_prediction / 12, 2)
|
| 88 |
+
|
| 89 |
+
advice = generate_advice(
|
| 90 |
+
yearly_premium=yearly_prediction,
|
| 91 |
+
monthly_premium=monthly,
|
| 92 |
+
age=input_data.age,
|
| 93 |
+
gender=input_data.gender,
|
| 94 |
+
marital_status=input_data.marital_status,
|
| 95 |
+
dependents=input_data.number_of_dependants,
|
| 96 |
+
bmi_category=input_data.bmi_category,
|
| 97 |
+
smoking_status=input_data.smoking_status,
|
| 98 |
+
medical_history=input_data.medical_history,
|
| 99 |
+
genetic_risk=input_data.genetical_risk,
|
| 100 |
+
region=input_data.region,
|
| 101 |
+
income_lakhs=input_data.income_lakhs,
|
| 102 |
+
employment_status=input_data.employment_status,
|
| 103 |
+
insurance_plan=input_data.insurance_plan
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
return HealthOutput(yearly=yearly_prediction, monthly=monthly, advice=advice)
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
raise HTTPException(status_code=500, detail=f"Prediction Failed: {str(e)}")
|
| 110 |
+
|
| 111 |
+
# ---------------- CHAT STREAM ---------------- #
|
| 112 |
+
|
| 113 |
+
@app.post('/chat')
|
| 114 |
+
def chat(input_data : ChatMessage):
|
| 115 |
+
try:
|
| 116 |
+
yearly_cost = input_data.yearly_cost
|
| 117 |
+
monthly_cost = input_data.monthly_cost
|
| 118 |
+
ai_summary = input_data.ai_summary
|
| 119 |
+
|
| 120 |
+
response = ask_chatbot(
|
| 121 |
+
yearly_cost=yearly_cost,
|
| 122 |
+
monthly_cost=monthly_cost,
|
| 123 |
+
ai_summary=ai_summary,
|
| 124 |
+
user_message=input_data.message,
|
| 125 |
+
thread_id=input_data.thread_id
|
| 126 |
+
)
|
| 127 |
+
return response
|
| 128 |
+
|
| 129 |
+
except Exception as e:
|
| 130 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
# ---------------- TTS ---------------- #
|
| 134 |
+
|
| 135 |
+
@app.post("/tts")
|
| 136 |
+
async def generate_tts(request: TTSRequest):
|
| 137 |
+
try:
|
| 138 |
+
if not request.text.strip():
|
| 139 |
+
raise HTTPException(status_code=400, detail="Text is empty")
|
| 140 |
+
|
| 141 |
+
audio_path = await TTS(text=request.text)
|
| 142 |
+
|
| 143 |
+
if not os.path.exists(audio_path):
|
| 144 |
+
raise HTTPException(status_code=500, detail="Audio file not created")
|
| 145 |
+
|
| 146 |
+
return FileResponse(
|
| 147 |
+
path=audio_path,
|
| 148 |
+
media_type="audio/mpeg",
|
| 149 |
+
filename="speech.mp3"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
except Exception as e:
|
| 153 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
# ---------------- STT ---------------- #
|
| 157 |
+
|
| 158 |
+
@app.post("/stt")
|
| 159 |
+
async def transcribe_audio(file: UploadFile = File(...)):
|
| 160 |
+
try:
|
| 161 |
+
return await STT(file)
|
| 162 |
+
except Exception as e:
|
| 163 |
+
raise HTTPException(status_code=500, detail=str(e))
|