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