ehsannotes commited on
Commit
6ab92a2
·
verified ·
1 Parent(s): c425f64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -3,46 +3,42 @@ from transformers import pipeline
3
  import requests
4
  import spacy
5
 
6
- # Load models
7
- nlp = spacy.load("en_core_web_sm") # For coherence analysis
8
- vocab_model = pipeline("text-classification", model="textattack/bert-base-uncased-CoLA") # Better for essays
9
 
10
  def evaluate_ielts(essay):
11
  # Vocabulary Assessment
12
- vocab_result = vocab_model(essay)[0]
13
- vocab_score = round(vocab_result['score'] * 9 * 1.2, 1) # Adjusted multiplier
 
 
14
 
15
- # Grammar Check (LanguageTool API)
16
  grammar_response = requests.post(
17
  "https://api.languagetool.org/v2/check",
18
  data={"text": essay, "language": "en-GB"}
19
  )
20
- grammar_errors = len(grammar_response.json()['matches'])
21
  grammar_score = max(1, 9 - (grammar_errors // 1.5)) # 1.5 errors = 1 point deduction
22
 
23
  # Coherence Analysis
24
  doc = nlp(essay)
25
- transition_words = ["however", "moreover", "therefore", "consequently"]
26
  transitions = sum(1 for token in doc if token.text.lower() in transition_words)
27
  coherence_score = min(9, transitions + 4) # 4-9 scale
28
 
29
- # Overall Band (IELTS-style weighting)
30
  overall = (vocab_score * 0.4) + (grammar_score * 0.3) + (coherence_score * 0.3)
31
 
32
  return {
33
- "Vocabulary Band": min(9, vocab_score),
34
  "Grammar Band": grammar_score,
35
  "Coherence Band": coherence_score,
36
  "Overall Band": round(overall, 1)
37
  }
38
 
 
39
  gr.Interface(
40
  fn=evaluate_ielts,
41
- inputs=gr.Textbox(label="Essay", lines=7),
42
- outputs=gr.JSON(),
43
- examples=[[
44
- "Education plays a vital role in societal development. "
45
- "Firstly, it equips individuals with critical thinking skills. "
46
- "Furthermore, educated citizens contribute to economic growth."
47
- ]]
48
- ).launch()
 
3
  import requests
4
  import spacy
5
 
6
+ # Load free models
7
+ nlp = spacy.load("en_core_web_sm")
8
+ vocab_model = pipeline("text-classification", model="roberta-base") # Free alternative
9
 
10
  def evaluate_ielts(essay):
11
  # Vocabulary Assessment
12
+ try:
13
+ vocab_score = min(9, vocab_model(essay)[0]['score'] * 9 + 1) # Adjusted scaling
14
+ except:
15
+ vocab_score = 6.0
16
 
17
+ # Grammar Check (Free API)
18
  grammar_response = requests.post(
19
  "https://api.languagetool.org/v2/check",
20
  data={"text": essay, "language": "en-GB"}
21
  )
22
+ grammar_errors = len(grammar_response.json().get('matches', []))
23
  grammar_score = max(1, 9 - (grammar_errors // 1.5)) # 1.5 errors = 1 point deduction
24
 
25
  # Coherence Analysis
26
  doc = nlp(essay)
27
+ transition_words = ["however", "moreover", "therefore", "furthermore"]
28
  transitions = sum(1 for token in doc if token.text.lower() in transition_words)
29
  coherence_score = min(9, transitions + 4) # 4-9 scale
30
 
31
+ # Overall Band Calculation
32
  overall = (vocab_score * 0.4) + (grammar_score * 0.3) + (coherence_score * 0.3)
33
 
34
  return {
35
+ "Vocabulary Band": round(vocab_score, 1),
36
  "Grammar Band": grammar_score,
37
  "Coherence Band": coherence_score,
38
  "Overall Band": round(overall, 1)
39
  }
40
 
41
+ # Create Interface
42
  gr.Interface(
43
  fn=evaluate_ielts,
44
+ inputs=gr.Textbox(label="Paste Your Essay",