Spaces:
Sleeping
Sleeping
Download app.py from nidhik-devs/GDM-Risk-Predictor: direct link, hf CLI and curl.
- Browser
- Download file 1.58 kB
-
https://huggingface.co/spaces/nidhik-devs/GDM-Risk-Predictor/resolve/main/app.py
- Command line
-
hf download hf://spaces/nidhik-devs/GDM-Risk-Predictor/app.py
-
curl -L -o app.py https://huggingface.co/spaces/nidhik-devs/GDM-Risk-Predictor/resolve/main/app.py
1.58 kB
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| import pandas as pd | |
| import joblib | |
| import google.generativeai as genai | |
| import os | |
| app = FastAPI() | |
| GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "") | |
| if GEMINI_API_KEY: | |
| genai.configure(api_key=GEMINI_API_KEY) | |
| try: | |
| model = joblib.load("model.pkl") | |
| except Exception: | |
| model = None | |
| class InputData(BaseModel): | |
| age: float | |
| glucose: float | |
| cdkal1: int | |
| sox17: int | |
| smoking: str | |
| stress: int | |
| def root(): | |
| return {"message": "Welcome to the GDM Predictor API", "status": "active"} | |
| def predict(data: InputData): | |
| if model is None: | |
| return {"error": "Model not loaded"} | |
| df = pd.DataFrame([data.dict()]) | |
| df["smoking"] = df["smoking"].apply(lambda x: 1 if x.lower()=="smoking" else 0) | |
| pred = model.predict(df)[0] | |
| prob = model.predict_proba(df)[0][1] | |
| label = "High Risk" if pred==1 else "Low Risk" | |
| return {"label": label, "probability": float(prob)} | |
| def generate_report(data: InputData): | |
| prompt = f""" | |
| Patient Age: {data.age}, Glucose: {data.glucose}, CDKAL1: {data.cdkal1}, | |
| SOX17: {data.sox17}, Smoking: {data.smoking}, Stress: {data.stress}. | |
| Write a short patient-friendly summary of their gestational diabetes risk. | |
| """ | |
| try: | |
| model_ai = genai.GenerativeModel("gemini-1.5-flash") | |
| result = model_ai.generate_content(prompt) | |
| return {"report": result.text} | |
| except Exception as e: | |
| return {"report": f"Error generating report: {str(e)}"} | |