Bndo commited on
Commit
c7c06b4
·
verified ·
1 Parent(s): 9c5d6aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -1,33 +1,32 @@
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load a Hugging Face model for chat (choose an appropriate model)
5
  chatbot_pipeline = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct", max_new_tokens=100)
6
 
7
- # Chatbot function using HF's transformer model
8
  def chat_response(history, user_input):
9
- """Handles chatbot interactions using a Hugging Face model."""
10
  if not user_input.strip():
11
  return history + [{"role": "user", "content": user_input}, {"role": "assistant", "content": "Please enter a valid message."}], ""
12
 
13
- # Generate AI response
14
  bot_reply = chatbot_pipeline(user_input)[0]["generated_text"]
15
 
16
- # Append messages to history
17
  history.append({"role": "user", "content": user_input})
18
  history.append({"role": "assistant", "content": bot_reply})
19
 
20
  return history, ""
21
 
22
- # Initialize Gradio chatbot
23
  with gr.Blocks() as demo:
24
  gr.Markdown("## 🤖 AI Bandar - Powered by Hugging Face")
25
 
26
- chatbot = gr.Chatbot(type="messages") # Correct type
27
  user_input = gr.Textbox(label="Your Message")
28
  submit_button = gr.Button("Send")
29
 
30
- # Button click or Enter key submits the message
31
  submit_button.click(chat_response, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
32
  user_input.submit(chat_response, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
33
 
 
1
+ import os
2
+ os.system("pip install transformers torch accelerate gradio") # Install dependencies
3
+
4
  import gradio as gr
5
  from transformers import pipeline
6
 
7
+ # Load the Hugging Face model
8
  chatbot_pipeline = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct", max_new_tokens=100)
9
 
10
+ # Chatbot function
11
  def chat_response(history, user_input):
 
12
  if not user_input.strip():
13
  return history + [{"role": "user", "content": user_input}, {"role": "assistant", "content": "Please enter a valid message."}], ""
14
 
 
15
  bot_reply = chatbot_pipeline(user_input)[0]["generated_text"]
16
 
 
17
  history.append({"role": "user", "content": user_input})
18
  history.append({"role": "assistant", "content": bot_reply})
19
 
20
  return history, ""
21
 
22
+ # Gradio UI
23
  with gr.Blocks() as demo:
24
  gr.Markdown("## 🤖 AI Bandar - Powered by Hugging Face")
25
 
26
+ chatbot = gr.Chatbot(type="messages")
27
  user_input = gr.Textbox(label="Your Message")
28
  submit_button = gr.Button("Send")
29
 
 
30
  submit_button.click(chat_response, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
31
  user_input.submit(chat_response, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
32