Dhansh2001 commited on
Commit
bd66ae1
·
verified ·
1 Parent(s): 9a1219a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -27,7 +27,7 @@ model.to(device)
27
  # ----------------------
28
  def chat_with_bot(message, history):
29
  try:
30
- # Combine history into a conversation string
31
  conversation = ""
32
  for user_msg, bot_msg in history:
33
  conversation += f"User: {user_msg}\nBot: {bot_msg}\n"
@@ -49,7 +49,7 @@ def chat_with_bot(message, history):
49
  early_stopping=True
50
  )
51
 
52
- # Decode only new part
53
  reply = tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)
54
  return reply.strip() if reply.strip() else "I'm not sure how to answer that."
55
 
@@ -57,24 +57,23 @@ def chat_with_bot(message, history):
57
  return f"⚠️ Error: {str(e)}"
58
 
59
  # ----------------------
60
- # Gradio ChatBlock
61
  # ----------------------
62
- with gr.Blocks() as demo:
63
- gr.Markdown("## Fitlien Chatbot 🤖\nChat with your fitness assistant!")
64
-
65
- chatbot = gr.Chatbot()
66
- msg = gr.Textbox(label="Your message")
67
- clear_btn = gr.Button("Clear Chat")
68
-
69
- def respond(message, history):
70
- reply = chat_with_bot(message, history)
71
- history.append((message, reply))
72
- return "", history
73
-
74
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
75
- clear_btn.click(lambda: [], None, chatbot)
76
-
77
- demo.api_name = "chat"
78
 
79
  # Launch
80
  if __name__ == "__main__":
 
27
  # ----------------------
28
  def chat_with_bot(message, history):
29
  try:
30
+ # Convert history to conversation string
31
  conversation = ""
32
  for user_msg, bot_msg in history:
33
  conversation += f"User: {user_msg}\nBot: {bot_msg}\n"
 
49
  early_stopping=True
50
  )
51
 
52
+ # Decode only the new part
53
  reply = tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)
54
  return reply.strip() if reply.strip() else "I'm not sure how to answer that."
55
 
 
57
  return f"⚠️ Error: {str(e)}"
58
 
59
  # ----------------------
60
+ # Use ChatInterface instead of Blocks
61
  # ----------------------
62
+ demo = gr.ChatInterface(
63
+ fn=chat_with_bot,
64
+ title="My Fitlien Chatbot",
65
+ description="A chatbot fine-tuned from DialoGPT-medium for fitness conversations.",
66
+ examples=[
67
+ "Hello! How are you?",
68
+ "What's a good workout routine?",
69
+ "Tell me about nutrition",
70
+ "How can I stay motivated?"
71
+ ],
72
+ retry_btn=None,
73
+ undo_btn="Delete Previous",
74
+ clear_btn="Clear",
75
+ submit_btn="Submit"
76
+ )
 
77
 
78
  # Launch
79
  if __name__ == "__main__":