Nidhirk2113 commited on
Commit
3814d80
·
1 Parent(s): 7458a51

Added Procfile and backend setup for Hugging Face Space

Browse files
Files changed (5) hide show
  1. Procfile +0 -0
  2. README.md +1 -11
  3. app.py +104 -0
  4. dataset.csv +0 -0
  5. requirements.txt +8 -0
Procfile ADDED
Binary file (100 Bytes). View file
 
README.md CHANGED
@@ -1,11 +1 @@
1
- ---
2
- title: GDM Risk Predictor
3
- emoji: ⚡
4
- colorFrom: indigo
5
- colorTo: gray
6
- sdk: docker
7
- pinned: false
8
- license: mit
9
- ---
10
-
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # GDM Predictor Backend\nRun in Kaggle or local with:\n!uvicorn app:app --host 0.0.0.0 --port 7860
 
 
 
 
 
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # ====================== CONFIG ======================
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
+ # Allow frontend CORS
19
+ app.add_middleware(
20
+ CORSMiddleware,
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
38
+ cdkal1: int
39
+ sox17: int
40
+ smoking: str
41
+ stress: int
42
+
43
+
44
+ # ====================== ROUTES ======================
45
+
46
+ @app.get("/")
47
+ def home():
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() == "smoking" else 0)
58
+ prediction = model.predict(df)[0]
59
+ probability = model.predict_proba(df)[0][1]
60
+ label = "High Risk" if prediction == 1 else "Low Risk"
61
+
62
+ return {"label": label, "probability": float(probability)}
63
+
64
+
65
+ @app.post("/generate-report")
66
+ def generate_report(data: InputData):
67
+ # Generate AI-based personalized report using Gemini
68
+ prompt = f'''
69
+ You are an AI healthcare assistant. Generate a concise, patient-friendly report
70
+ summarizing the Gestational Diabetes risk based on the following patient details:
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
+ model_gemini = genai.GenerativeModel("gemini-1.5-flash")
92
+ response = model_gemini.generate_content(prompt)
93
+ report_text = response.text if response.text else "Report generation failed."
94
+ except Exception as e:
95
+ report_text = f"Error generating report: {str(e)}"
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)
dataset.csv ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ google-generativeai
4
+ shap
5
+ joblib
6
+ pandas
7
+ scikit-learn
8
+ python-multipart