Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 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 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="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).", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
checkpoint = "IsmaelMousa/SmolLM2-135M-Instruct-EngSaf-217K"
|
| 6 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
|
| 10 |
+
assistant = pipeline("text-generation", tokenizer=tokenizer, model=model, device=0 if torch.cuda.is_available() else -1)
|
| 11 |
+
|
| 12 |
+
def grade(question, reference_answer, student_answer, mark_scheme, max_tokens, temperature, top_p):
|
| 13 |
+
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)."
|
| 14 |
+
|
| 15 |
+
user_content = (
|
| 16 |
+
"Provide both a score and a rationale by evaluating the student's answer strictly within the mark scheme range, "
|
| 17 |
+
"grading based on how well it meets the question's requirements by comparing the student answer to the reference answer.\n"
|
| 18 |
+
f"Question: {question}\n"
|
| 19 |
+
f"Reference Answer: {reference_answer}\n"
|
| 20 |
+
f"Student Answer: {student_answer}\n"
|
| 21 |
+
f"Mark Scheme: {mark_scheme}"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
messages = [{"role": "system", "content": system_content}, {"role": "user", "content": user_content}]
|
| 25 |
+
inputs = tokenizer.apply_chat_template(messages, tokenize=False)
|
| 26 |
+
|
| 27 |
+
output = assistant(inputs, max_new_tokens=max_tokens, do_sample=True, temperature=temperature, top_p=top_p, return_full_text=False)[0]["generated_text"]
|
| 28 |
+
|
| 29 |
+
return output
|
| 30 |
+
|
| 31 |
+
demo = gr.Interface(
|
| 32 |
+
fn=grade,
|
| 33 |
+
inputs=[
|
| 34 |
+
gr.Textbox(label="Question"),
|
| 35 |
+
gr.Textbox(label="Reference Answer"),
|
| 36 |
+
gr.Textbox(label="Student Answer"),
|
| 37 |
+
gr.Textbox(label="Mark Scheme"),
|
| 38 |
+
gr.Slider(minimum=1, maximum=512, value=128, step=1, label="Max new tokens"),
|
| 39 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 40 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
],
|
| 42 |
+
outputs=gr.Textbox(label="Evaluation Output (JSON)")
|
| 43 |
)
|
| 44 |
|
|
|
|
| 45 |
if __name__ == "__main__":
|
| 46 |
demo.launch()
|