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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
from typing import Generator, Tuple
|
| 5 |
+
|
| 6 |
+
def get_response(message: str, history: list) -> Generator[str, None, None]:
|
| 7 |
+
"""
|
| 8 |
+
Generate a response for the chatbot.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
message: The user's input message
|
| 12 |
+
history: The conversation history
|
| 13 |
+
|
| 14 |
+
Yields:
|
| 15 |
+
Stream of response text for a typing effect
|
| 16 |
+
"""
|
| 17 |
+
# Simple mock responses - in a real app, this would call an LLM API
|
| 18 |
+
responses = [
|
| 19 |
+
"That's an interesting question! Let me think about that...",
|
| 20 |
+
"I understand what you're saying. Here's my perspective:",
|
| 21 |
+
"Great point! I'd like to add that...",
|
| 22 |
+
"Hmm, that's something worth considering. My thoughts are:",
|
| 23 |
+
"Thank you for sharing! From my understanding:",
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
# Simple contextual responses
|
| 27 |
+
if "hello" in message.lower() or "hi" in message.lower():
|
| 28 |
+
response = "Hello! How can I help you today?"
|
| 29 |
+
elif "how are you" in message.lower():
|
| 30 |
+
response = "I'm doing great, thanks for asking! I'm here to assist you with any questions or conversations you'd like to have."
|
| 31 |
+
elif "bye" in message.lower() or "goodbye" in message.lower():
|
| 32 |
+
response = "Goodbye! It was nice chatting with you. Feel free to come back anytime!"
|
| 33 |
+
elif "help" in message.lower():
|
| 34 |
+
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?"
|
| 35 |
+
else:
|
| 36 |
+
# Generate a semi-random contextual response
|
| 37 |
+
base_response = random.choice(responses)
|
| 38 |
+
additional = [
|
| 39 |
+
" based on what you've shared, I believe the key is to consider multiple perspectives and find a balanced approach.",
|
| 40 |
+
" it's important to think about both the short-term and long-term implications.",
|
| 41 |
+
" there are often many valid viewpoints to consider in such situations.",
|
| 42 |
+
" the answer might depend on various factors and context.",
|
| 43 |
+
" this is a complex topic with many interesting dimensions to explore.",
|
| 44 |
+
]
|
| 45 |
+
response = base_response + random.choice(additional)
|
| 46 |
+
|
| 47 |
+
# Simulate typing effect
|
| 48 |
+
partial_response = ""
|
| 49 |
+
for word in response.split():
|
| 50 |
+
partial_response += word + " "
|
| 51 |
+
yield partial_response
|
| 52 |
+
time.sleep(0.05)
|
| 53 |
+
|
| 54 |
+
def format_examples() -> list:
|
| 55 |
+
"""
|
| 56 |
+
Create example prompts for the chatbot.
|
| 57 |
+
|
| 58 |
+
Returns:
|
| 59 |
+
List of example messages
|
| 60 |
+
"""
|
| 61 |
+
return [
|
| 62 |
+
"Hello! Tell me about yourself.",
|
| 63 |
+
"What's your opinion on artificial intelligence?",
|
| 64 |
+
"Can you help me understand machine learning?",
|
| 65 |
+
"How do you think technology will change the future?",
|
| 66 |
+
"Tell me an interesting fact!",
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
def create_chat_interface() -> gr.Blocks:
|
| 70 |
+
"""
|
| 71 |
+
Create and configure the Gradio chatbot interface.
|
| 72 |
+
|
| 73 |
+
Returns:
|
| 74 |
+
Configured Gradio Blocks interface
|
| 75 |
+
"""
|
| 76 |
+
with gr.Blocks(
|
| 77 |
+
title="AI Chatbot",
|
| 78 |
+
theme=gr.themes.Soft(
|
| 79 |
+
primary_hue="blue",
|
| 80 |
+
secondary_hue="gray",
|
| 81 |
+
neutral_hue="slate"
|
| 82 |
+
),
|
| 83 |
+
css="""
|
| 84 |
+
.chatbot-container {
|
| 85 |
+
height: 600px !important;
|
| 86 |
+
}
|
| 87 |
+
.message-wrap {
|
| 88 |
+
padding: 10px !important;
|
| 89 |
+
}
|
| 90 |
+
"""
|
| 91 |
+
) as interface:
|
| 92 |
+
|
| 93 |
+
# Header
|
| 94 |
+
with gr.Row():
|
| 95 |
+
gr.HTML("""
|
| 96 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
| 97 |
+
<h1>🤖 AI Chatbot</h1>
|
| 98 |
+
<p style="color: #666;">Your intelligent conversational assistant</p>
|
| 99 |
+
<p style="font-size: 0.9em; color: #999;">
|
| 100 |
+
Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #1f77b4; text-decoration: none;">anycoder</a>
|
| 101 |
+
</p>
|
| 102 |
+
</div>
|
| 103 |
+
""")
|
| 104 |
+
|
| 105 |
+
# Chat interface
|
| 106 |
+
with gr.Row():
|
| 107 |
+
with gr.Column(scale=4):
|
| 108 |
+
chatbot = gr.ChatInterface(
|
| 109 |
+
fn=get_response,
|
| 110 |
+
title="",
|
| 111 |
+
examples=format_examples(),
|
| 112 |
+
cache_examples=False,
|
| 113 |
+
chatbot=gr.Chatbot(
|
| 114 |
+
height=600,
|
| 115 |
+
show_copy_button=True,
|
| 116 |
+
avatar_images=("👤", "🤖"),
|
| 117 |
+
bubble_full_width=False,
|
| 118 |
+
type="messages"
|
| 119 |
+
),
|
| 120 |
+
textbox=gr.Textbox(
|
| 121 |
+
placeholder="Type your message here...",
|
| 122 |
+
container=False,
|
| 123 |
+
scale=7,
|
| 124 |
+
autofocus=True
|
| 125 |
+
),
|
| 126 |
+
submit_btn="Send",
|
| 127 |
+
stop_btn="Stop",
|
| 128 |
+
retry_btn="🔄 Retry",
|
| 129 |
+
undo_btn="↩️ Undo",
|
| 130 |
+
clear_btn="🗑️ Clear"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
with gr.Column(scale=1):
|
| 134 |
+
# Settings panel
|
| 135 |
+
gr.Markdown("### ⚙️ Settings")
|
| 136 |
+
|
| 137 |
+
with gr.Accordion("Chat Options", open=True):
|
| 138 |
+
temperature = gr.Slider(
|
| 139 |
+
minimum=0.1,
|
| 140 |
+
maximum=2.0,
|
| 141 |
+
value=0.7,
|
| 142 |
+
step=0.1,
|
| 143 |
+
label="Response Creativity",
|
| 144 |
+
info="Higher values make responses more creative"
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
max_length = gr.Slider(
|
| 148 |
+
minimum=50,
|
| 149 |
+
maximum=500,
|
| 150 |
+
value=200,
|
| 151 |
+
step=50,
|
| 152 |
+
label="Max Response Length",
|
| 153 |
+
info="Maximum characters in response"
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
with gr.Accordion("Conversation", open=False):
|
| 157 |
+
save_history = gr.Checkbox(
|
| 158 |
+
label="Save Conversation",
|
| 159 |
+
value=True,
|
| 160 |
+
info="Remember conversation context"
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
show_timestamp = gr.Checkbox(
|
| 164 |
+
label="Show Timestamps",
|
| 165 |
+
value=False,
|
| 166 |
+
info="Display message timestamps"
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# Quick actions
|
| 170 |
+
gr.Markdown("### 🎯 Quick Actions")
|
| 171 |
+
|
| 172 |
+
with gr.Row():
|
| 173 |
+
copy_last_btn = gr.Button("📋 Copy Last", size="sm")
|
| 174 |
+
clear_chat_btn = gr.Button("🗑️ Clear All", size="sm", variant="secondary")
|
| 175 |
+
|
| 176 |
+
# Info panel
|
| 177 |
+
with gr.Accordion("ℹ️ About", open=False):
|
| 178 |
+
gr.Markdown("""
|
| 179 |
+
**Model:** Mock AI Assistant
|
| 180 |
+
|
| 181 |
+
**Features:**
|
| 182 |
+
- Real-time responses
|
| 183 |
+
- Conversation memory
|
| 184 |
+
- Typing animation
|
| 185 |
+
- Copy messages
|
| 186 |
+
- Retry responses
|
| 187 |
+
|
| 188 |
+
**Tips:**
|
| 189 |
+
- Use clear questions
|
| 190 |
+
- Provide context for better answers
|
| 191 |
+
- Try the example prompts below
|
| 192 |
+
""")
|
| 193 |
+
|
| 194 |
+
# Footer
|
| 195 |
+
gr.HTML("""
|
| 196 |
+
<div style="text-align: center; margin-top: 20px; padding-top: 20px; border-top: 1px solid #eee;">
|
| 197 |
+
<p style="color: #999; font-size: 0.9em;">
|
| 198 |
+
💬 Have a great conversation! |
|
| 199 |
+
🔒 Your privacy matters - conversations are not stored
|
| 200 |
+
</p>
|
| 201 |
+
</div>
|
| 202 |
+
""")
|
| 203 |
+
|
| 204 |
+
return interface
|
| 205 |
+
|
| 206 |
+
if __name__ == "__main__":
|
| 207 |
+
# Create and launch the interface
|
| 208 |
+
app = create_chat_interface()
|
| 209 |
+
app.launch(
|
| 210 |
+
server_name="0.0.0.0",
|
| 211 |
+
server_port=7860,
|
| 212 |
+
share=False,
|
| 213 |
+
show_error=True,
|
| 214 |
+
quiet=False
|
| 215 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
Pillow
|
| 4 |
+
numpy
|
| 5 |
+
pandas
|
| 6 |
+
matplotlib
|
| 7 |
+
plotly
|
| 8 |
+
fastapi
|
| 9 |
+
uvicorn
|
| 10 |
+
pydantic
|