Web4 commited on
Commit
4277bd8
·
verified ·
1 Parent(s): 5c009d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -4,25 +4,28 @@ 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,
@@ -35,7 +38,11 @@ def predict_impact(
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],
@@ -49,16 +56,16 @@ def predict_impact(
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=[
@@ -75,9 +82,9 @@ demo = gr.Interface(
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()
 
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
+ # The filename now includes the 'data/' subdirectory.
9
  repo_id = "Web4/LS-W4-Mini-RF_Addiction_Impact"
10
+ model_file = "data/LS-W4-Mini-RF_Addiction_Impact.joblib"
11
 
12
+ # Get the Hugging Face token from environment variables.
13
+ # This is required for gated repositories.
14
  token = os.environ.get("HF_TOKEN")
15
 
16
+ # Download the model file from the Hugging Face Hub using the token.
17
  try:
18
  model_path = hf_hub_download(repo_id=repo_id, filename=model_file, token=token)
19
  print(f"Model downloaded to: {model_path}")
20
  except Exception as e:
21
+ # This error indicates that the file was not found or access was denied.
22
  print(f"Error downloading model: {e}")
23
  raise
24
 
25
+ # Load the scikit-learn pipeline from the downloaded joblib file.
26
  pipeline = joblib.load(model_path)
27
 
28
+ # Define the prediction function for the Gradio interface.
29
  def predict_impact(
30
  gender,
31
  academic_level,
 
38
  addicted_score,
39
  conflicts_over_social_media
40
  ):
41
+ """
42
+ Takes user inputs, creates a pandas DataFrame, and makes a prediction
43
+ using the loaded scikit-learn pipeline.
44
+ """
45
+ # Create a pandas DataFrame from the user inputs.
46
  input_data = pd.DataFrame({
47
  'Gender': [gender],
48
  'Academic_Level': [academic_level],
 
56
  'Conflicts_Over_Social_Media': [conflicts_over_social_media]
57
  })
58
 
59
+ # Make a prediction. The pipeline handles the preprocessing automatically.
60
  prediction = pipeline.predict(input_data)[0]
61
 
62
+ # Return a user-friendly result based on the prediction.
63
  if prediction == 1:
64
  return "Prediction: Yes, social media use is likely to impact academic performance."
65
  else:
66
  return "Prediction: No, social media use is likely not to impact academic performance."
67
 
68
+ # Define the Gradio interface components.
69
  demo = gr.Interface(
70
  fn=predict_impact,
71
  inputs=[
 
82
  ],
83
  outputs="text",
84
  title="Social Media Addiction Impact on Academic Performance",
85
+ description="A Random Forest model to predict if social media use impacts a student's academic performance. This is not a diagnostic tool."
86
  )
87
 
88
+ # Launch the Gradio app.
89
  if __name__ == "__main__":
90
+ demo.launch()