Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
+
from json_repair import repair_json
|
| 5 |
+
from json import loads
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
checkpoint = "IsmaelMousa/Qwen2.5-3B-Instruct-EngSaf-628K"
|
| 9 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
+
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(checkpoint)
|
| 13 |
+
assistant = pipeline("text-generation", tokenizer=tokenizer, model=model, device=device)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def extract(text):
|
| 17 |
+
start = text.find("{")
|
| 18 |
+
end = text.find("}", start)
|
| 19 |
+
|
| 20 |
+
if start == -1 or end == -1: return text
|
| 21 |
+
|
| 22 |
+
response = text[start:end + 1].strip()
|
| 23 |
+
response = repair_json(response)
|
| 24 |
+
|
| 25 |
+
try : return loads(s=response)
|
| 26 |
+
except: return response
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def grade(question, reference_answer, student_answer, mark_scheme):
|
| 30 |
+
system_content = "You are a grading assistant. Evaluate student answers based on the mark scheme. Respond only in JSON format with keys \"score\" (int) and \"rationale\" (string)."
|
| 31 |
+
|
| 32 |
+
user_content = ("Provide both a score and a rationale by evaluating the student's answer strictly within the mark scheme range, "
|
| 33 |
+
"grading based on how well it meets the question's requirements by comparing the student answer to the reference answer.\n"
|
| 34 |
+
f"Question: {question}\n"
|
| 35 |
+
f"Reference Answer: {reference_answer}\n"
|
| 36 |
+
f"Student Answer: {student_answer}\n"
|
| 37 |
+
f"Mark Scheme: {mark_scheme}")
|
| 38 |
+
|
| 39 |
+
messages = [{"role": "system", "content": system_content}, {"role": "user", "content": user_content}]
|
| 40 |
+
|
| 41 |
+
inputs = tokenizer.apply_chat_template(messages, tokenize=False)
|
| 42 |
+
|
| 43 |
+
output = assistant(inputs, max_new_tokens=128, do_sample=False, return_full_text=False)[0]["generated_text"]
|
| 44 |
+
parsed = extract(output)
|
| 45 |
+
|
| 46 |
+
return parsed
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
demo = gr.Interface(fn =grade,
|
| 50 |
+
inputs=[gr.Textbox(label="Question"),
|
| 51 |
+
gr.Textbox(label="Reference Answer"),
|
| 52 |
+
gr.Textbox(label="Student Answer"),
|
| 53 |
+
gr.Textbox(label="Mark Scheme")],
|
| 54 |
+
outputs=gr.JSON (label="Evaluation Output"))
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__": demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|