Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,34 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
@@ -15,34 +38,48 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
18 |
-
messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
|
|
|
|
40 |
yield response
|
41 |
|
|
|
42 |
|
43 |
-
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
@@ -50,15 +87,10 @@ demo = gr.ChatInterface(
|
|
50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
),
|
59 |
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
+
# app.py
|
2 |
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from threading import Thread
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
6 |
|
7 |
+
# Choose any chat model with a chat template; Zephyr works well:
|
8 |
+
MODEL_NAME = "google/gemma-3-270m"
|
|
|
|
|
9 |
|
10 |
+
# Load model + tokenizer
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=True)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
13 |
+
MODEL_NAME,
|
14 |
+
torch_dtype="auto",
|
15 |
+
device_map="auto",
|
16 |
+
)
|
17 |
+
|
18 |
+
def build_chat(system_message: str, history: list[tuple[str, str]], user_message: str):
|
19 |
+
"""Convert Gradio history into a list of chat messages for apply_chat_template."""
|
20 |
+
messages = []
|
21 |
+
if system_message:
|
22 |
+
messages.append({"role": "system", "content": system_message})
|
23 |
+
|
24 |
+
for u, a in history:
|
25 |
+
if u:
|
26 |
+
messages.append({"role": "user", "content": u})
|
27 |
+
if a:
|
28 |
+
messages.append({"role": "assistant", "content": a})
|
29 |
+
|
30 |
+
messages.append({"role": "user", "content": user_message})
|
31 |
+
return messages
|
32 |
|
33 |
def respond(
|
34 |
message,
|
|
|
38 |
temperature,
|
39 |
top_p,
|
40 |
):
|
41 |
+
# 1) Build chat messages and tokenize using the model's chat template
|
42 |
+
messages = build_chat(system_message, history, message)
|
43 |
+
|
44 |
+
inputs = tokenizer.apply_chat_template(
|
45 |
+
messages,
|
46 |
+
add_generation_prompt=True,
|
47 |
+
tokenize=True,
|
48 |
+
return_tensors="pt",
|
49 |
+
)
|
50 |
|
51 |
+
inputs = inputs.to(model.device)
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
# 2) Stream generation
|
54 |
+
streamer = TextIteratorStreamer(
|
55 |
+
tokenizer,
|
56 |
+
skip_prompt=True,
|
57 |
+
skip_special_tokens=True,
|
58 |
+
)
|
59 |
|
60 |
+
gen_kwargs = dict(
|
61 |
+
input_ids=inputs,
|
62 |
+
max_new_tokens=int(max_tokens),
|
63 |
+
do_sample=True,
|
64 |
+
temperature=float(temperature),
|
65 |
+
top_p=float(top_p),
|
66 |
+
eos_token_id=tokenizer.eos_token_id,
|
67 |
+
pad_token_id=tokenizer.eos_token_id,
|
68 |
+
streamer=streamer,
|
69 |
+
)
|
70 |
|
71 |
+
# Run generate() in a background thread while we yield chunks
|
72 |
+
thread = Thread(target=model.generate, kwargs=gen_kwargs)
|
73 |
+
thread.start()
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
response = ""
|
76 |
+
for new_text in streamer:
|
77 |
+
response += new_text
|
78 |
yield response
|
79 |
|
80 |
+
thread.join()
|
81 |
|
82 |
+
# Gradio UI (same controls as your example)
|
|
|
|
|
83 |
demo = gr.ChatInterface(
|
84 |
respond,
|
85 |
additional_inputs=[
|
|
|
87 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
88 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
89 |
gr.Slider(
|
90 |
+
minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
|
|
|
|
|
|
|
|
|
91 |
),
|
92 |
],
|
93 |
)
|
94 |
|
|
|
95 |
if __name__ == "__main__":
|
96 |
demo.launch()
|