Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
| 6 |
+
|
| 7 |
+
SYSTEM_PROMPT = """
|
| 8 |
+
You are StudyAI, a friendly tutor for secondary school students.
|
| 9 |
+
|
| 10 |
+
Rules:
|
| 11 |
+
- Explain things clearly and step by step
|
| 12 |
+
- Help the student understand instead of only giving answers
|
| 13 |
+
- Give examples when useful
|
| 14 |
+
- For maths, show working
|
| 15 |
+
- Keep explanations clear and not unnecessarily long
|
| 16 |
+
- For quizzes, do not reveal answers immediately
|
| 17 |
+
- If unsure about something, say so instead of inventing information
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
def study_ai(message, history, subject, mode):
|
| 21 |
+
if not message.strip():
|
| 22 |
+
return "Type something first!"
|
| 23 |
+
|
| 24 |
+
instructions = {
|
| 25 |
+
"Ask anything":
|
| 26 |
+
f"Answer the student's question about {subject}.",
|
| 27 |
+
|
| 28 |
+
"Explain":
|
| 29 |
+
f"""Explain this {subject} topic:
|
| 30 |
+
{message}
|
| 31 |
+
|
| 32 |
+
Include:
|
| 33 |
+
1. Simple explanation
|
| 34 |
+
2. Important points
|
| 35 |
+
3. An example
|
| 36 |
+
4. Short recap""",
|
| 37 |
+
|
| 38 |
+
"Quiz me":
|
| 39 |
+
f"""Create a 5-question quiz about this {subject} topic:
|
| 40 |
+
{message}
|
| 41 |
+
|
| 42 |
+
Do not give the answers yet.""",
|
| 43 |
+
|
| 44 |
+
"Flashcards":
|
| 45 |
+
f"""Create 10 flashcards about this {subject} topic:
|
| 46 |
+
{message}
|
| 47 |
+
|
| 48 |
+
Format each as:
|
| 49 |
+
Q: ...
|
| 50 |
+
A: ...""",
|
| 51 |
+
|
| 52 |
+
"Study plan":
|
| 53 |
+
f"""Create a realistic study plan based on:
|
| 54 |
+
{message}
|
| 55 |
+
|
| 56 |
+
Include revision, practice questions, active recall and breaks.""",
|
| 57 |
+
|
| 58 |
+
"Check my answer":
|
| 59 |
+
f"""Check the student's {subject} answer:
|
| 60 |
+
{message}
|
| 61 |
+
|
| 62 |
+
Explain:
|
| 63 |
+
1. What is correct
|
| 64 |
+
2. What needs improvement
|
| 65 |
+
3. A better answer or method
|
| 66 |
+
4. One revision tip"""
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
prompt = instructions.get(mode, message)
|
| 70 |
+
|
| 71 |
+
if mode == "Ask anything":
|
| 72 |
+
prompt += f"\n\nStudent question:\n{message}"
|
| 73 |
+
|
| 74 |
+
response = client.responses.create(
|
| 75 |
+
model="gpt-5.4-mini",
|
| 76 |
+
instructions=SYSTEM_PROMPT,
|
| 77 |
+
input=prompt
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
return response.output_text
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
with gr.Blocks(title="FYR StudyAI") as demo:
|
| 84 |
+
|
| 85 |
+
gr.Markdown(
|
| 86 |
+
"""
|
| 87 |
+
# ๐ FYR StudyAI
|
| 88 |
+
### Your AI study assistant
|
| 89 |
+
Ask questions, revise topics, generate quizzes and make flashcards.
|
| 90 |
+
"""
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
with gr.Row():
|
| 94 |
+
subject = gr.Dropdown(
|
| 95 |
+
[
|
| 96 |
+
"General",
|
| 97 |
+
"Mathematics",
|
| 98 |
+
"Science",
|
| 99 |
+
"English",
|
| 100 |
+
"Chinese",
|
| 101 |
+
"History",
|
| 102 |
+
"Geography"
|
| 103 |
+
],
|
| 104 |
+
value="General",
|
| 105 |
+
label="๐ Subject"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
mode = gr.Dropdown(
|
| 109 |
+
[
|
| 110 |
+
"Ask anything",
|
| 111 |
+
"Explain",
|
| 112 |
+
"Quiz me",
|
| 113 |
+
"Flashcards",
|
| 114 |
+
"Study plan",
|
| 115 |
+
"Check my answer"
|
| 116 |
+
],
|
| 117 |
+
value="Ask anything",
|
| 118 |
+
label="๐ ๏ธ Study Tool"
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
chatbot = gr.ChatInterface(
|
| 122 |
+
fn=study_ai,
|
| 123 |
+
additional_inputs=[subject, mode],
|
| 124 |
+
textbox=gr.Textbox(
|
| 125 |
+
placeholder="Ask StudyAI something...",
|
| 126 |
+
lines=2
|
| 127 |
+
)
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
gr.Markdown(
|
| 131 |
+
"โ ๏ธ StudyAI can make mistakes. Check important information with your notes or teacher."
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
demo.launch()
|