Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
response = ""
|
26 |
-
for message in client.chat_completion(
|
27 |
-
messages,
|
28 |
-
max_tokens=max_tokens,
|
29 |
-
stream=True,
|
30 |
-
temperature=temperature,
|
31 |
-
top_p=top_p,
|
32 |
-
):
|
33 |
-
token = message.choices[0].delta.content
|
34 |
-
response += token
|
35 |
-
yield response
|
36 |
-
|
37 |
-
|
38 |
with gr.Blocks() as demo:
|
39 |
-
system_message = gr.Textbox(
|
40 |
-
label="System Message",
|
41 |
-
value="You are a helpful assistant.",
|
42 |
-
lines=2,
|
43 |
-
)
|
44 |
-
chat_history = gr.State([])
|
45 |
-
|
46 |
with gr.Row():
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
)
|
53 |
-
temperature = gr.Slider(
|
54 |
-
minimum=0, maximum=1, step=0.01, value=0.7, label="Temperature"
|
55 |
-
)
|
56 |
-
top_p = gr.Slider(
|
57 |
-
minimum=0, maximum=1, step=0.01, value=1, label="Top-p"
|
58 |
-
)
|
59 |
-
|
60 |
-
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
history.append((message, bot_message))
|
65 |
-
return history, history
|
66 |
|
67 |
-
|
68 |
-
user_interaction,
|
69 |
-
inputs=[user_input, chat_history, system_message, max_tokens, temperature, top_p],
|
70 |
-
outputs=[chatbot, chat_history],
|
71 |
-
)
|
72 |
|
73 |
-
|
74 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the Hugging Face model and tokenizer
|
6 |
+
model_name = "HuggingFaceH4/zephyr-7b-beta"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
|
9 |
+
|
10 |
+
# Define custom system content
|
11 |
+
custom_system_content = """
|
12 |
+
You are a helpful chatbot designed to assist users with any questions or tasks they may have.
|
13 |
+
Please provide thoughtful and concise responses.
|
14 |
+
"""
|
15 |
+
|
16 |
+
# Function to generate chatbot responses
|
17 |
+
def chatbot_response(user_input):
|
18 |
+
inputs = tokenizer(custom_system_content + user_input, return_tensors="pt").to("cuda")
|
19 |
+
outputs = model.generate(**inputs, max_length=256)
|
20 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
21 |
+
return response[len(custom_system_content):]
|
22 |
+
|
23 |
+
# Gradio Blocks UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
with gr.Row():
|
26 |
+
gr.Markdown("<h2>Zephyr-7B Chatbot</h2>")
|
27 |
+
|
28 |
+
with gr.Row():
|
29 |
+
with gr.Column():
|
30 |
+
user_input = gr.Textbox(label="Your message", placeholder="Type your message here...")
|
31 |
+
chatbot_output = gr.Textbox(label="Chatbot Response", placeholder="Chatbot will respond here...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
with gr.Column():
|
34 |
+
submit_btn = gr.Button("Send")
|
|
|
|
|
35 |
|
36 |
+
submit_btn.click(fn=chatbot_response, inputs=user_input, outputs=chatbot_output)
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
demo.launch()
|
|