Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from utils import get_chatbot_response | |
| import random | |
| def chat_response(message, history): | |
| """ | |
| Process user message and generate chatbot response | |
| Args: | |
| message (str): User's input message | |
| history (list): Chat history for context | |
| Returns: | |
| str: Generated response from the chatbot | |
| """ | |
| if not message.strip(): | |
| return "Please enter a message to chat!" | |
| # Get response from the utility function | |
| response = get_chatbot_response(message, history) | |
| return response | |
| def clear_chat(): | |
| """Clear the chat history""" | |
| return [] | |
| def refresh_suggestions(): | |
| """Generate new conversation starters""" | |
| suggestions = [ | |
| "What's the weather like today?", | |
| "Tell me a joke", | |
| "How does artificial intelligence work?", | |
| "What are some productivity tips?", | |
| "Explain quantum computing simply", | |
| "What are the latest trends in technology?", | |
| "How can I learn programming?", | |
| "What's the meaning of life?", | |
| "Tell me about space exploration", | |
| "What are some good books to read?" | |
| ] | |
| return random.choice(suggestions) | |
| # Create the Gradio Blocks interface | |
| with gr.Blocks( | |
| title="AI Chatbot", | |
| theme=gr.themes.Soft( | |
| primary_hue="blue", | |
| secondary_hue="gray", | |
| font=[gr.themes.GoogleFont("Inter"), "Arial", "sans-serif"] | |
| ), | |
| css=""" | |
| .gradio-container {max-width: 1200px !important;} | |
| .chatbot-container {height: 600px !important;} | |
| .header-text {text-align: center; margin-bottom: 20px;} | |
| .footer-text {text-align: center; margin-top: 20px; color: #666;} | |
| """, | |
| title="Intelligent Chatbot" | |
| ) as demo: | |
| # Header | |
| gr.HTML(""" | |
| <div class="header-text"> | |
| <h1>π€ AI Chatbot</h1> | |
| <p>Welcome! I'm your intelligent assistant. Ask me anything!</p> | |
| <p><a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #007bff; text-decoration: none;">Built with anycoder</a></p> | |
| </div> | |
| """) | |
| # Main chat interface | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| chat_interface = gr.ChatInterface( | |
| fn=chat_response, | |
| title="", | |
| description="", | |
| examples=[ | |
| "Hello! How are you?", | |
| "What's the weather like?", | |
| "Tell me a joke", | |
| "Explain AI in simple terms", | |
| "What are some productivity tips?", | |
| "How do I learn programming?", | |
| "What's the meaning of life?", | |
| "Tell me about space", | |
| "What books do you recommend?", | |
| "How does the internet work?" | |
| ], | |
| example_labels=[ | |
| "Greeting", "Weather", "Joke", "AI Explanation", "Productivity", | |
| "Programming", "Philosophy", "Space", "Books", "Technology" | |
| ], | |
| retry_btn="Send Again β»", | |
| undo_btn="Undo Last Message βΆ", | |
| clear_btn="Clear Chat ποΈ", | |
| submit_btn="Send β€" | |
| ) | |
| with gr.Column(scale=1): | |
| # Side panel with conversation starters and info | |
| gr.HTML(""" | |
| <div style="background: #f8f9fa; padding: 20px; border-radius: 10px; margin-bottom: 20px;"> | |
| <h3>π¬ Conversation Starters</h3> | |
| </div> | |
| """) | |
| conversation_starters = gr.Examples( | |
| examples=[ | |
| ["Tell me a joke", "What makes you laugh?"], | |
| ["What's your favorite topic?", "I love discussing technology!"], | |
| ["How can I be productive?", "Time management is key!"], | |
| ["What should I learn today?", "Learning is lifelong!"], | |
| ["Explain something interesting", "Did you know honey never spoils?"], | |
| ], | |
| inputs=[chat_interface.message_box], | |
| cache_examples=False, | |
| label="Quick Starters" | |
| ) | |
| # Tips and info | |
| gr.HTML(""" | |
| <div style="background: #e3f2fd; padding: 15px; border-radius: 8px; margin-top: 20px;"> | |
| <h4>π‘ Tips</h4> | |
| <ul style="margin: 10px 0; padding-left: 20px;"> | |
| <li>Ask follow-up questions for deeper conversations</li> | |
| <li>Try different topics - I'm knowledgeable about many subjects!</li> | |
| <li>Use the examples above to get started quickly</li> | |
| </ul> | |
| </div> | |
| """) | |
| # Chat stats | |
| chat_stats = gr.HTML(""" | |
| <div style="background: #fff3e0; padding: 15px; border-radius: 8px; margin-top: 15px;"> | |
| <h4>π Chat Info</h4> | |
| <p><strong>Model:</strong> AI Assistant v1.0</p> | |
| <p><strong>Features:</strong> Multi-turn conversations</p> | |
| <p><strong>Topics:</strong> General knowledge, tech, science, jokes & more!</p> | |
| </div> | |
| """) | |
| # Footer | |
| gr.HTML(""" | |
| <div class="footer-text"> | |
| <p>π¬ Have a great conversation! | | |
| <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #007bff;">Built with anycoder</a> | | |
| <a href="#" style="color: #007bff; text-decoration: none;" onclick="window.location.reload()">Refresh Chat β»</a></p> | |
| </div> | |
| """) | |
| # Launch the application | |
| if __name__ == "__main__": | |
| demo.queue( | |
| concurrency_count=3, | |
| max_size=50, | |
| default_concurrency_limit=1 | |
| ).launch( | |
| share=False, | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| show_error=True, | |
| quiet=False | |
| ) |