Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Coherence Analysis
|
| 26 |
-
|
| 27 |
-
transition_words = ["however", "moreover", "
|
| 28 |
transitions = sum(1 for token in doc if token.text.lower() in transition_words)
|
| 29 |
-
coherence_score = min(9, transitions +
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
return {
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
-
"
|
| 38 |
-
"
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|