Streamlit commited on
Commit ·
d3ae65d
1
Parent(s): 56e1a9b
app
Browse files- README.md +1 -0
- app.py +113 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -5,6 +5,7 @@ colorFrom: pink
|
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.49.1
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
short_description: Review Your Practice Quiz
|
|
|
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.49.1
|
| 8 |
+
python_version: 3.13
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
short_description: Review Your Practice Quiz
|
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
def login(username, password):
|
| 9 |
+
return (username=="admin" and password=="NRSG4604")
|
| 10 |
+
|
| 11 |
+
def build_system_prompt(params: dict) -> str:
|
| 12 |
+
question = params.get("question", "")
|
| 13 |
+
raw_choices = params.get("choices", "[]")
|
| 14 |
+
try:
|
| 15 |
+
choices = json.loads(raw_choices)
|
| 16 |
+
except json.JSONDecodeError:
|
| 17 |
+
choices = []
|
| 18 |
+
|
| 19 |
+
student_answer = params.get("student_answer", "")
|
| 20 |
+
correct_answer = params.get("correct_answer", "")
|
| 21 |
+
|
| 22 |
+
# You can tune this prompt however you like
|
| 23 |
+
lines = []
|
| 24 |
+
lines.append("You are a tutoring assistant helping a student review a Canvas quiz question.")
|
| 25 |
+
if question:
|
| 26 |
+
lines.append(f"\nQuestion:\n{question}")
|
| 27 |
+
if choices:
|
| 28 |
+
lines.append("\nChoices:")
|
| 29 |
+
for i, c in enumerate(choices):
|
| 30 |
+
label = chr(ord("A") + i)
|
| 31 |
+
lines.append(f"{label}. {c}")
|
| 32 |
+
if student_answer:
|
| 33 |
+
lines.append(f"\nStudent's answer: {student_answer}")
|
| 34 |
+
if correct_answer:
|
| 35 |
+
lines.append(f"Correct answer: {correct_answer}")
|
| 36 |
+
|
| 37 |
+
lines.append("\n\nWhen the student asks something, explain step-by-step why the correct answer is correct and, if relevant, why the student's answer is incorrect. Be supportive and focus on reasoning, not just telling them the answer.")
|
| 38 |
+
return "\n".join(lines)
|
| 39 |
+
|
| 40 |
+
def predict(message, messages, request: gr.Request):
|
| 41 |
+
if request is not None and hasattr(request, "query_params"):
|
| 42 |
+
query_params = dict(request.query_params)
|
| 43 |
+
else:
|
| 44 |
+
return messages
|
| 45 |
+
|
| 46 |
+
# 2. On the first turn, inject a system prompt built from the quiz data
|
| 47 |
+
if len(messages) == 0:
|
| 48 |
+
system_prompt = build_system_prompt(query_params)
|
| 49 |
+
messages.append({"role": "system", "content": system_prompt})
|
| 50 |
+
messages.append({"role": "user", "content": message})
|
| 51 |
+
params = {
|
| 52 |
+
"model": "gpt-5",
|
| 53 |
+
"messages": messages,
|
| 54 |
+
"stream": True,
|
| 55 |
+
"stream_options": {"include_usage": True},
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
response = client.chat.completions.create(**params)
|
| 59 |
+
content = ""
|
| 60 |
+
for event in response:
|
| 61 |
+
if event.choices and event.choices[0].delta.content:
|
| 62 |
+
chunk = event.choices[0].delta.content
|
| 63 |
+
content += chunk
|
| 64 |
+
yield content
|
| 65 |
+
|
| 66 |
+
messages.append(
|
| 67 |
+
{
|
| 68 |
+
"role": "assistant",
|
| 69 |
+
"content": content,
|
| 70 |
+
}
|
| 71 |
+
)
|
| 72 |
+
return messages
|
| 73 |
+
|
| 74 |
+
def vote(data: gr.LikeData):
|
| 75 |
+
if data.liked:
|
| 76 |
+
print("You upvoted this response: " + data.value["value"])
|
| 77 |
+
else:
|
| 78 |
+
print("You downvoted this response: " + data.value["value"])
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def show_question(request: gr.Request):
|
| 82 |
+
params = dict(request.query_params)
|
| 83 |
+
q = params.get("question", "")
|
| 84 |
+
return f"### Question\n\n{q}" if q else "No question data found in URL."
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
api_key=os.getenv('api')
|
| 88 |
+
client = OpenAI(api_key=api_key)
|
| 89 |
+
placeholder = """
|
| 90 |
+
<center><h1>Hello there!</h1><br>
|
| 91 |
+
How can I help you?
|
| 92 |
+
</center>
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
examples=["Can you explain why my answer is wrong?", "Can you explain this concept?"]
|
| 96 |
+
|
| 97 |
+
with gr.Blocks(title="Chat") as demo:
|
| 98 |
+
question_md = gr.Markdown()
|
| 99 |
+
chatbot=gr.Chatbot(
|
| 100 |
+
placeholder=placeholder,
|
| 101 |
+
type='messages',
|
| 102 |
+
)
|
| 103 |
+
#chatbot.like(vote, None, None)
|
| 104 |
+
chat = gr.ChatInterface(
|
| 105 |
+
predict,
|
| 106 |
+
chatbot=chatbot,
|
| 107 |
+
type="messages",
|
| 108 |
+
examples=examples,
|
| 109 |
+
cache_examples=False,
|
| 110 |
+
flagging_mode="manual"
|
| 111 |
+
)
|
| 112 |
+
demo.load(show_question, inputs=None, outputs=question_md)
|
| 113 |
+
demo.launch(auth=login, ssr_mode=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
dotenv
|