Hev832 commited on
Commit
e6b1e56
·
verified ·
1 Parent(s): e398ade

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -68
app.py CHANGED
@@ -1,74 +1,38 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
-
6
-
7
- def respond(
8
- message,
9
- history: list[tuple[str, str]],
10
- system_message,
11
- max_tokens,
12
- temperature,
13
- top_p,
14
- ):
15
- messages = [{"role": "system", "content": system_message}]
16
-
17
- for val in history:
18
- if val[0]:
19
- messages.append({"role": "user", "content": val[0]})
20
- if val[1]:
21
- messages.append({"role": "assistant", "content": val[1]})
22
-
23
- messages.append({"role": "user", "content": message})
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
- with gr.Column(scale=0.8):
48
- chatbot = gr.Chatbot()
49
- with gr.Column(scale=0.2):
50
- max_tokens = gr.Slider(
51
- minimum=1, maximum=512, step=1, value=128, label="Max Tokens"
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
- def user_interaction(message, history, system_message, max_tokens, temperature, top_p):
63
- bot_message = next(respond(message, history, system_message, max_tokens, temperature, top_p))
64
- history.append((message, bot_message))
65
- return history, history
66
 
67
- user_input.submit(
68
- user_interaction,
69
- inputs=[user_input, chat_history, system_message, max_tokens, temperature, top_p],
70
- outputs=[chatbot, chat_history],
71
- )
72
 
73
- if __name__ == "__main__":
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()