import gradio as gr from huggingface_hub import InferenceClient client = InferenceClient("Qwen/Qwen2.5-Coder-7B-Instruct") def respond(message, history): messages = [] for msg in history: messages.append({"role": msg["role"], "content": msg["content"]}) messages.append({"role": "user", "content": message}) response = "" try: for message_chunk in client.chat_completion( messages, max_tokens=2048, stream=True, temperature=0.7, top_p=0.9 ): token = message_chunk.choices[0].delta.content if token: response += token yield response except Exception as e: yield f"Error calling inference API: {str(e)}" # Create Gradio Chat Interface (using Gradio 5 messages format) demo = gr.ChatInterface( respond, type="messages", title="Qwen 2.5 Coder Assistant", description="Ask Qwen 2.5 Coder anything! It runs on the free, fast serverless Hugging Face Inference API.", examples=[ ["Write a Python function to check if a number is prime."], ["Explain the difference between REST and GraphQL in simple terms."], ["How do I write a Dockerfile for a Next.js application?"] ] ) if __name__ == "__main__": demo.launch(mcp_server=True)