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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -1,22 +1,29 @@
1
  import gradio as gr
 
2
 
3
- # Define the chatbot function
 
 
 
4
  def chat_response(history, user_input):
5
- """Handles chatbot interactions."""
6
  if not user_input.strip():
7
- return history + [("User", user_input), ("Bot", "Please enter a valid message.")]
 
 
 
8
 
9
- response = f"You said: {user_input}" # Replace this with actual chatbot logic (e.g., OpenAI API)
10
  history.append({"role": "user", "content": user_input})
11
- history.append({"role": "assistant", "content": response})
12
 
13
  return history, ""
14
 
15
- # Initialize Gradio chatbot with the correct type
16
  with gr.Blocks() as demo:
17
- gr.Markdown("## 🚀 AI Bandar -")
18
 
19
- chatbot = gr.Chatbot(type="messages") # Fix: Use 'messages' format
20
  user_input = gr.Textbox(label="Your Message")
21
  submit_button = gr.Button("Send")
22
 
@@ -24,6 +31,6 @@ with gr.Blocks() as demo:
24
  submit_button.click(chat_response, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
25
  user_input.submit(chat_response, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
26
 
27
- # Run the Gradio app
28
  if __name__ == "__main__":
29
- demo.launch(share=True) # 🚀 Allows sharing via public Gradio link
 
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
 
 
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
 
34
+ # Run on Hugging Face Spaces
35
  if __name__ == "__main__":
36
+ demo.launch()