Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
chatbot_pipeline = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct", max_new_tokens=100)
|
| 6 |
|
| 7 |
-
# Chatbot function
|
| 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 |
-
#
|
| 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 |
-
# 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 |
|