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()