mwaliahmad commited on
Commit
f3c19a0
·
1 Parent(s): ef1263f
Files changed (1) hide show
  1. app.py +16 -21
app.py CHANGED
@@ -1,28 +1,19 @@
1
  import gradio as gr
 
2
  import torch
3
- import numpy as np
4
- from transformers import AutoModelForSequenceClassification
5
 
6
- # Load ONLY the model, NOT the tokenizer
7
  model = AutoModelForSequenceClassification.from_pretrained(
8
  "Kevintu/Engessay_grading_ML")
 
9
 
10
 
11
- def process_embeddings(embeddings_array):
12
- # Convert the received embeddings to the format expected by the model
13
- embeddings_tensor = torch.tensor(embeddings_array)
14
-
15
- # Process embeddings with the model
16
  model.eval()
17
  with torch.no_grad():
18
- # Create a dict with the expected input format
19
- model_inputs = {
20
- 'input_ids': None, # Not needed since we're using embeddings directly
21
- 'attention_mask': None, # Not needed for this use case
22
- 'inputs_embeds': embeddings_tensor # Pass embeddings directly
23
- }
24
- outputs = model(**model_inputs)
25
-
26
  predictions = outputs.logits.squeeze()
27
 
28
  item_names = ["cohesion", "syntax", "vocabulary",
@@ -35,14 +26,18 @@ def process_embeddings(embeddings_array):
35
  return results
36
 
37
 
38
- # Create Gradio interface for embeddings input
39
  demo = gr.Interface(
40
- fn=process_embeddings,
41
- inputs=gr.JSON(label="Embeddings"),
42
- outputs=gr.JSON(label="Scores"),
43
  title="Essay Grading API",
44
- description="Grade essays based on precomputed embeddings"
 
 
 
45
  )
46
 
 
47
  demo.queue()
48
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
  import torch
 
 
4
 
5
+ # Load model and tokenizer
6
  model = AutoModelForSequenceClassification.from_pretrained(
7
  "Kevintu/Engessay_grading_ML")
8
+ tokenizer = AutoTokenizer.from_pretrained("KevSun/Engessay_grading_ML")
9
 
10
 
11
+ def grade_essay(text):
12
+ encoded_input = tokenizer(
13
+ text, return_tensors='pt', padding=True, truncation=True, max_length=64)
 
 
14
  model.eval()
15
  with torch.no_grad():
16
+ outputs = model(**encoded_input)
 
 
 
 
 
 
 
17
  predictions = outputs.logits.squeeze()
18
 
19
  item_names = ["cohesion", "syntax", "vocabulary",
 
26
  return results
27
 
28
 
29
+ # Create Gradio interface
30
  demo = gr.Interface(
31
+ fn=grade_essay,
32
+ inputs=gr.Textbox(lines=10, placeholder="Enter essay text here..."),
33
+ outputs=gr.JSON(),
34
  title="Essay Grading API",
35
+ description="Grade essays on six dimensions of writing quality",
36
+ examples=[
37
+ ["The English Language Learner Insight, Proficiency and Skills Evaluation (ELLIPSE) Corpus is a freely available corpus of ~6,500 ELL writing samples that have been scored for overall holistic language proficiency as well as analytic proficiency scores."]
38
+ ],
39
  )
40
 
41
+ # For API access
42
  demo.queue()
43
  demo.launch()