Spaces:
Runtime error
Runtime error
File size: 5,953 Bytes
4fd8404 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 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
) |