Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- Mnetal_Health_Model.pkl +3 -0
- README.md +14 -6
- app.py +51 -0
- main.py +87 -0
- requirements.txt +6 -0
Mnetal_Health_Model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e6993c7c740681178645fa421743b8bcc15450f1940a609a5c5059e2470c7c49
|
| 3 |
+
size 25616627
|
README.md
CHANGED
|
@@ -1,13 +1,21 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.31.0
|
| 8 |
-
python_version:
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: MindScope API
|
| 3 |
+
emoji: 🧠
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.31.0
|
| 8 |
+
python_version: "3.10"
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# MindScope API
|
| 14 |
+
|
| 15 |
+
FastAPI backend for MindScope, a student mental health score predictor.
|
| 16 |
+
|
| 17 |
+
- `GET /hello` — health check
|
| 18 |
+
- `POST /predict` — returns `{"predicted_mental_health_score": <float>}`
|
| 19 |
+
- `/docs` — interactive API docs
|
| 20 |
+
|
| 21 |
+
Educational project. Predictions are machine learning estimates, not clinical assessments.
|
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Hugging Face Spaces entry point (Gradio SDK, free tier).
|
| 3 |
+
|
| 4 |
+
Docker Spaces are paid, so this file runs the existing FastAPI app from
|
| 5 |
+
main.py on the Gradio SDK instead. The API is unchanged:
|
| 6 |
+
GET /hello
|
| 7 |
+
POST /predict
|
| 8 |
+
A small Gradio page is mounted at "/" so the Space has a landing page.
|
| 9 |
+
"""
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
# On ZeroGPU hardware, Hugging Face refuses to start unless at least one
|
| 13 |
+
# function is decorated with @spaces.GPU. The model runs on CPU, so this
|
| 14 |
+
# placeholder is never called. On CPU hardware (or locally) `spaces` may
|
| 15 |
+
# not be installed, and the placeholder is simply skipped.
|
| 16 |
+
try:
|
| 17 |
+
import spaces
|
| 18 |
+
|
| 19 |
+
@spaces.GPU
|
| 20 |
+
def _zerogpu_placeholder():
|
| 21 |
+
return None
|
| 22 |
+
except ImportError:
|
| 23 |
+
pass
|
| 24 |
+
|
| 25 |
+
import gradio as gr
|
| 26 |
+
import uvicorn
|
| 27 |
+
|
| 28 |
+
from main import app # the FastAPI app with /hello and /predict
|
| 29 |
+
|
| 30 |
+
with gr.Blocks(title="MindScope API") as info_page:
|
| 31 |
+
gr.Markdown(
|
| 32 |
+
"""
|
| 33 |
+
# MindScope API
|
| 34 |
+
This Space hosts the prediction API for the MindScope project.
|
| 35 |
+
|
| 36 |
+
- `GET /hello` — health check
|
| 37 |
+
- `POST /predict` — returns `predicted_mental_health_score`
|
| 38 |
+
- `/docs` — interactive API documentation
|
| 39 |
+
|
| 40 |
+
Educational project. Predictions are machine learning estimates,
|
| 41 |
+
not clinical assessments.
|
| 42 |
+
"""
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Routes defined in main.py are registered first, so they take priority
|
| 46 |
+
# over the Gradio page mounted at the root.
|
| 47 |
+
app = gr.mount_gradio_app(app, info_page, path="/")
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
port = int(os.environ.get("GRADIO_SERVER_PORT", os.environ.get("PORT", 7860)))
|
| 51 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
main.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel, Field
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from typing import Literal
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
|
| 8 |
+
model = joblib.load('Mnetal_Health_Model.pkl') #importing data
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"]
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# PYNDATIC MODEL
|
| 20 |
+
|
| 21 |
+
class StudentData(BaseModel):
|
| 22 |
+
Age : int = Field(..., gt=10, le=100, description="Age must be a positive integer")
|
| 23 |
+
Gender : Literal['Male', 'Female']
|
| 24 |
+
Country : str
|
| 25 |
+
Academic_Level : Literal['Undergraduate', 'Graduate', 'High School']
|
| 26 |
+
Most_Used_Platform : Literal['Facebook', 'LinkedIn', 'Instagram', 'Snapchat', 'Twitter',
|
| 27 |
+
'YouTube', 'TikTok', 'LINE', 'KakaoTalk', 'VKontakte', 'WhatsApp',
|
| 28 |
+
'WeChat']
|
| 29 |
+
Purpose_Of_Use : Literal['Networking', 'Education', 'Entertainment', 'News']
|
| 30 |
+
Avg_Daily_Usage_Hours : float = Field(..., gt=0, le=24, description="Average daily usage hours must be between 0 and 24")
|
| 31 |
+
Daily_Unlocks : int = Field(..., gt=0, description="Daily unlocks must be a positive integer")
|
| 32 |
+
Study_Hours : float = Field(..., gt=0, le=24, description="Study hours must be between 0 and 24")
|
| 33 |
+
Physical_Activity_Hours : float = Field(..., gt = 0, le = 24)
|
| 34 |
+
Sleep_Hours_Per_Night : float = Field(..., gt=0, le=24, description="Sleep hours per night must be between 0 and 24")
|
| 35 |
+
Stress_Level : Literal['Low', 'Medium', 'High', 'Very High']
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
#desribe what we send back
|
| 41 |
+
|
| 42 |
+
class predictionResponse(BaseModel) :
|
| 43 |
+
predicted_mental_health_score : float = Field(..., description="Predicted mental health score")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@app.get('/hello')
|
| 48 |
+
def greet():
|
| 49 |
+
return('hello guys this is me testing FASTAPI')
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
top_countries = ['Other',
|
| 53 |
+
'India',
|
| 54 |
+
'USA',
|
| 55 |
+
'Canada',
|
| 56 |
+
'Australia',
|
| 57 |
+
'UK',
|
| 58 |
+
'Germany',
|
| 59 |
+
'Turkey',
|
| 60 |
+
'Mexico',
|
| 61 |
+
'France']
|
| 62 |
+
|
| 63 |
+
@app.post('/predict', response_model=predictionResponse)
|
| 64 |
+
def predict(data: StudentData):
|
| 65 |
+
|
| 66 |
+
country_group = data.Country if data.Country in top_countries else "Other"
|
| 67 |
+
|
| 68 |
+
input_row = pd.DataFrame( [{
|
| 69 |
+
'Age' : data.Age,
|
| 70 |
+
'Gender' : data.Gender,
|
| 71 |
+
'Country' : data.Country,
|
| 72 |
+
'Academic_Level' : data.Academic_Level,
|
| 73 |
+
'Most_Used_Platform' : data.Most_Used_Platform,
|
| 74 |
+
'Purpose_Of_Use' : data.Purpose_Of_Use,
|
| 75 |
+
'Avg_Daily_Usage_Hours' : data.Avg_Daily_Usage_Hours,
|
| 76 |
+
'Daily_Unlocks' : data.Daily_Unlocks,
|
| 77 |
+
'Study_Hours' : data.Study_Hours,
|
| 78 |
+
'Physical_Activity_Hours' : data.Physical_Activity_Hours,
|
| 79 |
+
'Sleep_Hours_Per_Night' : data.Sleep_Hours_Per_Night,
|
| 80 |
+
'Stress_Level' : data.Stress_Level,
|
| 81 |
+
'Grouped Country' : country_group
|
| 82 |
+
}])
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
prediction = model.predict(input_row)[0]
|
| 86 |
+
|
| 87 |
+
return predictionResponse(predicted_mental_health_score=round(float(prediction),2))
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gradio (preinstalled by the Space) already brings fastapi, uvicorn and pydantic.
|
| 2 |
+
# The model was trained with these versions; keep them pinned.
|
| 3 |
+
scikit-learn==1.7.2
|
| 4 |
+
pandas==2.3.3
|
| 5 |
+
numpy==2.2.6
|
| 6 |
+
joblib==1.5.3
|