Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
# import random
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
client = InferenceClient("microsoft/phi-4")
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def respond(message, history):
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
if history:
|
| 11 |
messages.extend(history)
|
| 12 |
|
| 13 |
messages.append({"role": "user", "content": message})
|
| 14 |
|
| 15 |
-
|
| 16 |
response = client.chat_completion(
|
| 17 |
messages,
|
| 18 |
max_tokens=300
|
| 19 |
)
|
| 20 |
return response['choices'][0]['message']['content'].strip()
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
chatbot.launch()
|
| 26 |
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
client = InferenceClient("microsoft/phi-4")
|
| 5 |
|
| 6 |
+
# Topics
|
| 7 |
+
BIO_TOPICS = [
|
| 8 |
+
"Cell Biology",
|
| 9 |
+
"Organisation",
|
| 10 |
+
"Infection and Response",
|
| 11 |
+
"Bioenergetics",
|
| 12 |
+
"Homeostasis and Response",
|
| 13 |
+
"Inheritance, Variation and Evolution",
|
| 14 |
+
"Ecology"
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
chosen_topic = None
|
| 18 |
+
|
| 19 |
+
def set_topic(topic):
|
| 20 |
+
global chosen_topic
|
| 21 |
+
chosen_topic = topic
|
| 22 |
+
return f"✅ Great! You've chosen **{topic}**. Let's start your study session."
|
| 23 |
+
|
| 24 |
def respond(message, history):
|
| 25 |
+
global chosen_topic
|
| 26 |
+
if not chosen_topic:
|
| 27 |
+
return "⚠️ Please choose a topic before starting."
|
| 28 |
+
|
| 29 |
+
messages = [{
|
| 30 |
+
"role": "system",
|
| 31 |
+
"content": f"You are a friendly GCSE Biology tutor focusing on **{chosen_topic}**."
|
| 32 |
+
}]
|
| 33 |
|
| 34 |
if history:
|
| 35 |
messages.extend(history)
|
| 36 |
|
| 37 |
messages.append({"role": "user", "content": message})
|
| 38 |
|
|
|
|
| 39 |
response = client.chat_completion(
|
| 40 |
messages,
|
| 41 |
max_tokens=300
|
| 42 |
)
|
| 43 |
return response['choices'][0]['message']['content'].strip()
|
| 44 |
|
| 45 |
+
# Create the Gradio interface
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("# ACE it! 📚 — GCSE Biology Tutor")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
topic_dropdown = gr.Dropdown(choices=BIO_TOPICS, label="Choose a Biology Topic")
|
| 51 |
+
topic_button = gr.Button("Confirm Topic")
|
| 52 |
+
|
| 53 |
+
topic_output = gr.Markdown()
|
| 54 |
+
|
| 55 |
+
chatbot = gr.ChatInterface(respond, type="messages", title="ACE it!")
|
| 56 |
+
|
| 57 |
+
topic_button.click(set_topic, inputs=topic_dropdown, outputs=topic_output)
|
| 58 |
|
| 59 |
+
demo.launch()
|
|
|
|
| 60 |
|