import gradio as gr from huggingface_hub import InferenceClient client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct") def respond(message, history, system_message, max_tokens, temperature, top_p): messages = [{"role": "system", "content": system_message}] for user_msg, assistant_msg in history: messages.extend([ {"role": "user", "content": user_msg}, {"role": "assistant", "content": assistant_msg} ]) messages.append({"role": "user", "content": message}) full_response = "" for chunk in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): token = chunk.choices[0].delta.content or "" full_response += token yield full_response custom_css = """ :root { --primary: #2563eb; --primary-dark: #1d4ed8; --dark: #1e293b; --light: #f8fafc; --gray: #94a3b8; --success: #10b981; } body { font-family: 'Inter', sans-serif; background-color: #f1f5f9 !important; } .message-user { background: white !important; border-left: 4px solid var(--primary) !important; } .message-assistant { background: white !important; border-left: 4px solid var(--success) !important; } .code-block { background-color: #0f172a !important; color: #e2e8f0 !important; font-family: 'Fira Code', monospace !important; padding: 1rem !important; border-radius: 0.5rem !important; margin: 0.5rem 0 !important; } .settings-panel { background: #f8fafc !important; padding: 1rem !important; border-radius: 0.5rem !important; margin-bottom: 1rem !important; } .avatar { width: 32px !important; height: 32px !important; border-radius: 50% !important; display: flex !important; align-items: center !important; justify-content: center !important; } .avatar-user { background: var(--primary) !important; } .avatar-bot { background: var(--success) !important; } """ with gr.Blocks(css=custom_css, title="Qwen2.5 Coder - AI Assistant") as demo: # Header gr.Markdown("""
32B Instruct
Qwen2.5 Coder may produce inaccurate information about people, places, or facts.
""") # Event Handlers msg.submit( respond, [msg, chatbot, system_msg, max_tokens, temperature, top_p], [chatbot, msg], api_name="respond" ).then(lambda: None, None, msg, queue=False) submit_btn.click( respond, [msg, chatbot, system_msg, max_tokens, temperature, top_p], [chatbot, msg], api_name="respond" ) if __name__ == "__main__": demo.launch()