Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import spacy | |
| import requests | |
| from transformers import pipeline | |
| from supabase import create_client | |
| from statistics import mean | |
| # Configuration | |
| SUPABASE_URL = "your-supabase-url" | |
| SUPABASE_KEY = "your-supabase-key" | |
| HF_TOKEN = "your-hf-token" | |
| nlp = spacy.load("en_core_web_lg") | |
| # Models | |
| MODELS = { | |
| "vocab": pipeline("text-classification", | |
| model="domenicrosati/IELTS-writing-task-2-rater"), | |
| "grammar": pipeline("text2text-generation", | |
| model="vennify/t5-base-grammar-correction"), | |
| "coherence": pipeline("feature-extraction", | |
| model="sentence-transformers/all-mpnet-base-v2") | |
| } | |
| # Supabase Client | |
| supabase = create_client(SUPABASE_URL, SUPABASE_KEY) | |
| def analyze_text(essay): | |
| """Comprehensive text analysis using multiple NLP techniques""" | |
| doc = nlp(essay) | |
| # Vocabulary Analysis | |
| vocab_score = MODELS["vocab"](essay)[0]["score"] * 9 | |
| # Grammar Analysis | |
| grammar_errors = len(MODELS["grammar"](f"grammar: {essay}")[0]["generated_text"].split("<error>")) - 1 | |
| grammar_score = max(1, 9 - (grammar_errors // 1.2)) | |
| # Coherence Analysis | |
| embeddings = MODELS["coherence"](essay) | |
| transition_words = ["however", "moreover", "furthermore", "consequently"] | |
| transitions = sum(1 for token in doc if token.text.lower() in transition_words) | |
| coherence_score = min(9, 4 + (transitions * 0.5) + (len(doc.ents) * 0.2)) | |
| # Task Achievement (Using Mistral-7B) | |
| task_response = requests.post( | |
| "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3", | |
| headers={"Authorization": f"Bearer {HF_TOKEN}"}, | |
| json={"inputs": f"Rate IELTS task achievement (1-9): {essay}"} | |
| ) | |
| task_score = int(task_response.json()[0]["generated_text"][:1]) if task_response.ok else 6 | |
| return { | |
| "vocab": round(vocab_score, 1), | |
| "grammar": grammar_score, | |
| "coherence": round(coherence_score, 1), | |
| "task": task_score, | |
| "overall": round(mean([vocab_score, grammar_score, coherence_score, task_score]), 1) | |
| } | |
| def evaluate_ielts(essay): | |
| analysis = analyze_text(essay) | |
| # Store results | |
| supabase.table("ielts_results").insert({ | |
| "essay": essay, | |
| "scores": analysis, | |
| "timestamp": "now()" | |
| }).execute() | |
| return analysis | |
| # Professional Gradio Interface | |
| with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 800px!important}") as demo: | |
| gr.Markdown("## Professional IELTS Evaluation System") | |
| with gr.Row(): | |
| essay_input = gr.Textbox(label="Enter Essay", lines=10, | |
| placeholder="Discuss both views and give your opinion...") | |
| output_json = gr.JSON(label="Evaluation Results") | |
| with gr.Row(): | |
| gr.Examples( | |
| examples=[ | |
| ["While some argue technology improves learning, others believe it distracts students. This essay examines both perspectives before concluding that balanced use offers optimal benefits."], | |
| ["Environmental protection requires global cooperation. Governments must implement stricter regulations while promoting sustainable energy alternatives to combat climate change effectively."] | |
| ], | |
| inputs=essay_input | |
| ) | |
| submit_btn = gr.Button("Evaluate", variant="primary") | |
| submit_btn.click(fn=evaluate_ielts, inputs=essay_input, outputs=output_json) | |
| demo.launch() |