Web4 commited on
Commit
5c009d8
·
verified ·
1 Parent(s): 2d2b9d3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import joblib
4
+ from huggingface_hub import hf_hub_download
5
+ import os
6
+
7
+ # Define the model path and file name on Hugging Face Hub
8
+ repo_id = "Web4/LS-W4-Mini-RF_Addiction_Impact"
9
+ model_file = "LS-W4-Mini-RF_Addiction_Impact.joblib"
10
+
11
+ # Get the Hugging Face token from environment variables
12
+ token = os.environ.get("HF_TOKEN")
13
+
14
+ # Download the model file from the Hugging Face Hub using the token
15
+ try:
16
+ model_path = hf_hub_download(repo_id=repo_id, filename=model_file, token=token)
17
+ print(f"Model downloaded to: {model_path}")
18
+ except Exception as e:
19
+ print(f"Error downloading model: {e}")
20
+ raise
21
+
22
+ # Load the scikit-learn pipeline from the downloaded joblib file
23
+ pipeline = joblib.load(model_path)
24
+
25
+ # Define the prediction function for the Gradio interface
26
+ def predict_impact(
27
+ gender,
28
+ academic_level,
29
+ most_used_platform,
30
+ relationship_status,
31
+ age,
32
+ avg_daily_usage_hours,
33
+ sleep_hours_per_night,
34
+ mental_health_score,
35
+ addicted_score,
36
+ conflicts_over_social_media
37
+ ):
38
+ # Create a pandas DataFrame from the user inputs
39
+ input_data = pd.DataFrame({
40
+ 'Gender': [gender],
41
+ 'Academic_Level': [academic_level],
42
+ 'Most_Used_Platform': [most_used_platform],
43
+ 'Relationship_Status': [relationship_status],
44
+ 'Age': [age],
45
+ 'Avg_Daily_Usage_Hours': [avg_daily_usage_hours],
46
+ 'Sleep_Hours_Per_Night': [sleep_hours_per_night],
47
+ 'Mental_Health_Score': [mental_health_score],
48
+ 'Addicted_Score': [addicted_score],
49
+ 'Conflicts_Over_Social_Media': [conflicts_over_social_media]
50
+ })
51
+
52
+ # Make a prediction using the loaded pipeline
53
+ prediction = pipeline.predict(input_data)[0]
54
+
55
+ # Return a user-friendly result based on the prediction
56
+ if prediction == 1:
57
+ return "Prediction: Yes, social media use is likely to impact academic performance."
58
+ else:
59
+ return "Prediction: No, social media use is likely not to impact academic performance."
60
+
61
+ # Define the Gradio interface components
62
+ demo = gr.Interface(
63
+ fn=predict_impact,
64
+ inputs=[
65
+ gr.Dropdown(["Male", "Female"], label="Gender"),
66
+ gr.Dropdown(["Undergraduate", "Postgraduate", "High School"], label="Academic_Level"),
67
+ gr.Dropdown(["Instagram", "Facebook", "Twitter", "YouTube", "WhatsApp", "Other"], label="Most_Used_Platform"),
68
+ gr.Dropdown(["Single", "In a relationship"], label="Relationship_Status"),
69
+ gr.Slider(16, 25, value=20, label="Age"),
70
+ gr.Slider(0, 24, value=3.0, label="Avg_Daily_Usage_Hours"),
71
+ gr.Slider(0, 12, value=7, label="Sleep_Hours_Per_Night"),
72
+ gr.Slider(0, 10, value=5, label="Mental_Health_Score (0-10)"),
73
+ gr.Slider(0, 10, value=5, label="Addicted_Score (0-10)"),
74
+ gr.Dropdown([0, 1], label="Conflicts_Over_Social_Media (0=No, 1=Yes)")
75
+ ],
76
+ outputs="text",
77
+ title="Social Media Addiction Impact on Academic Performance",
78
+ description="A Random Forest model to predict if social media use impacts a student's academic performance."
79
+ )
80
+
81
+ # Launch the Gradio app
82
+ if __name__ == "__main__":
83
+ demo.launch()