Spaces:
Sleeping
Sleeping
edited: app.py
Browse files8 unmodified lines910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656624 unmodified lines919293949596971 unmodified line991001011021031041051061071081091101111121131141151161171181191201216 unmodified lines1281291301311321331341351361371381398 unmodified linesclient = Groq(api_key=os.environ["GROQ_API_KEY"])MODEL = "llama-3.3-70b-versatile"class CriterionScore(BaseModel):score: float = Field(ge=1.0, le=9.0)comment: strclass IELTSScore(BaseModel):task_response: CriterionScorecoherence_and_cohesion: CriterionScorelexical_resource: CriterionScoregrammatical_range_and_accuracy: CriterionScoreclass EvaluateRequest(BaseModel):topic: stressay: strPROMPT_TEMPLATE = """You are an expert IELTS Writing examiner with 15+ years of experience in high-stakes assessment. Evaluate the Task 2 essay below using the four official IELTS band descriptors.Read the essay four times — once per criterion — before scoring. This ensures each dimension is assessed independently and accurately.For each criterion, provide:1. A score from 1.0 to 9.02. A full, detailed comment (4-6 sentences) that covers:- What the candidate did well for this criterion- The most significant weakness, with a specific example quoted or paraphrased from the essay- How that weakness impacts the score- A concrete, actionable suggestion for improvementCriteria:1. Task Response (TR)Does the essay fully address all parts of the prompt? Is the position clear, consistent, and well-developed? Are ideas supported with relevant, specific examples? Is the word count appropriate (250+ words)?2. Coherence and Cohesion (CC)Are ideas logically organised with clear progression? Is there an effective introduction and conclusion? Are cohesive devices (linking words, pronouns, referencing) used accurately and with variety? Is paragraphing appropriate?3. Lexical Resource (LR)Is vocabulary varied and precise? Are collocations and topic-specific terms used correctly? Are there errors in spelling or word formation that affect communication?4. Grammatical Range and Accuracy (GRA)Is there a mix of simple, compound, and complex sentence structures? Are grammatical errors rare and non-impeding? Is punctuation accurate?Scoring Rules:- Be strict and accurate. Do NOT inflate scores.- Every criterion MUST receive a different score.- Never give all four criteria the same or nearly identical scores.- Base scores strictly on the essay content, not assumptions.Exam Topic:{topic}Candidate's Essay:{essay}"""def calibrate(s: float) -> float:return round(s * 2) / 224 unmodified linesif word_count < 50:return {"error": "Essay is too short to evaluate."}prompt = PROMPT_TEMPLATE.format(topic=req.topic, essay=req.essay) + "\nRespond with a JSON object only."try:response = client.chat.completions.create(model=MODEL,1 unmodified lineresponse_format={"type": "json_object"},temperature=0.2,)raw = json.loads(response.choices[0].message.content)key_map = {"task response": "task_response","coherence and cohesion": "coherence_and_cohesion","lexical resource": "lexical_resource","grammatical range and accuracy": "grammatical_range_and_accuracy",}normalized = {key_map.get(k.lower(), k.lower().replace(" ", "_")): v for k, v in raw.items()}result = IELTSScore.model_validate(normalized)except Exception as e:return {"error": f"Scoring failed: {e}"}tr = calibrate(result.task_response.score)cc = calibrate(result.coherence_and_cohesion.score)lr = calibrate(result.lexical_resource.score)gra = calibrate(result.grammatical_range_and_accuracy.score)overall = round((tr + cc + lr + gra) / 4 * 2) / 2penalty = 0.0if word_count < 250:6 unmodified lines"overall": overall,"overall_label": band_label(overall),"criteria": {"task_response": {"score": tr, "label": band_label(tr), "comment": result.task_response.comment},"coherence_and_cohesion": {"score": cc, "label": band_label(cc), "comment": result.coherence_and_cohesion.comment},"lexical_resource": {"score": lr, "label": band_label(lr), "comment": result.lexical_resource.comment},"grammatical_range_and_accuracy": {"score": gra, "label": band_label(gra), "comment": result.grammatical_range_and_accuracy.comment},}}if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=7860)8 unmodified lines910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210324 unmodified lines1281291301311321331341 unmodified line1361371381391401411421431441451461471481491501511521531541556 unmodified lines1621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052068 unmodified linesclient = Groq(api_key=os.environ["GROQ_API_KEY"])MODEL = "llama-3.3-70b-versatile"class TRScore(BaseModel):relevance_to_prompt: float = Field(ge=1.0, le=9.0)clarity_of_position: float = Field(ge=1.0, le=9.0)depth_of_ideas: float = Field(ge=1.0, le=9.0)appropriateness_of_format: float = Field(ge=1.0, le=9.0)relevant_and_specific_examples: float = Field(ge=1.0, le=9.0)appropriate_word_count: float = Field(ge=1.0, le=9.0)comment: strclass CCScore(BaseModel):logical_organization: float = Field(ge=1.0, le=9.0)effective_introduction_and_conclusion: float = Field(ge=1.0, le=9.0)supported_main_points: float = Field(ge=1.0, le=9.0)cohesive_devices_usage: float = Field(ge=1.0, le=9.0)paragraphing: float = Field(ge=1.0, le=9.0)comment: strclass GRAScore(BaseModel):sentence_structure_variety: float = Field(ge=1.0, le=9.0)grammar_accuracy: float = Field(ge=1.0, le=9.0)punctuation_usage: float = Field(ge=1.0, le=9.0)comment: strclass LRScore(BaseModel):vocabulary_range: float = Field(ge=1.0, le=9.0)lexical_accuracy: float = Field(ge=1.0, le=9.0)spelling_and_word_formation: float = Field(ge=1.0, le=9.0)comment: strclass IELTSScore(BaseModel):task_response: TRScorecoherence_and_cohesion: CCScoregrammatical_range_and_accuracy: GRAScorelexical_resource: LRScoreclass EvaluateRequest(BaseModel):topic: stressay: strPROMPT_TEMPLATE = """Task: ielts-writing-task-2.The task prompt:"{topic}"The candidate's essay:"{essay}"You are an expert IELTS Writing Task 2 examiner with 15+ years of experience. Evaluate the essay above using the four official IELTS band descriptors. Score each sub-criterion independently on a scale of 1.0 to 9.0, then provide a detailed examiner comment per criterion.Scoring rules:- Be strict and realistic. Do NOT inflate scores. A strong but imperfect essay is typically 6.5–7.5.- Sub-scores within a criterion may differ from each other.- Base all scores strictly on the essay content — no assumptions.- Word count below 250 is a penalty factor; reflect it in TR scores.Criteria and sub-scores to evaluate:1. task_response- relevance_to_prompt: Does the essay fully address all parts of the prompt?- clarity_of_position: Is the candidate's position clear and consistent throughout?- depth_of_ideas: Are ideas well-developed with sufficient explanation?- appropriateness_of_format: Is the essay format appropriate for Task 2 (intro, body, conclusion)?- relevant_and_specific_examples: Are examples relevant, specific, and well-integrated?- appropriate_word_count: Is the word count sufficient (250+ words)?- comment: 3–5 sentence examiner comment covering strengths, the most significant weakness with a specific quote or paraphrase from the essay, and a concrete improvement suggestion.2. coherence_and_cohesion- logical_organization: Are ideas logically sequenced with clear progression?- effective_introduction_and_conclusion: Are the introduction and conclusion effective and complete?- supported_main_points: Is each main point supported with explanation or evidence?- cohesive_devices_usage: Are linking words and referencing used accurately and with variety?- paragraphing: Is paragraphing logical and consistent?- comment: 3–5 sentence examiner comment (same format as above).3. grammatical_range_and_accuracy- sentence_structure_variety: Is there a mix of simple, compound, and complex structures?- grammar_accuracy: Are grammatical errors rare and non-impeding?- punctuation_usage: Is punctuation accurate throughout?- comment: 3–5 sentence examiner comment (same format as above).4. lexical_resource- vocabulary_range: Is vocabulary varied and topic-appropriate?- lexical_accuracy: Are words and collocations used correctly?- spelling_and_word_formation: Are spelling and word formation accurate?- comment: 3–5 sentence examiner comment (same format as above).Respond with a JSON object only."""def avg(*scores: float) -> float:return sum(scores) / len(scores)def calibrate(s: float) -> float:return round(s * 2) / 224 unmodified linesif word_count < 50:return {"error": "Essay is too short to evaluate."}prompt = PROMPT_TEMPLATE.format(topic=req.topic, essay=req.essay)try:response = client.chat.completions.create(model=MODEL,1 unmodified lineresponse_format={"type": "json_object"},temperature=0.2,)result = IELTSScore.model_validate_json(response.choices[0].message.content)except Exception as e:return {"error": f"Scoring failed: {e}"}tr = result.task_responsecc = result.coherence_and_cohesiongra = result.grammatical_range_and_accuracylr = result.lexical_resourcetr_score = calibrate(avg(tr.relevance_to_prompt, tr.clarity_of_position, tr.depth_of_ideas, tr.appropriateness_of_format, tr.relevant_and_specific_examples, tr.appropriate_word_count))cc_score = calibrate(avg(cc.logical_organization, cc.effective_introduction_and_conclusion, cc.supported_main_points, cc.cohesive_devices_usage, cc.paragraphing))gra_score = calibrate(avg(gra.sentence_structure_variety, gra.grammar_accuracy, gra.punctuation_usage))lr_score = calibrate(avg(lr.vocabulary_range, lr.lexical_accuracy, lr.spelling_and_word_formation))overall = round((tr_score + cc_score + gra_score + lr_score) / 4 * 2) / 2penalty = 0.0if word_count < 250:6 unmodified lines"overall": overall,"overall_label": band_label(overall),"criteria": {"task_response": {"score": tr_score, "label": band_label(tr_score), "comment": tr.comment,"sub_scores": {"relevance_to_prompt": tr.relevance_to_prompt,"clarity_of_position": tr.clarity_of_position,"depth_of_ideas": tr.depth_of_ideas,"appropriateness_of_format": tr.appropriateness_of_format,"relevant_and_specific_examples": tr.relevant_and_specific_examples,"appropriate_word_count": tr.appropriate_word_count,}},"coherence_and_cohesion": {"score": cc_score, "label": band_label(cc_score), "comment": cc.comment,"sub_scores": {"logical_organization": cc.logical_organization,"effective_introduction_and_conclusion": cc.effective_introduction_and_conclusion,"supported_main_points": cc.supported_main_points,"cohesive_devices_usage": cc.cohesive_devices_usage,"paragraphing": cc.paragraphing,}},"grammatical_range_and_accuracy": {"score": gra_score, "label": band_label(gra_score), "comment": gra.comment,"sub_scores": {"sentence_structure_variety": gra.sentence_structure_variety,"grammar_accuracy": gra.grammar_accuracy,"punctuation_usage": gra.punctuation_usage,}},"lexical_resource": {"score": lr_score, "label": band_label(lr_score), "comment": lr.comment,"sub_scores": {"vocabulary_range": lr.vocabulary_range,"lexical_accuracy": lr.lexical_accuracy,"spelling_and_word_formation": lr.spelling_and_word_formation,}},}}if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=7860)
app.py+131-64