Spaces:
Runtime error
Runtime error
File size: 8,000 Bytes
d065bd3 |
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
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
) |