import gradio as gr from transformers import pipeline # Load a small instruction-following model generator = pipeline( "text-generation", model="Qwen/Qwen2.5-0.5B-Instruct", device_map="auto" ) def chat(message, history): # Convert Gradio history into the format expected by the model messages = [] for user_msg, assistant_msg in history: messages.append({"role": "user", "content": user_msg}) messages.append({"role": "assistant", "content": assistant_msg}) messages.append({"role": "user", "content": message}) # Generate response output = generator( messages, max_new_tokens=300, temperature=0.7, do_sample=True, top_p=0.9 ) # Get the generated assistant response response = output[0]["generated_text"][-1]["content"] return response # Custom ChatGPT-like interface with gr.Blocks( title="My AI Assistant", theme=gr.themes.Soft() ) as demo: gr.Markdown( """ # 🤖 My AI Assistant ### Chat with my own Hugging Face AI model """ ) chatbot = gr.Chatbot( height=550, label="AI Chat", bubble_full_width=False ) msg = gr.Textbox( placeholder="Message your AI assistant...", label="", lines=2 ) with gr.Row(): send = gr.Button("➤ Send", variant="primary") clear = gr.Button("🗑️ Clear") # Send message send.click( chat, inputs=[msg, chatbot], outputs=chatbot ).then( lambda: "", outputs=msg ) # Press Enter to send msg.submit( chat, inputs=[msg, chatbot], outputs=chatbot ).then( lambda: "", outputs=msg ) # Clear conversation clear.click( lambda: [], outputs=chatbot ) demo.launch()