Nawal20 commited on
Commit
802dcc0
·
verified ·
1 Parent(s): bfd93ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -6,7 +6,7 @@ import lightgbm as lgb
6
  from sentence_transformers import SentenceTransformer
7
  import numpy as np
8
 
9
- # Load files from the model repo "your-username/Essay"
10
  repo_id = "Nawal20/Essay"
11
 
12
  ridge_path = hf_hub_download(repo_id=repo_id, filename="ridge_model.pkl")
@@ -22,36 +22,36 @@ encoder = joblib.load(encoder_path)
22
  with open(metadata_path, "r") as f:
23
  metadata_columns = json.load(f)
24
 
25
- # Load SBERT model (will download at runtime)
26
  sbert = SentenceTransformer("sentence-transformers/paraphrase-mpnet-base-v2")
27
 
28
- def predict_score(essay_text, gender, race_ethnicity, assignment, prompt_name, disability, disadvantaged, ell_status):
29
- # Encode essay
30
- essay_embedding = sbert.encode([essay_text])
31
-
32
- # Prepare metadata as dict
33
  metadata_input = {
34
- "gender": "female",
35
- "race_ethnicity": "Asian",
36
- "assignment": "Informative", # ✅ must be here
37
- "prompt_name": "Education Benefits",
38
- "economically_disadvantaged": "No",
39
- "student_disability_status": "None",
40
- "ell_status": "No"
41
  }
42
 
43
- # Convert to array in correct order
44
  metadata_values = [metadata_input[col] for col in metadata_columns]
45
- metadata_array = encoder.transform([metadata_values]) # shape: (1, n)
46
 
47
- # Combine essay embedding + metadata
48
- full_input = np.hstack([essay_embedding, metadata_array])
49
 
50
- # Predict from both models
51
  ridge_score = ridge.predict(full_input)[0]
52
  lgb_score = lgb_model.predict(full_input)[0]
53
- #final_score = round((0.5 * ridge_score + 0.5 * lgb_score), 2)
54
- final_score = (ridge_pred + lgb_pred) / 2
55
  return final_score
56
 
57
  # Gradio UI
@@ -61,14 +61,14 @@ iface = gr.Interface(
61
  gr.Textbox(label="Essay Text", lines=10, placeholder="Paste your essay here..."),
62
  gr.Dropdown(["Male", "Female", "Other"], label="Gender"),
63
  gr.Dropdown(["Asian", "Black", "Hispanic", "White", "Other"], label="Race/Ethnicity"),
64
- gr.Dropdown(["Informative", "Argumentative", "Narrative"], label="Assignment"), # ✅ ADD
65
- gr.Dropdown(["Education Benefits", "Technology Impact", "Climate Change"], label="Prompt Name"), # ✅ ADD
66
  gr.Dropdown(["Yes", "No"], label="Economically Disadvantaged"),
67
- gr.Dropdown(["Yes", "No"], label="Student has Disability"),
68
  gr.Dropdown(["Yes", "No"], label="ELL Status"),
69
  ],
70
  outputs=gr.Number(label="Predicted Essay Score"),
71
- title="Automated Essay Scoring App"
72
  )
73
 
74
  iface.launch()
 
6
  from sentence_transformers import SentenceTransformer
7
  import numpy as np
8
 
9
+ # Load files from the model repo
10
  repo_id = "Nawal20/Essay"
11
 
12
  ridge_path = hf_hub_download(repo_id=repo_id, filename="ridge_model.pkl")
 
22
  with open(metadata_path, "r") as f:
23
  metadata_columns = json.load(f)
24
 
25
+ # Load SBERT model
26
  sbert = SentenceTransformer("sentence-transformers/paraphrase-mpnet-base-v2")
27
 
28
+ def predict_score(essay_text, gender, race_ethnicity, assignment, prompt_name, disadvantaged, disability, ell_status):
29
+ # Encode the essay
30
+ essay_embedding = sbert.encode([essay_text]) # shape (1, 768)
31
+
32
+ # Create metadata dict from input
33
  metadata_input = {
34
+ "gender": gender,
35
+ "race_ethnicity": race_ethnicity,
36
+ "assignment": assignment,
37
+ "prompt_name": prompt_name,
38
+ "economically_disadvantaged": disadvantaged,
39
+ "student_disability_status": disability,
40
+ "ell_status": ell_status
41
  }
42
 
43
+ # Create input array based on column order
44
  metadata_values = [metadata_input[col] for col in metadata_columns]
45
+ metadata_array = encoder.transform([metadata_values]) # shape (1, N)
46
 
47
+ # Combine essay + metadata
48
+ full_input = np.hstack([essay_embedding.reshape(1, -1), metadata_array.toarray()])
49
 
50
+ # Predict scores
51
  ridge_score = ridge.predict(full_input)[0]
52
  lgb_score = lgb_model.predict(full_input)[0]
53
+ final_score = round((ridge_score + lgb_score) / 2, 2)
54
+
55
  return final_score
56
 
57
  # Gradio UI
 
61
  gr.Textbox(label="Essay Text", lines=10, placeholder="Paste your essay here..."),
62
  gr.Dropdown(["Male", "Female", "Other"], label="Gender"),
63
  gr.Dropdown(["Asian", "Black", "Hispanic", "White", "Other"], label="Race/Ethnicity"),
64
+ gr.Dropdown(["Informative", "Argumentative", "Narrative"], label="Assignment"),
65
+ gr.Dropdown(["Education Benefits", "Technology Impact", "Climate Change"], label="Prompt Name"),
66
  gr.Dropdown(["Yes", "No"], label="Economically Disadvantaged"),
67
+ gr.Dropdown(["None", "Learning", "Physical", "Other"], label="Student has Disability"),
68
  gr.Dropdown(["Yes", "No"], label="ELL Status"),
69
  ],
70
  outputs=gr.Number(label="Predicted Essay Score"),
71
+ title="📘 Automated Essay Scoring App"
72
  )
73
 
74
  iface.launch()