Nawal20 commited on
Commit
1a80667
·
verified ·
1 Parent(s): d1778f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -18
app.py CHANGED
@@ -1,25 +1,61 @@
1
  import gradio as gr
2
  import joblib
3
- import numpy as np
4
- from sentence_transformers import SentenceTransformer
5
  import lightgbm as lgb
 
 
 
 
 
 
 
6
 
7
- # Load models
8
- ridge = joblib.load("model/ridge_model.pkl")
9
- lgbm = lgb.Booster(model_file="model/lgb_model.txt")
10
- sbert = SentenceTransformer('paraphrase-mpnet-base-v2')
11
 
12
- def predict_score(essay_text):
13
- embedding = sbert.encode([essay_text])
14
- ridge_score = ridge.predict(embedding)[0]
15
- lgbm_score = lgbm.predict(embedding)[0]
16
- final_score = 0.5 * ridge_score + 0.5 * lgbm_score
17
- return round(final_score, 2)
18
 
19
- gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  fn=predict_score,
21
- inputs="text",
22
- outputs="number",
23
- title="Automated Essay Scorer",
24
- description="Paste your essay below and get a predicted score."
25
- ).launch()
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import joblib
3
+ import json
 
4
  import lightgbm as lgb
5
+ from sentence_transformers import SentenceTransformer
6
+ import numpy as np
7
+
8
+ # Load models from Hugging Face Hub
9
+ ridge = joblib.load("Essay/ridge_model.pkl")
10
+ lgb_model = lgb.Booster(model_file="Essay/lightgbm_model.txt")
11
+ encoder = joblib.load("Essay/scaler_encoder.pkl")
12
 
13
+ # Load metadata column order
14
+ with open("Essay/metadata_columns.json", "r") as f:
15
+ metadata_columns = json.load(f)
 
16
 
17
+ # Load SBERT model (will download at runtime)
18
+ sbert = SentenceTransformer("sentence-transformers/paraphrase-mpnet-base-v2")
 
 
 
 
19
 
20
+ def predict_score(essay_text, gender, race_ethnicity, disability, disadvantaged, ell_status):
21
+ # Encode essay
22
+ essay_embedding = sbert.encode([essay_text])
23
+
24
+ # Prepare metadata as dict
25
+ metadata_input = {
26
+ "gender": gender,
27
+ "race_ethnicity": race_ethnicity,
28
+ "student_disability_status": disability,
29
+ "economically_disadvantaged": disadvantaged,
30
+ "ell_status": ell_status
31
+ }
32
+
33
+ # Convert to array in correct order
34
+ metadata_values = [metadata_input[col] for col in metadata_columns]
35
+ metadata_array = encoder.transform([metadata_values]) # shape: (1, n)
36
+
37
+ # Combine essay embedding + metadata
38
+ full_input = np.hstack([essay_embedding, metadata_array])
39
+
40
+ # Predict from both models
41
+ ridge_score = ridge.predict(full_input)[0]
42
+ lgb_score = lgb_model.predict(full_input)[0]
43
+ final_score = round((0.5 * ridge_score + 0.5 * lgb_score), 2)
44
+ return final_score
45
+
46
+ # Gradio UI
47
+ iface = gr.Interface(
48
  fn=predict_score,
49
+ inputs=[
50
+ gr.Textbox(label="Essay Text", lines=10, placeholder="Paste your essay here..."),
51
+ gr.Dropdown(["Male", "Female", "Other"], label="Gender"),
52
+ gr.Dropdown(["Asian", "Black", "Hispanic", "White", "Other"], label="Race/Ethnicity"),
53
+ gr.Dropdown(["Yes", "No"], label="Student has Disability"),
54
+ gr.Dropdown(["Yes", "No"], label="Economically Disadvantaged"),
55
+ gr.Dropdown(["Yes", "No"], label="ELL Status")
56
+ ],
57
+ outputs=gr.Number(label="Predicted Essay Score"),
58
+ title="Automated Essay Scoring App"
59
+ )
60
+
61
+ iface.launch()