akhaliq HF Staff commited on
Commit
63a4de4
Β·
verified Β·
1 Parent(s): 5fbe893

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +217 -0
  2. requirements.txt +10 -0
app.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ import time
4
+ from datetime import datetime
5
+
6
+ def format_message(role, content, timestamp):
7
+ """Format a chat message as HTML"""
8
+ avatar = "πŸ€–" if role == "assistant" else "πŸ‘€"
9
+ bg_color = "#f0f0f0" if role == "assistant" else "#e3f2fd"
10
+ align = "flex-start" if role == "assistant" else "flex-end"
11
+
12
+ html = f"""
13
+ <div style="display: flex; justify-content: {align}; margin: 10px 0;">
14
+ <div style="max-width: 70%; background: {bg_color}; padding: 12px 16px; border-radius: 12px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
15
+ <div style="display: flex; align-items: center; margin-bottom: 4px;">
16
+ <span style="font-size: 20px; margin-right: 8px;">{avatar}</span>
17
+ <span style="font-weight: 600; color: #333;">{role.capitalize()}</span>
18
+ <span style="margin-left: auto; font-size: 11px; color: #666;">{timestamp}</span>
19
+ </div>
20
+ <div style="color: #222; line-height: 1.5; white-space: pre-wrap;">{content}</div>
21
+ </div>
22
+ </div>
23
+ """
24
+ return html
25
+
26
+ def create_chat_html(messages):
27
+ """Create HTML for entire chat history"""
28
+ if not messages:
29
+ return """
30
+ <div style="text-align: center; padding: 40px; color: #999;">
31
+ <h3>No messages yet</h3>
32
+ <p>Start a conversation by typing a message below!</p>
33
+ </div>
34
+ """
35
+
36
+ chat_html = """
37
+ <div style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;">
38
+ """
39
+
40
+ for msg in messages:
41
+ chat_html += format_message(
42
+ msg["role"],
43
+ msg["content"],
44
+ msg["timestamp"]
45
+ )
46
+
47
+ chat_html += "</div>"
48
+ return chat_html
49
+
50
+ def respond(message, history):
51
+ """Generate bot response"""
52
+ timestamp = datetime.now().strftime("%H:%M")
53
+
54
+ # Add user message to history
55
+ history.append({
56
+ "role": "user",
57
+ "content": message,
58
+ "timestamp": timestamp
59
+ })
60
+
61
+ # Simulate thinking
62
+ time.sleep(0.5)
63
+
64
+ # Generate bot response
65
+ responses = [
66
+ "That's an interesting point! Tell me more about that.",
67
+ "I understand what you're saying. How does that make you feel?",
68
+ "Thanks for sharing! I'm here to help with any questions you have.",
69
+ "Great question! Let me think about that for a moment...",
70
+ "I appreciate your perspective on this topic.",
71
+ "That's a good observation. What else would you like to discuss?",
72
+ "Interesting! I'd love to hear more details about that.",
73
+ "I see where you're coming from. Let's explore this further.",
74
+ ]
75
+
76
+ # Add some context-aware responses
77
+ if "hello" in message.lower() or "hi" in message.lower():
78
+ bot_response = "Hello! How can I assist you today? 😊"
79
+ elif "bye" in message.lower() or "goodbye" in message.lower():
80
+ bot_response = "Goodbye! It was nice chatting with you. Have a great day! πŸ‘‹"
81
+ elif "help" in message.lower():
82
+ bot_response = "I'm here to help! You can ask me anything, and I'll do my best to assist you. What would you like to know?"
83
+ elif "?" in message:
84
+ bot_response = f"That's a great question! {random.choice(responses)}"
85
+ else:
86
+ bot_response = random.choice(responses)
87
+
88
+ # Add bot message to history
89
+ history.append({
90
+ "role": "assistant",
91
+ "content": bot_response,
92
+ "timestamp": datetime.now().strftime("%H:%M")
93
+ })
94
+
95
+ return create_chat_html(history), history, ""
96
+
97
+ def clear_chat():
98
+ """Clear chat history"""
99
+ return create_chat_html([]), [], ""
100
+
101
+ # Custom CSS for better styling
102
+ custom_css = """
103
+ #chat-container {
104
+ height: 500px;
105
+ overflow-y: auto;
106
+ border: 1px solid #ddd;
107
+ border-radius: 8px;
108
+ padding: 16px;
109
+ background: white;
110
+ }
111
+
112
+ #chat-container::-webkit-scrollbar {
113
+ width: 8px;
114
+ }
115
+
116
+ #chat-container::-webkit-scrollbar-track {
117
+ background: #f1f1f1;
118
+ border-radius: 4px;
119
+ }
120
+
121
+ #chat-container::-webkit-scrollbar-thumb {
122
+ background: #888;
123
+ border-radius: 4px;
124
+ }
125
+
126
+ #chat-container::-webkit-scrollbar-thumb:hover {
127
+ background: #555;
128
+ }
129
+
130
+ .input-row {
131
+ margin-top: 16px;
132
+ }
133
+
134
+ footer {
135
+ text-align: center;
136
+ margin-top: 20px;
137
+ padding: 10px;
138
+ color: #666;
139
+ }
140
+ """
141
+
142
+ # Create the Gradio interface
143
+ with gr.Blocks(css=custom_css, title="HTML Chatbot", fill_height=True) as demo:
144
+ gr.Markdown(
145
+ """
146
+ # πŸ€– Custom HTML Chatbot
147
+ ### A beautiful chatbot built with gr.HTML component
148
+
149
+ <p style='text-align: center;'>
150
+ <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank' style='text-decoration: none; color: #4A90E2;'>
151
+ Built with anycoder
152
+ </a>
153
+ </p>
154
+ """
155
+ )
156
+
157
+ # State to store chat history
158
+ chat_history = gr.State([])
159
+
160
+ # Chat display using HTML component
161
+ chat_display = gr.HTML(
162
+ value=create_chat_html([]),
163
+ elem_id="chat-container"
164
+ )
165
+
166
+ # Input row
167
+ with gr.Row(elem_classes="input-row"):
168
+ with gr.Column(scale=9):
169
+ message_input = gr.Textbox(
170
+ placeholder="Type your message here...",
171
+ show_label=False,
172
+ container=False,
173
+ autofocus=True
174
+ )
175
+ with gr.Column(scale=1, min_width=100):
176
+ send_btn = gr.Button("Send", variant="primary")
177
+
178
+ with gr.Row():
179
+ clear_btn = gr.Button("Clear Chat", variant="secondary", size="sm")
180
+
181
+ # Event handlers
182
+ send_btn.click(
183
+ fn=respond,
184
+ inputs=[message_input, chat_history],
185
+ outputs=[chat_display, chat_history, message_input],
186
+ api_name="send_message"
187
+ )
188
+
189
+ message_input.submit(
190
+ fn=respond,
191
+ inputs=[message_input, chat_history],
192
+ outputs=[chat_display, chat_history, message_input],
193
+ api_name=False
194
+ )
195
+
196
+ clear_btn.click(
197
+ fn=clear_chat,
198
+ inputs=[],
199
+ outputs=[chat_display, chat_history, message_input],
200
+ api_name="clear_chat"
201
+ )
202
+
203
+ gr.Markdown(
204
+ """
205
+ ---
206
+ ### Features:
207
+ - 🎨 Custom HTML rendering for beautiful message display
208
+ - ⏰ Timestamps for each message
209
+ - πŸ‘€ User and bot avatars
210
+ - 🎯 Context-aware responses
211
+ - 🧹 Clear chat functionality
212
+ - πŸ“± Responsive design
213
+ """
214
+ )
215
+
216
+ if __name__ == "__main__":
217
+ demo.launch()
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