ehsannotes commited on
Commit
b4e7fb7
·
verified ·
1 Parent(s): bf0c292

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -32
app.py CHANGED
@@ -1,44 +1,93 @@
1
  import gradio as gr
2
- from transformers import pipeline
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",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
2
  import spacy
3
+ import requests
4
+ from transformers import pipeline
5
+ from supabase import create_client
6
+ from statistics import mean
7
 
8
+ # Configuration
9
+ SUPABASE_URL = "your-supabase-url"
10
+ SUPABASE_KEY = "your-supabase-key"
11
+ HF_TOKEN = "your-hf-token"
12
+ nlp = spacy.load("en_core_web_lg")
13
 
14
+ # Models
15
+ MODELS = {
16
+ "vocab": pipeline("text-classification",
17
+ model="domenicrosati/IELTS-writing-task-2-rater"),
18
+ "grammar": pipeline("text2text-generation",
19
+ model="vennify/t5-base-grammar-correction"),
20
+ "coherence": pipeline("feature-extraction",
21
+ model="sentence-transformers/all-mpnet-base-v2")
22
+ }
23
+
24
+ # Supabase Client
25
+ supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
26
+
27
+ def analyze_text(essay):
28
+ """Comprehensive text analysis using multiple NLP techniques"""
29
+ doc = nlp(essay)
30
+
31
+ # Vocabulary Analysis
32
+ vocab_score = MODELS["vocab"](essay)[0]["score"] * 9
33
+
34
+ # Grammar Analysis
35
+ grammar_errors = len(MODELS["grammar"](f"grammar: {essay}")[0]["generated_text"].split("<error>")) - 1
36
+ grammar_score = max(1, 9 - (grammar_errors // 1.2))
37
 
38
  # Coherence Analysis
39
+ embeddings = MODELS["coherence"](essay)
40
+ transition_words = ["however", "moreover", "furthermore", "consequently"]
41
  transitions = sum(1 for token in doc if token.text.lower() in transition_words)
42
+ coherence_score = min(9, 4 + (transitions * 0.5) + (len(doc.ents) * 0.2))
43
 
44
+ # Task Achievement (Using Mistral-7B)
45
+ task_response = requests.post(
46
+ "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3",
47
+ headers={"Authorization": f"Bearer {HF_TOKEN}"},
48
+ json={"inputs": f"Rate IELTS task achievement (1-9): {essay}"}
49
+ )
50
+ task_score = int(task_response.json()[0]["generated_text"][:1]) if task_response.ok else 6
51
 
52
  return {
53
+ "vocab": round(vocab_score, 1),
54
+ "grammar": grammar_score,
55
+ "coherence": round(coherence_score, 1),
56
+ "task": task_score,
57
+ "overall": round(mean([vocab_score, grammar_score, coherence_score, task_score]), 1)
58
  }
59
 
60
+ def evaluate_ielts(essay):
61
+ analysis = analyze_text(essay)
62
+
63
+ # Store results
64
+ supabase.table("ielts_results").insert({
65
+ "essay": essay,
66
+ "scores": analysis,
67
+ "timestamp": "now()"
68
+ }).execute()
69
+
70
+ return analysis
71
+
72
+ # Professional Gradio Interface
73
+ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 800px!important}") as demo:
74
+ gr.Markdown("## Professional IELTS Evaluation System")
75
+
76
+ with gr.Row():
77
+ essay_input = gr.Textbox(label="Enter Essay", lines=10,
78
+ placeholder="Discuss both views and give your opinion...")
79
+ output_json = gr.JSON(label="Evaluation Results")
80
+
81
+ with gr.Row():
82
+ gr.Examples(
83
+ examples=[
84
+ ["While some argue technology improves learning, others believe it distracts students. This essay examines both perspectives before concluding that balanced use offers optimal benefits."],
85
+ ["Environmental protection requires global cooperation. Governments must implement stricter regulations while promoting sustainable energy alternatives to combat climate change effectively."]
86
+ ],
87
+ inputs=essay_input
88
+ )
89
+
90
+ submit_btn = gr.Button("Evaluate", variant="primary")
91
+ submit_btn.click(fn=evaluate_ielts, inputs=essay_input, outputs=output_json)
92
+
93
+ demo.launch()