BikoRiko commited on
Commit
2d5c442
Β·
verified Β·
1 Parent(s): f910969

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +388 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,388 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ AI Chatbot powered by HuggingFace Model
3
+ A modern conversational AI built with Gradio 6 and transformers
4
+ """
5
+
6
+ import gradio as gr
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
8
+ import warnings
9
+
10
+ warnings.filterwarnings("ignore")
11
+
12
+ # Model configuration
13
+ MODEL_NAME = "microsoft/DialoGPT-medium"
14
+
15
+ def load_chatbot():
16
+ """
17
+ Load the conversational model and tokenizer from HuggingFace.
18
+ Returns a conversational pipeline for generating responses.
19
+ """
20
+ try:
21
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, padding_side="left")
22
+ if tokenizer.pad_token is None:
23
+ tokenizer.pad_token = tokenizer.eos_token
24
+
25
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
26
+
27
+ conversation_pipeline = pipeline(
28
+ "text-generation",
29
+ model=model,
30
+ tokenizer=tokenizer,
31
+ max_new_tokens=150,
32
+ do_sample=True,
33
+ temperature=0.7,
34
+ top_p=0.95,
35
+ pad_token_id=tokenizer.eos_token_id
36
+ )
37
+
38
+ return conversation_pipeline
39
+ except Exception as e:
40
+ raise RuntimeError(f"Failed to load model: {str(e)}")
41
+
42
+ # Global pipeline variable
43
+ chat_pipeline = None
44
+
45
+ def get_chat_pipeline():
46
+ """Get or initialize the chat pipeline."""
47
+ global chat_pipeline
48
+ if chat_pipeline is None:
49
+ chat_pipeline = load_chatbot()
50
+ return chat_pipeline
51
+
52
+ def format_prompt(message, history):
53
+ """Format the conversation history and current message into a prompt."""
54
+ prompt = ""
55
+
56
+ # Add conversation history
57
+ for user_msg, bot_msg in history:
58
+ prompt += f"<|user|>{user_msg}<|endoftext|>"
59
+ prompt += f"<|assistant|>{bot_msg}<|endoftext|>"
60
+
61
+ # Add current message
62
+ prompt += f"<|user|>{message}<|endoftext|>"
63
+ prompt += "<|assistant|>"
64
+
65
+ return prompt
66
+
67
+ def chatbot_fn(message, history, system_prompt, max_length, temperature, top_p):
68
+ """
69
+ Generate a response from the chatbot based on user input.
70
+
71
+ Args:
72
+ message: User's input message
73
+ history: List of previous (user, bot) message tuples
74
+ system_prompt: System prompt to set the bot's behavior
75
+ max_length: Maximum length of generated response
76
+ temperature: Randomness of generation (0.0-2.0)
77
+ top_p: Top-p sampling parameter (0.0-1.0)
78
+
79
+ Returns:
80
+ Updated conversation history with the new response
81
+ """
82
+ # Validate input
83
+ if not message or not message.strip():
84
+ return history + [("Empty message", "Please enter a message to chat!")]
85
+
86
+ # Update pipeline parameters
87
+ pipeline = get_chat_pipeline()
88
+ pipeline.task_kwargs = {
89
+ "max_new_tokens": max_length,
90
+ "do_sample": temperature > 0,
91
+ "temperature": temperature if temperature > 0 else None,
92
+ "top_p": top_p if top_p < 1.0 else None,
93
+ }
94
+
95
+ try:
96
+ # Format the prompt with history
97
+ full_prompt = format_prompt(message, history)
98
+
99
+ # Add system prompt context
100
+ if system_prompt:
101
+ full_prompt = f"<|system|>{system_prompt}<|endoftext|>\n{full_prompt}"
102
+
103
+ # Generate response
104
+ response = pipeline(
105
+ full_prompt,
106
+ max_new_tokens=max_length,
107
+ do_sample=temperature > 0,
108
+ temperature=temperature if temperature > 0 else 0.7,
109
+ top_p=top_p if top_p < 1.0 else 0.95,
110
+ pad_token_id=50256, # EOS token for DialoGPT
111
+ truncation=True
112
+ )
113
+
114
+ # Extract the generated text
115
+ generated_text = response[0]["generated_text"]
116
+
117
+ # Remove the prompt from the response
118
+ response_text = generated_text[len(full_prompt):].strip()
119
+
120
+ # Clean up the response - remove special tokens
121
+ for token in ["<|endoftext|>", "<|user|>", "<|assistant|>", "<|system|>"]:
122
+ response_text = response_text.split(token)[0].strip()
123
+
124
+ # If response is empty or too short, provide a fallback
125
+ if not response_text or len(response_text) < 2:
126
+ response_text = "I'm not sure how to respond to that. Could you try again?"
127
+
128
+ # Add the new exchange to history
129
+ new_history = history + [(message, response_text)]
130
+
131
+ return new_history
132
+
133
+ except Exception as e:
134
+ error_msg = f"Sorry, I encountered an error: {str(e)}"
135
+ return history + [(message, error_msg)]
136
+
137
+ def clear_chat():
138
+ """Clear the chat history."""
139
+ return []
140
+
141
+ # Create custom theme for the chatbot
142
+ chatbot_theme = gr.themes.Soft(
143
+ primary_hue="indigo",
144
+ secondary_hue="purple",
145
+ neutral_hue="slate",
146
+ font=gr.themes.GoogleFont("Inter"),
147
+ text_size="lg",
148
+ spacing_size="md",
149
+ radius_size="lg"
150
+ ).set(
151
+ button_primary_background_fill="*primary_600",
152
+ button_primary_background_fill_hover="*primary_700",
153
+ button_secondary_background_fill="*secondary_300",
154
+ button_secondary_background_fill_hover="*secondary_400",
155
+ block_title_text_weight="600",
156
+ block_background_fill_dark="*neutral_800",
157
+ )
158
+
159
+ # Custom CSS for enhanced styling
160
+ custom_css = """
161
+ .gradio-container {
162
+ max-width: 1200px !important;
163
+ }
164
+
165
+ .chatbot-container {
166
+ background: linear-gradient(135deg, #f5f7fa 0%, #e4e8ec 100%);
167
+ border-radius: 16px;
168
+ padding: 20px;
169
+ }
170
+
171
+ .chat-header {
172
+ background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
173
+ -webkit-background-clip: text;
174
+ -webkit-text-fill-color: transparent;
175
+ background-clip: text;
176
+ }
177
+
178
+ .user-message {
179
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
180
+ color: white;
181
+ border-radius: 18px 18px 4px 18px;
182
+ padding: 12px 16px;
183
+ margin: 8px 0;
184
+ }
185
+
186
+ .bot-message {
187
+ background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
188
+ color: white;
189
+ border-radius: 18px 18px 18px 4px;
190
+ padding: 12px 16px;
191
+ margin: 8px 0;
192
+ }
193
+
194
+ .settings-panel {
195
+ background: rgba(255, 255, 255, 0.9);
196
+ border-radius: 12px;
197
+ padding: 16px;
198
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
199
+ }
200
+
201
+ .loading-indicator {
202
+ animation: pulse 1.5s infinite;
203
+ }
204
+
205
+ @keyframes pulse {
206
+ 0%, 100% { opacity: 1; }
207
+ 50% { opacity: 0.5; }
208
+ }
209
+ """
210
+
211
+ # Build the Gradio application
212
+ with gr.Blocks(
213
+ title="AI Chatbot - Powered by HuggingFace",
214
+ fill_height=True,
215
+ fill_width=True
216
+ ) as demo:
217
+
218
+ # Header with branding
219
+ with gr.Row():
220
+ gr.HTML("""
221
+ <div style="text-align: center; padding: 20px 0;">
222
+ <h1 style="font-size: 2.5em; margin-bottom: 10px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;">
223
+ πŸ€– AI Chatbot
224
+ </h1>
225
+ <p style="color: #666; font-size: 1.1em;">
226
+ Powered by Microsoft DialoGPT β€’ Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #667eea;">anycoder</a>
227
+ </p>
228
+ </div>
229
+ """)
230
+
231
+ # Main content area
232
+ with gr.Row(equal_height=True):
233
+ # Settings sidebar
234
+ with gr.Column(scale=1, min_width=280):
235
+ with gr.Group(elem_classes=["settings-panel"]):
236
+ gr.Markdown("### βš™οΈ Chat Settings")
237
+
238
+ system_prompt = gr.Textbox(
239
+ label="System Prompt",
240
+ placeholder="You are a helpful and friendly assistant...",
241
+ value="You are a helpful, friendly AI assistant. You provide clear, concise, and accurate responses.",
242
+ lines=3,
243
+ max_lines=5
244
+ )
245
+
246
+ with gr.Accordion("Advanced Settings", open=False):
247
+ max_length = gr.Slider(
248
+ label="Max Response Length",
249
+ minimum=50,
250
+ maximum=300,
251
+ value=150,
252
+ step=10
253
+ )
254
+
255
+ temperature = gr.Slider(
256
+ label="Temperature (Creativity)",
257
+ minimum=0.0,
258
+ maximum=2.0,
259
+ value=0.7,
260
+ step=0.1,
261
+ info="Higher = more creative, Lower = more focused"
262
+ )
263
+
264
+ top_p = gr.Slider(
265
+ label="Top-p Sampling",
266
+ minimum=0.1,
267
+ maximum=1.0,
268
+ value=0.95,
269
+ step=0.05,
270
+ info="Controls vocabulary diversity"
271
+ )
272
+
273
+ clear_btn = gr.Button(
274
+ "πŸ—‘οΈ Clear Chat",
275
+ variant="secondary",
276
+ size="lg"
277
+ )
278
+
279
+ gr.Markdown("""
280
+ ### πŸ’‘ Tips
281
+ - Be specific in your questions
282
+ - Try different prompts
283
+ - Adjust temperature for creativity
284
+ - Clear chat to start fresh
285
+ """)
286
+
287
+ # Chat interface
288
+ with gr.Column(scale=3):
289
+ with gr.Group(elem_classes=["chatbot-container"]):
290
+ chatbot = gr.Chatbot(
291
+ label="πŸ’¬ Conversation",
292
+ placeholder="Start chatting with the AI!",
293
+ height=450,
294
+ bubble_full_width=False,
295
+ avatar_images=("πŸ‘€", "πŸ€–"),
296
+ latex_delimiters=[],
297
+ show_copy_button=True,
298
+ render_markdown=True
299
+ )
300
+
301
+ # Input area
302
+ with gr.Row():
303
+ message_input = gr.Textbox(
304
+ placeholder="Type your message here...",
305
+ label=None,
306
+ show_label=False,
307
+ scale=5,
308
+ lines=2,
309
+ max_lines=4,
310
+ submit_btn=True
311
+ )
312
+
313
+ submit_btn = gr.Button(
314
+ "Send",
315
+ variant="primary",
316
+ scale=1,
317
+ size="lg"
318
+ )
319
+
320
+ # Example prompts
321
+ with gr.Row():
322
+ gr.Markdown("### ✨ Try these prompts:")
323
+
324
+ with gr.Row():
325
+ examples = gr.Examples(
326
+ examples=[
327
+ ["Tell me about artificial intelligence"],
328
+ ["What are some tips for productivity?"],
329
+ ["Explain quantum computing simply"],
330
+ ["Write a short poem about nature"]
331
+ ],
332
+ inputs=message_input,
333
+ label=None
334
+ )
335
+
336
+ # Event handlers
337
+ submit_btn.click(
338
+ fn=chatbot_fn,
339
+ inputs=[
340
+ message_input,
341
+ chatbot,
342
+ system_prompt,
343
+ max_length,
344
+ temperature,
345
+ top_p
346
+ ],
347
+ outputs=chatbot,
348
+ api_visibility="public",
349
+ show_progress="minimal"
350
+ )
351
+
352
+ message_input.submit(
353
+ fn=chatbot_fn,
354
+ inputs=[
355
+ message_input,
356
+ chatbot,
357
+ system_prompt,
358
+ max_length,
359
+ temperature,
360
+ top_p
361
+ ],
362
+ outputs=chatbot,
363
+ api_visibility="public",
364
+ show_progress="minimal"
365
+ )
366
+
367
+ clear_btn.click(
368
+ fn=clear_chat,
369
+ inputs=None,
370
+ outputs=chatbot,
371
+ api_visibility="public"
372
+ )
373
+
374
+ # Launch the application with Gradio 6 parameters
375
+ demo.launch(
376
+ theme=chatbot_theme,
377
+ css=custom_css,
378
+ server_name="0.0.0.0",
379
+ server_port=7860,
380
+ show_error=True,
381
+ quiet=False,
382
+ footer_links=[
383
+ {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
384
+ {"label": "Model: DialoGPT-medium", "url": "https://huggingface.co/microsoft/DialoGPT-medium"},
385
+ {"label": "Gradio", "url": "https://gradio.app"},
386
+ {"label": "HuggingFace", "url": "https://huggingface.co"}
387
+ ]
388
+ )
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ transformers