File size: 1,403 Bytes
74bca27 e6b1e56 de82ee9 e6b1e56 de82ee9 e6b1e56 e398ade e6b1e56 de82ee9 e398ade e6b1e56 e398ade e6b1e56 74bca27 e6b1e56 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# Load the Hugging Face model and tokenizer
model_name = "HuggingFaceH4/zephyr-7b-beta"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
# Define custom system content
custom_system_content = """
You are a helpful chatbot designed to assist users with any questions or tasks they may have.
Please provide thoughtful and concise responses.
"""
# Function to generate chatbot responses
def chatbot_response(user_input):
inputs = tokenizer(custom_system_content + user_input, return_tensors="pt")
outputs = model.generate(**inputs, max_length=256)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response[len(custom_system_content):]
# Gradio Blocks UI
with gr.Blocks() as demo:
with gr.Row():
gr.Markdown("<h2>Zephyr-7B Chatbot</h2>")
with gr.Row():
with gr.Column():
user_input = gr.Textbox(label="Your message", placeholder="Type your message here...")
chatbot_output = gr.Chatbot(label="Chatbot Response", placeholder="Chatbot will respond here...")
with gr.Column():
submit_btn = gr.Button("Send")
submit_btn.click(fn=chatbot_response, inputs=user_input, outputs=chatbot_output)
demo.launch()
|