Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import random | |
| import time | |
| from typing import Generator, Tuple | |
| def get_response(message: str, history: list) -> Generator[str, None, None]: | |
| """ | |
| Generate a response for the chatbot. | |
| Args: | |
| message: The user's input message | |
| history: The conversation history | |
| Yields: | |
| Stream of response text for a typing effect | |
| """ | |
| # Simple mock responses - in a real app, this would call an LLM API | |
| responses = [ | |
| "That's an interesting question! Let me think about that...", | |
| "I understand what you're saying. Here's my perspective:", | |
| "Great point! I'd like to add that...", | |
| "Hmm, that's something worth considering. My thoughts are:", | |
| "Thank you for sharing! From my understanding:", | |
| ] | |
| # Simple contextual responses | |
| if "hello" in message.lower() or "hi" in message.lower(): | |
| response = "Hello! How can I help you today?" | |
| elif "how are you" in message.lower(): | |
| response = "I'm doing great, thanks for asking! I'm here to assist you with any questions or conversations you'd like to have." | |
| elif "bye" in message.lower() or "goodbye" in message.lower(): | |
| response = "Goodbye! It was nice chatting with you. Feel free to come back anytime!" | |
| elif "help" in message.lower(): | |
| response = "I'm here to help! You can ask me questions, have conversations, or just chat about anything on your mind. What would you like to discuss?" | |
| else: | |
| # Generate a semi-random contextual response | |
| base_response = random.choice(responses) | |
| additional = [ | |
| " based on what you've shared, I believe the key is to consider multiple perspectives and find a balanced approach.", | |
| " it's important to think about both the short-term and long-term implications.", | |
| " there are often many valid viewpoints to consider in such situations.", | |
| " the answer might depend on various factors and context.", | |
| " this is a complex topic with many interesting dimensions to explore.", | |
| ] | |
| response = base_response + random.choice(additional) | |
| # Simulate typing effect | |
| partial_response = "" | |
| for word in response.split(): | |
| partial_response += word + " " | |
| yield partial_response | |
| time.sleep(0.05) | |
| def format_examples() -> list: | |
| """ | |
| Create example prompts for the chatbot. | |
| Returns: | |
| List of example messages | |
| """ | |
| return [ | |
| "Hello! Tell me about yourself.", | |
| "What's your opinion on artificial intelligence?", | |
| "Can you help me understand machine learning?", | |
| "How do you think technology will change the future?", | |
| "Tell me an interesting fact!", | |
| ] | |
| def create_chat_interface() -> gr.Blocks: | |
| """ | |
| Create and configure the Gradio chatbot interface. | |
| Returns: | |
| Configured Gradio Blocks interface | |
| """ | |
| with gr.Blocks( | |
| title="AI Chatbot", | |
| theme=gr.themes.Soft( | |
| primary_hue="blue", | |
| secondary_hue="gray", | |
| neutral_hue="slate" | |
| ), | |
| css=""" | |
| .chatbot-container { | |
| height: 600px !important; | |
| } | |
| .message-wrap { | |
| padding: 10px !important; | |
| } | |
| """ | |
| ) as interface: | |
| # Header | |
| with gr.Row(): | |
| gr.HTML(""" | |
| <div style="text-align: center; margin-bottom: 20px;"> | |
| <h1>🤖 AI Chatbot</h1> | |
| <p style="color: #666;">Your intelligent conversational assistant</p> | |
| <p style="font-size: 0.9em; color: #999;"> | |
| Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #1f77b4; text-decoration: none;">anycoder</a> | |
| </p> | |
| </div> | |
| """) | |
| # Chat interface | |
| with gr.Row(): | |
| with gr.Column(scale=4): | |
| chatbot = gr.ChatInterface( | |
| fn=get_response, | |
| title="", | |
| examples=format_examples(), | |
| cache_examples=False, | |
| chatbot=gr.Chatbot( | |
| height=600, | |
| show_copy_button=True, | |
| avatar_images=("👤", "🤖"), | |
| bubble_full_width=False, | |
| type="messages" | |
| ), | |
| textbox=gr.Textbox( | |
| placeholder="Type your message here...", | |
| container=False, | |
| scale=7, | |
| autofocus=True | |
| ), | |
| submit_btn="Send", | |
| stop_btn="Stop", | |
| retry_btn="🔄 Retry", | |
| undo_btn="↩️ Undo", | |
| clear_btn="🗑️ Clear" | |
| ) | |
| with gr.Column(scale=1): | |
| # Settings panel | |
| gr.Markdown("### ⚙️ Settings") | |
| with gr.Accordion("Chat Options", open=True): | |
| temperature = gr.Slider( | |
| minimum=0.1, | |
| maximum=2.0, | |
| value=0.7, | |
| step=0.1, | |
| label="Response Creativity", | |
| info="Higher values make responses more creative" | |
| ) | |
| max_length = gr.Slider( | |
| minimum=50, | |
| maximum=500, | |
| value=200, | |
| step=50, | |
| label="Max Response Length", | |
| info="Maximum characters in response" | |
| ) | |
| with gr.Accordion("Conversation", open=False): | |
| save_history = gr.Checkbox( | |
| label="Save Conversation", | |
| value=True, | |
| info="Remember conversation context" | |
| ) | |
| show_timestamp = gr.Checkbox( | |
| label="Show Timestamps", | |
| value=False, | |
| info="Display message timestamps" | |
| ) | |
| # Quick actions | |
| gr.Markdown("### 🎯 Quick Actions") | |
| with gr.Row(): | |
| copy_last_btn = gr.Button("📋 Copy Last", size="sm") | |
| clear_chat_btn = gr.Button("🗑️ Clear All", size="sm", variant="secondary") | |
| # Info panel | |
| with gr.Accordion("ℹ️ About", open=False): | |
| gr.Markdown(""" | |
| **Model:** Mock AI Assistant | |
| **Features:** | |
| - Real-time responses | |
| - Conversation memory | |
| - Typing animation | |
| - Copy messages | |
| - Retry responses | |
| **Tips:** | |
| - Use clear questions | |
| - Provide context for better answers | |
| - Try the example prompts below | |
| """) | |
| # Footer | |
| gr.HTML(""" | |
| <div style="text-align: center; margin-top: 20px; padding-top: 20px; border-top: 1px solid #eee;"> | |
| <p style="color: #999; font-size: 0.9em;"> | |
| 💬 Have a great conversation! | | |
| 🔒 Your privacy matters - conversations are not stored | |
| </p> | |
| </div> | |
| """) | |
| return interface | |
| if __name__ == "__main__": | |
| # Create and launch the interface | |
| app = create_chat_interface() | |
| app.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False, | |
| show_error=True, | |
| quiet=False | |
| ) |