Spaces:
Paused
Paused
Create app_chat.py
Browse files- app_chat.py +128 -0
app_chat.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from threading import Thread
|
3 |
+
from typing import Iterator
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import spaces
|
7 |
+
import torch
|
8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
9 |
+
|
10 |
+
MAX_MAX_NEW_TOKENS = 1024
|
11 |
+
DEFAULT_MAX_NEW_TOKENS = 512
|
12 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
13 |
+
|
14 |
+
DESCRIPTION = """\
|
15 |
+
# Hymba-1.5B chat
|
16 |
+
|
17 |
+
"""
|
18 |
+
|
19 |
+
model_id = "nvidia/Hymba-1.5B-Instruct"
|
20 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, trust_remote_code=True)
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
22 |
+
#tokenizer.use_default_system_prompt = False
|
23 |
+
|
24 |
+
|
25 |
+
@spaces.GPU
|
26 |
+
def generate(
|
27 |
+
message: str,
|
28 |
+
chat_history: list[dict],
|
29 |
+
system_prompt: str = "",
|
30 |
+
max_new_tokens: int = 1024,
|
31 |
+
temperature: float = 0.6,
|
32 |
+
top_p: float = 0.9,
|
33 |
+
top_k: int = 50,
|
34 |
+
repetition_penalty: float = 1.2,
|
35 |
+
) -> Iterator[str]:
|
36 |
+
conversation = []
|
37 |
+
if system_prompt:
|
38 |
+
conversation.append({"role": "System", "content": system_prompt})
|
39 |
+
conversation += chat_history
|
40 |
+
conversation.append({"role": "User", "content": message})
|
41 |
+
|
42 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
|
43 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
44 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
45 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
46 |
+
input_ids = input_ids.to(model.device)
|
47 |
+
|
48 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=False)
|
49 |
+
generate_kwargs = dict(
|
50 |
+
{"input_ids": input_ids},
|
51 |
+
streamer=streamer,
|
52 |
+
max_new_tokens=max_new_tokens,
|
53 |
+
do_sample=True,
|
54 |
+
top_p=top_p,
|
55 |
+
top_k=top_k,
|
56 |
+
temperature=temperature,
|
57 |
+
num_beams=1,
|
58 |
+
repetition_penalty=repetition_penalty,
|
59 |
+
)
|
60 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
61 |
+
t.start()
|
62 |
+
|
63 |
+
outputs = []
|
64 |
+
for text in streamer:
|
65 |
+
outputs.append(text)
|
66 |
+
yield "".join(outputs)
|
67 |
+
|
68 |
+
|
69 |
+
chat_interface = gr.ChatInterface(
|
70 |
+
fn=generate,
|
71 |
+
additional_inputs=[
|
72 |
+
gr.Textbox(label="System prompt", lines=6),
|
73 |
+
gr.Slider(
|
74 |
+
label="Max new tokens",
|
75 |
+
minimum=1,
|
76 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
77 |
+
step=1,
|
78 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
79 |
+
),
|
80 |
+
gr.Slider(
|
81 |
+
label="Temperature",
|
82 |
+
minimum=0.1,
|
83 |
+
maximum=4.0,
|
84 |
+
step=0.1,
|
85 |
+
value=0.6,
|
86 |
+
),
|
87 |
+
gr.Slider(
|
88 |
+
label="Top-p (nucleus sampling)",
|
89 |
+
minimum=0.05,
|
90 |
+
maximum=1.0,
|
91 |
+
step=0.05,
|
92 |
+
value=0.9,
|
93 |
+
),
|
94 |
+
gr.Slider(
|
95 |
+
label="Top-k",
|
96 |
+
minimum=1,
|
97 |
+
maximum=1000,
|
98 |
+
step=1,
|
99 |
+
value=50,
|
100 |
+
),
|
101 |
+
gr.Slider(
|
102 |
+
label="Repetition penalty",
|
103 |
+
minimum=1.0,
|
104 |
+
maximum=2.0,
|
105 |
+
step=0.05,
|
106 |
+
value=1.2,
|
107 |
+
),
|
108 |
+
],
|
109 |
+
stop_btn=None,
|
110 |
+
examples=[
|
111 |
+
["Hello there! How are you doing?"],
|
112 |
+
["Can you explain briefly to me what is the Python programming language?"],
|
113 |
+
["Explain the plot of Cinderella in a sentence."],
|
114 |
+
["How many hours does it take a man to eat a Helicopter?"],
|
115 |
+
["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
|
116 |
+
],
|
117 |
+
cache_examples=False,
|
118 |
+
type="messages",
|
119 |
+
)
|
120 |
+
|
121 |
+
with gr.Blocks(css_paths="style.css", fill_height=True) as demo:
|
122 |
+
gr.Markdown(DESCRIPTION)
|
123 |
+
# gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
124 |
+
chat_interface.render()
|
125 |
+
gr.Markdown(LICENSE)
|
126 |
+
|
127 |
+
if __name__ == "__main__":
|
128 |
+
demo.queue(max_size=20).launch()
|