Spaces:
Sleeping
Sleeping
Nidhirk2113 commited on
Commit ·
56b3719
1
Parent(s): 3c9676e
added app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,21 @@
|
|
| 1 |
-
# ================================================================
|
| 2 |
-
# 🩺 GDM Predictor Backend — FastAPI + Gemini Integration
|
| 3 |
-
# ================================================================
|
| 4 |
-
|
| 5 |
from fastapi import FastAPI
|
| 6 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from pydantic import BaseModel
|
| 8 |
import pandas as pd
|
| 9 |
import joblib
|
| 10 |
import google.generativeai as genai
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
GEMINI_API_KEY = "YOUR_GEMINI_API_KEY" # Replace with your key from makersuite.google.com
|
| 14 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 15 |
-
|
| 16 |
-
app = FastAPI(title="GDM Predictor Backend", version="1.0")
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
allow_origins=["*"],
|
| 22 |
-
allow_credentials=True,
|
| 23 |
-
allow_methods=["*"],
|
| 24 |
-
allow_headers=["*"],
|
| 25 |
-
)
|
| 26 |
|
| 27 |
-
# ====================== MODEL ======================
|
| 28 |
try:
|
| 29 |
model = joblib.load("model.pkl")
|
| 30 |
except Exception:
|
| 31 |
model = None
|
| 32 |
-
print("⚠️ Model file not found! Please upload 'model.pkl' in the backend folder.")
|
| 33 |
|
| 34 |
-
# ====================== SCHEMAS ======================
|
| 35 |
class InputData(BaseModel):
|
| 36 |
age: float
|
| 37 |
glucose: float
|
|
@@ -40,65 +24,31 @@ class InputData(BaseModel):
|
|
| 40 |
smoking: str
|
| 41 |
stress: int
|
| 42 |
|
| 43 |
-
|
| 44 |
-
# ====================== ROUTES ======================
|
| 45 |
-
|
| 46 |
@app.get("/")
|
| 47 |
-
def
|
| 48 |
return {"message": "Welcome to the GDM Predictor API", "status": "active"}
|
| 49 |
|
| 50 |
-
|
| 51 |
@app.post("/predict")
|
| 52 |
def predict(data: InputData):
|
| 53 |
if model is None:
|
| 54 |
return {"error": "Model not loaded"}
|
| 55 |
-
|
| 56 |
df = pd.DataFrame([data.dict()])
|
| 57 |
-
df["smoking"] = df["smoking"].apply(lambda x: 1 if x.lower()
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
label = "High Risk" if
|
| 61 |
-
|
| 62 |
-
return {"label": label, "probability": float(probability)}
|
| 63 |
-
|
| 64 |
|
| 65 |
@app.post("/generate-report")
|
| 66 |
def generate_report(data: InputData):
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
Age: {data.age}
|
| 73 |
-
Glucose Level: {data.glucose}
|
| 74 |
-
CDKAL1 SNP dosage: {data.cdkal1}
|
| 75 |
-
SOX17 SNP dosage: {data.sox17}
|
| 76 |
-
Smoking: {data.smoking}
|
| 77 |
-
Stress Level: {data.stress}
|
| 78 |
-
|
| 79 |
-
The AI model predicted the GDM risk category (High/Low) accordingly.
|
| 80 |
-
|
| 81 |
-
Provide a short report including:
|
| 82 |
-
- A friendly greeting and overview of what the result means.
|
| 83 |
-
- A short paragraph explaining contributing factors.
|
| 84 |
-
- General wellness and monitoring advice (non-medical).
|
| 85 |
-
- One concluding sentence reassuring the user to consult professionals if needed.
|
| 86 |
-
|
| 87 |
-
Keep it under 200 words, formatted in paragraphs.
|
| 88 |
-
'''
|
| 89 |
-
|
| 90 |
try:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
except Exception as e:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
return {"report": report_text}
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
# ====================== MAIN ======================
|
| 101 |
-
# Run with: uvicorn app:app --host 0.0.0.0 --port 7860
|
| 102 |
-
if __name__ == "__main__":
|
| 103 |
-
import uvicorn
|
| 104 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import pandas as pd
|
| 4 |
import joblib
|
| 5 |
import google.generativeai as genai
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "")
|
| 11 |
+
if GEMINI_API_KEY:
|
| 12 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
try:
|
| 15 |
model = joblib.load("model.pkl")
|
| 16 |
except Exception:
|
| 17 |
model = None
|
|
|
|
| 18 |
|
|
|
|
| 19 |
class InputData(BaseModel):
|
| 20 |
age: float
|
| 21 |
glucose: float
|
|
|
|
| 24 |
smoking: str
|
| 25 |
stress: int
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
@app.get("/")
|
| 28 |
+
def root():
|
| 29 |
return {"message": "Welcome to the GDM Predictor API", "status": "active"}
|
| 30 |
|
|
|
|
| 31 |
@app.post("/predict")
|
| 32 |
def predict(data: InputData):
|
| 33 |
if model is None:
|
| 34 |
return {"error": "Model not loaded"}
|
|
|
|
| 35 |
df = pd.DataFrame([data.dict()])
|
| 36 |
+
df["smoking"] = df["smoking"].apply(lambda x: 1 if x.lower()=="smoking" else 0)
|
| 37 |
+
pred = model.predict(df)[0]
|
| 38 |
+
prob = model.predict_proba(df)[0][1]
|
| 39 |
+
label = "High Risk" if pred==1 else "Low Risk"
|
| 40 |
+
return {"label": label, "probability": float(prob)}
|
|
|
|
|
|
|
| 41 |
|
| 42 |
@app.post("/generate-report")
|
| 43 |
def generate_report(data: InputData):
|
| 44 |
+
prompt = f"""
|
| 45 |
+
Patient Age: {data.age}, Glucose: {data.glucose}, CDKAL1: {data.cdkal1},
|
| 46 |
+
SOX17: {data.sox17}, Smoking: {data.smoking}, Stress: {data.stress}.
|
| 47 |
+
Write a short patient-friendly summary of their gestational diabetes risk.
|
| 48 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
+
model_ai = genai.GenerativeModel("gemini-1.5-flash")
|
| 51 |
+
result = model_ai.generate_content(prompt)
|
| 52 |
+
return {"report": result.text}
|
| 53 |
except Exception as e:
|
| 54 |
+
return {"report": f"Error generating report: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|