Soumik555 commited on
Commit
a41d90a
Β·
1 Parent(s): 0020469
Files changed (3) hide show
  1. Dockerfile +1 -1
  2. app.py β†’ main.py +158 -0
  3. requirements.txt +1 -0
Dockerfile CHANGED
@@ -57,4 +57,4 @@ HEALTHCHECK --interval=30s --timeout=30s --start-period=90s --retries=3 \
57
  CMD curl -f http://localhost:7860/health || exit 1
58
 
59
  # Run the application
60
- CMD ["python", "app.py"]
 
57
  CMD curl -f http://localhost:7860/health || exit 1
58
 
59
  # Run the application
60
+ CMD ["python", "main.py"]
app.py β†’ main.py RENAMED
@@ -1,4 +1,5 @@
1
  import os
 
2
  from fastapi import FastAPI, HTTPException
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from fastapi.staticfiles import StaticFiles
@@ -258,6 +259,148 @@ if Path("static").exists():
258
  app.mount("/static", StaticFiles(directory="static"), name="static")
259
 
260
  # Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
 
262
  def run_fastapi():
263
  """Run FastAPI server"""
@@ -283,10 +426,25 @@ def main():
283
 
284
  logger.info("βœ… Model loaded successfully!")
285
 
 
 
 
 
286
  # Start FastAPI server in a separate thread
287
  logger.info("🌐 Starting FastAPI server...")
288
  fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
289
  fastapi_thread.start()
 
 
 
 
 
 
 
 
 
 
 
290
 
291
  if __name__ == "__main__":
292
  main()
 
1
  import os
2
+ import gradio as gr
3
  from fastapi import FastAPI, HTTPException
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from fastapi.staticfiles import StaticFiles
 
259
  app.mount("/static", StaticFiles(directory="static"), name="static")
260
 
261
  # Gradio interface
262
+ def chat_with_bot(message, history, max_length, temperature, top_p):
263
+ """Gradio chat function with advanced parameters"""
264
+ if not message.strip():
265
+ return "Please enter a message."
266
+
267
+ response_text, _ = generate_response(message.strip(), max_length, temperature, top_p)
268
+ return response_text
269
+
270
+ def create_gradio_interface():
271
+ """Create enhanced Gradio interface"""
272
+
273
+ # Custom CSS
274
+ css = """
275
+ .gradio-container {
276
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
277
+ max-width: 1200px;
278
+ margin: 0 auto;
279
+ }
280
+ .chat-message {
281
+ font-size: 14px !important;
282
+ line-height: 1.4;
283
+ }
284
+ .gradio-chatbot {
285
+ height: 500px;
286
+ }
287
+ """
288
+
289
+ # Create interface with advanced controls
290
+ with gr.Blocks(css=css, title="FastAPI Chatbot", theme=gr.themes.Soft()) as demo:
291
+
292
+ gr.HTML("<h1 style='text-align: center; color: #2563eb;'>πŸ€– FastAPI Chatbot</h1>")
293
+ gr.HTML(f"<p style='text-align: center; color: #6b7280;'>Powered by {MODEL_NAME}</p>")
294
+
295
+ with gr.Row():
296
+ with gr.Column(scale=3):
297
+ chatbot = gr.Chatbot(
298
+ height=500,
299
+ show_copy_button=True,
300
+ bubble_full_width=False,
301
+ avatar_images=("πŸ‘€", "πŸ€–")
302
+ )
303
+
304
+ with gr.Row():
305
+ msg = gr.Textbox(
306
+ placeholder="Type your message here...",
307
+ container=False,
308
+ scale=4,
309
+ max_lines=3
310
+ )
311
+ submit_btn = gr.Button("Send πŸ“€", scale=1, variant="primary")
312
+
313
+ with gr.Row():
314
+ clear_btn = gr.Button("Clear Chat πŸ—‘οΈ", scale=1)
315
+ retry_btn = gr.Button("Retry Last ↻", scale=1)
316
+
317
+ with gr.Column(scale=1):
318
+ gr.HTML("<h3>Settings</h3>")
319
+
320
+ max_length = gr.Slider(
321
+ minimum=50,
322
+ maximum=200,
323
+ value=MAX_LENGTH,
324
+ step=10,
325
+ label="Max Response Length"
326
+ )
327
+
328
+ temperature = gr.Slider(
329
+ minimum=0.1,
330
+ maximum=1.5,
331
+ value=DEFAULT_TEMPERATURE,
332
+ step=0.1,
333
+ label="Temperature (Creativity)"
334
+ )
335
+
336
+ top_p = gr.Slider(
337
+ minimum=0.1,
338
+ maximum=1.0,
339
+ value=0.9,
340
+ step=0.05,
341
+ label="Top-p (Focus)"
342
+ )
343
+
344
+ gr.HTML("<h4>Example Messages:</h4>")
345
+ examples = gr.Examples(
346
+ examples=[
347
+ ["Hello! How are you today?"],
348
+ ["Tell me a joke"],
349
+ ["What's your favorite hobby?"],
350
+ ["Can you help me with a creative writing prompt?"],
351
+ ["What do you think about technology?"]
352
+ ],
353
+ inputs=msg,
354
+ label="Click to try:"
355
+ )
356
+
357
+ # Event handlers
358
+ def respond(message, history, max_len, temp, top_p):
359
+ if not message.strip():
360
+ return history, ""
361
+
362
+ # Add user message
363
+ history.append([message, None])
364
+
365
+ # Generate bot response
366
+ bot_response = chat_with_bot(message, history, max_len, temp, top_p)
367
+ history[-1][1] = bot_response
368
+
369
+ return history, ""
370
+
371
+ def clear_chat():
372
+ return [], ""
373
+
374
+ def retry_last(history, max_len, temp, top_p):
375
+ if not history:
376
+ return history
377
+
378
+ last_user_msg = history[-1][0]
379
+ history[-1][1] = "Thinking..."
380
+
381
+ # Regenerate response
382
+ bot_response = chat_with_bot(last_user_msg, history, max_len, temp, top_p)
383
+ history[-1][1] = bot_response
384
+
385
+ return history
386
+
387
+ # Wire up events
388
+ submit_btn.click(
389
+ respond,
390
+ [msg, chatbot, max_length, temperature, top_p],
391
+ [chatbot, msg]
392
+ )
393
+
394
+ msg.submit(
395
+ respond,
396
+ [msg, chatbot, max_length, temperature, top_p],
397
+ [chatbot, msg]
398
+ )
399
+
400
+ clear_btn.click(clear_chat, outputs=[chatbot, msg])
401
+ retry_btn.click(retry_last, [chatbot, max_length, temperature, top_p], chatbot)
402
+
403
+ return demo
404
 
405
  def run_fastapi():
406
  """Run FastAPI server"""
 
426
 
427
  logger.info("βœ… Model loaded successfully!")
428
 
429
+ # Create Gradio interface
430
+ logger.info("🎨 Creating Gradio interface...")
431
+ demo = create_gradio_interface()
432
+
433
  # Start FastAPI server in a separate thread
434
  logger.info("🌐 Starting FastAPI server...")
435
  fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
436
  fastapi_thread.start()
437
+
438
+ # Launch Gradio interface
439
+ logger.info("πŸš€ Launching Gradio interface...")
440
+ demo.launch(
441
+ server_name="0.0.0.0",
442
+ server_port=7860,
443
+ share=False,
444
+ show_error=True,
445
+ quiet=False,
446
+ show_api=False
447
+ )
448
 
449
  if __name__ == "__main__":
450
  main()
requirements.txt CHANGED
@@ -4,6 +4,7 @@ transformers==4.35.2
4
  torch==2.1.0
5
  tokenizers==0.15.0
6
  accelerate==0.24.1
 
7
  requests==2.31.0
8
  numpy==1.24.3
9
  pydantic==2.4.2
 
4
  torch==2.1.0
5
  tokenizers==0.15.0
6
  accelerate==0.24.1
7
+ gradio==4.7.1
8
  requests==2.31.0
9
  numpy==1.24.3
10
  pydantic==2.4.2