| import gradio as gr | |
| from transformers import pipeline | |
| def respond(message, history): | |
| """Process user message with conversation history""" | |
| # Format conversation history | |
| prompt = "" | |
| if history: | |
| for human_msg, bot_msg in history: | |
| prompt += f"Human: {human_msg}\nAssistant: {bot_msg}\n" | |
| # Add current message | |
| prompt += f"Human: {message}\nAssistant:" | |
| # Initialize chatbot | |
| chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium") | |
| # Generate response | |
| response = chatbot( | |
| prompt, | |
| max_length=200, | |
| temperature=0.7, | |
| top_p=0.9, | |
| repetition_penalty=1.1, | |
| do_sample=True, | |
| pad_token_id=50256 | |
| ) | |
| # Extract only the assistant's response | |
| full_text = response[0]['generated_text'] | |
| assistant_response = full_text.split("Assistant:")[-1].strip() | |
| return assistant_response | |
| # Create Gradio interface | |
| demo = gr.ChatInterface( | |
| fn=respond, | |
| title="?? SimpleAI Chatbot", | |
| description="A conversational AI powered by DialoGPT", | |
| examples=["Hello!", "What's AI?", "Tell me a joke"], | |
| theme="soft", | |
| retry_btn=None, | |
| undo_btn=None, | |
| clear_btn="Clear Chat" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |