Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,3 @@
|
|
| 1 |
-
# import os
|
| 2 |
-
# import json
|
| 3 |
-
# import re
|
| 4 |
-
# import torch
|
| 5 |
-
# import gradio as gr
|
| 6 |
-
# import google.generativeai as genai
|
| 7 |
-
# from sentence_transformers import SentenceTransformer, util
|
| 8 |
-
|
| 9 |
-
# # ============================================================
|
| 10 |
-
# # CONFIG
|
| 11 |
-
# # ============================================================
|
| 12 |
-
# GEMINI_API_KEY = "AIzaSyBrbLGXkSdXReb0lUucYqcNCNBkvS-RBFw"
|
| 13 |
-
# genai.configure(api_key=GEMINI_API_KEY)
|
| 14 |
-
|
| 15 |
-
# # UPDATED: Use a supported 2026 model
|
| 16 |
-
# MODEL = genai.GenerativeModel("gemini-2.5-flash")
|
| 17 |
-
|
| 18 |
-
# DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
-
# EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 20 |
-
# SIM_THRESHOLD = 0.55
|
| 21 |
-
|
| 22 |
-
# print("Loading embedding model...")
|
| 23 |
-
# embedder = SentenceTransformer(EMBED_MODEL, device=DEVICE)
|
| 24 |
-
# print("✅ Ready")
|
| 25 |
-
|
| 26 |
-
# # ============================================================
|
| 27 |
-
# # LOGIC
|
| 28 |
-
# # ============================================================
|
| 29 |
-
# def get_evaluation_data(kb, question):
|
| 30 |
-
# """Gets both intent and rubric in one single API request."""
|
| 31 |
-
# prompt = f"""
|
| 32 |
-
# Acting as an examiner, analyze the Knowledge Base (KB) and Question.
|
| 33 |
-
# 1. Determine the intent (FACTUAL, EXPLANATORY, PROCESS, or COMPARISON).
|
| 34 |
-
# 2. Create a rubric of 3-5 atomic grading criteria based ONLY on the KB.
|
| 35 |
-
|
| 36 |
-
# KB: {kb}
|
| 37 |
-
# Question: {question}
|
| 38 |
-
|
| 39 |
-
# OUTPUT JSON ONLY:
|
| 40 |
-
# {{
|
| 41 |
-
# "intent": "LABEL",
|
| 42 |
-
# "criteria": ["criterion 1", "criterion 2"]
|
| 43 |
-
# }}
|
| 44 |
-
# """
|
| 45 |
-
# try:
|
| 46 |
-
# response = MODEL.generate_content(prompt)
|
| 47 |
-
# # Handle cases where model might wrap JSON in backticks
|
| 48 |
-
# clean_text = re.sub(r'```json|```', '', response.text).strip()
|
| 49 |
-
# return json.loads(clean_text)
|
| 50 |
-
# except Exception as e:
|
| 51 |
-
# print(f"API Error: {e}")
|
| 52 |
-
# return {"intent": "ERROR", "criteria": []}
|
| 53 |
-
|
| 54 |
-
# def evaluate(answer, question, kb):
|
| 55 |
-
# # API Call
|
| 56 |
-
# data = get_evaluation_data(kb, question)
|
| 57 |
-
# rubric = data.get("criteria", [])
|
| 58 |
-
|
| 59 |
-
# if not rubric:
|
| 60 |
-
# return {"error": "Could not generate rubric. Check API status."}
|
| 61 |
-
|
| 62 |
-
# # Semantic Matching (Local)
|
| 63 |
-
# sents = [s.strip() for s in re.split(r'(?<=[.!?])\s+', answer) if len(s.strip()) > 5]
|
| 64 |
-
# if not sents:
|
| 65 |
-
# return {"error": "Answer is too short to evaluate."}
|
| 66 |
-
|
| 67 |
-
# ans_emb = embedder.encode(sents, convert_to_tensor=True)
|
| 68 |
-
# results = []
|
| 69 |
-
# for crit in rubric:
|
| 70 |
-
# crit_emb = embedder.encode(crit, convert_to_tensor=True)
|
| 71 |
-
# sims = util.cos_sim(crit_emb, ans_emb)[0]
|
| 72 |
-
# score = float(torch.max(sims)) if sims.numel() else 0.0
|
| 73 |
-
# results.append({"criterion": crit, "satisfied": score >= SIM_THRESHOLD})
|
| 74 |
-
|
| 75 |
-
# # Verdict
|
| 76 |
-
# hits = sum(r["satisfied"] for r in results)
|
| 77 |
-
# verdict = "✅ CORRECT" if hits == len(results) else "⚠️ PARTIAL" if hits > 0 else "❌ INCORRECT"
|
| 78 |
-
|
| 79 |
-
# return {
|
| 80 |
-
# "intent": data.get("intent"),
|
| 81 |
-
# "rubric_results": results,
|
| 82 |
-
# "final_verdict": verdict
|
| 83 |
-
# }
|
| 84 |
-
|
| 85 |
-
# # UI
|
| 86 |
-
# with gr.Blocks() as demo:
|
| 87 |
-
# gr.Markdown("## 🧠 Gemini 2.5 Answer Grader")
|
| 88 |
-
# kb_input = gr.Textbox(label="Knowledge Base", lines=5)
|
| 89 |
-
# q_input = gr.Textbox(label="Question")
|
| 90 |
-
# a_input = gr.Textbox(label="Student Answer", lines=4)
|
| 91 |
-
# out = gr.JSON(label="Evaluation Result")
|
| 92 |
-
# gr.Button("Evaluate").click(evaluate, [a_input, q_input, kb_input], out)
|
| 93 |
-
|
| 94 |
-
# demo.launch()
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
|
| 113 |
import os
|
|
@@ -156,6 +46,12 @@ def get_advanced_evaluation(kb, question, answer):
|
|
| 156 |
Question: {question}
|
| 157 |
Student Answer: {answer}
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
STRICT JSON OUTPUT ONLY:
|
| 160 |
{{
|
| 161 |
"intent": "...",
|
|
@@ -163,7 +59,7 @@ def get_advanced_evaluation(kb, question, answer):
|
|
| 163 |
{{"criterion": "...", "satisfied": true, "confidence": 95}}
|
| 164 |
],
|
| 165 |
"irrelevant_snippets": ["...", "..."],
|
| 166 |
-
"contradictions": ["...", "
|
| 167 |
"suggested_mark": 85,
|
| 168 |
"feedback": "..."
|
| 169 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
|
| 3 |
import os
|
|
|
|
| 46 |
Question: {question}
|
| 47 |
Student Answer: {answer}
|
| 48 |
|
| 49 |
+
|
| 50 |
+
but if Knowledge Base is empty then literally just evaluate the answer with respect the question and apply the same ideas we discussed before by generating a KB with respect the queston yourself.
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
STRICT JSON OUTPUT ONLY:
|
| 56 |
{{
|
| 57 |
"intent": "...",
|
|
|
|
| 59 |
{{"criterion": "...", "satisfied": true, "confidence": 95}}
|
| 60 |
],
|
| 61 |
"irrelevant_snippets": ["...", "..."],
|
| 62 |
+
"contradictions": [{{"snippet": "...", "start": <the character where that snippet starts in user's answer> , "end": <the character where that snippet ends in user's answer>, "reason": "....."}, ....}] ,
|
| 63 |
"suggested_mark": 85,
|
| 64 |
"feedback": "..."
|
| 65 |
}}
|