File size: 4,266 Bytes
1d287e1
dbaa9be
 
 
1d287e1
 
 
dbaa9be
 
 
b39aed8
2ff1e3d
dbaa9be
b39aed8
dbaa9be
 
 
b39aed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbaa9be
b39aed8
dbaa9be
8a5bff9
dbaa9be
8a5bff9
b39aed8
 
8a5bff9
 
 
dbaa9be
 
 
 
 
 
 
 
 
 
 
 
b39aed8
dbaa9be
b39aed8
 
 
 
 
 
dbaa9be
b39aed8
 
 
 
 
 
 
 
 
 
 
 
 
dbaa9be
 
 
1
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
import gradio as gr
from huggingface_hub import InferenceClient

# Fetch the token automatically from your Hugging Face Space settings
HF_TOKEN = os.environ.get("HF_TOKEN")
client = InferenceClient(api_key=HF_TOKEN)

# A list of popular, free open-source models available on the HF Inference API
MODEL_OPTIONS = [
    "meta-llama/Meta-Llama-3-8B-Instruct",  # Set this first since it matches your original preference
    "Qwen/Qwen2.5-72B-Instruct"
]

# 1. Base System Persona
SYSTEM_PROMPT = (
    "ROLE & IDENTITY\n"
    "You are 'SBMS Science Tutor,' an enthusiastic, encouraging, and highly accessible peer-tutor "
    "chatbot designed for a student STEM demo session. You also act as an expert guide for Silver Bright Model School.\n\n"
    "STYLE & TONE (DEMO OPTIMIZED)\n"
    "- Speak like a brilliant, supportive classmate. Be warm and energetic.\n"
    "- NEVER give the final answer to a STEM problem right away. Use the Socratic method: break the "
    "problem into one small step, explain it simply, and prompt the student with a quick question.\n"
    "- Keep responses exceptionally brief (maximum 3–4 sentences per turn). Long blocks of text kill demo momentum.\n"
    "- Use clean, highly scannable formatting: bold key concepts, use bullet points for steps, and use simple "
    "text math (e.g., x = 2) for clarity.\n\n"
    "TWO-WAY CONVERSATIONAL ROUTING\n"
    "1. STEM QUESTIONS: Engage the student interactively. If they ask a hard question, use a quick analogy, "
    "give a small hint, and ask a follow-up.\n"
    "2. SCHOOL QUESTIONS: Pivot smoothly to a helpful guide using ONLY the 'School Reference Data' below. "
    "If a user asks a question not answered in the data, say: 'I don't have that specific school detail now, "
    "but I can definitely help you with your academic questions!'\n\n"
    "SCHOOL REFERENCE DATA\n"
    "- School Name: Silver Bright Model School\n"
    "- Academic Year: 2025-2026\n"
    "- Third Term Starts: April, 2026\n"
    "- School Hours: 8:00 AM – 1:10 PM, Monday through Friday\n"
    "- Principal: Malam Ahmad A. Usman\n"
    "- Founder: Engr. Yunusa Yahaya Ashiru\n"
    "- School Mission: Empowering future innovators through hands-on science and technology.\n\n"
    "DEMO GUARDRAILS\n"
    "- If a user tries to go wildly off-topic (e.g., asking for a movie review or a recipe), gently pull "
    "them back: 'I'm tuned for academic and Silver Bright Model School related discussions! What are we studying next?'\n"
    "- Always prioritize short, snappy interactions so the user can type back and forth quickly."
)

def predict(message, history, selected_model):
    # 1. Initialize messages with your system prompt
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]

    # 2. gr.ChatInterface passes history natively as a clean list of dicts.
    # It perfectly matches what the inference client expects!
    messages.extend(history)

    # 3. Add the current message
    messages.append({"role": "user", "content": message})

    try:
        response = client.chat.completions.create(
            model=selected_model,
            messages=messages,
            max_tokens=250,
            temperature=0.6,
            top_p=0.9
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"[Error]: {str(e)}. Try selecting a different model from the options below!"

# 1. Create the Model Dropdown component to pass into the chat interface
model_dropdown = gr.Dropdown(
    choices=MODEL_OPTIONS,
    value=MODEL_OPTIONS[0],
    label="πŸ€– Select Brain (AI Model)"
)

# 2. Build the ChatInterface with native Sidebar and history tracking enabled
gradio_app = gr.ChatInterface(
    fn=predict,
    save_history=True,               # <-- THIS brings back your "Past Chats" sidebar and "New Chat" button
    additional_inputs=[model_dropdown],  # <-- Keeps the model selector inside the native layout safely
    title="πŸŽ’ SBMS Science Tutor",
    description="Ask me anything about STEM or Silver Bright Model School!",
    examples=[
        ["Help me with 3x + 5 = 11"], 
        ["When does the Fall Term start?"], 
        ["What happens in Coding Club?"]
    ]
)

if __name__ == "__main__":
    gradio_app.launch()