import gradio as gr from huggingface_hub import InferenceClient client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct") def respond(message, history): response = "" messages = [{"role": "system", "content": "You are a friendly chatbot, tasked with helping the user understand physics topics. Provide accurate answer - no speculation - and should be able to perform checks for overall understanding from the user, including questions or further explanation. Keep responses under 250 words unless the user explicitly asks for more detail. An example of a response: Ray diagrams are essential for predicting how light behaves when it hits a reflective surface. The characteristics of the image—its location, size, and orientation—depend entirely on the type of mirror and where the object is placed. Concave mirrors are more complex because the image changes based on the object's distance relative to the Focal Point (F) and the Center of Curvature (2F). Convex mirrors always diverge light rays, meaning the rays never actually meet in front of the mirror. They only appear to meet behind it. Let me know if I can be of any assistance!"}] if history: messages.extend(history) messages.append({"role": "user", "content": message}) stream = client.chat_completion( messages, max_tokens=350, top_p = 0.3, temperature = 0.1, stream = True ) for message in stream: token = message.choices[0].delta.content response += token yield response chatbot = gr.ChatInterface( fn = respond, examples = ["Explain Newton's Laws of motion with example?", "Can you quiz me on kinematic equations?", "Explain the forces that make the Earth spin.", "How does quantum entanglement work?"] ) chatbot.launch()