Spaces:
Running
on
L4
Running
on
L4
Update app.py
Browse filesAdd initial app.py code
app.py
CHANGED
@@ -1,63 +1,203 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
|
|
|
|
4 |
"""
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
6 |
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
response = ""
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
top_p=top_p,
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
gr.Slider(
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
maximum=1.0,
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
step=0.05,
|
56 |
-
|
57 |
),
|
|
|
|
|
|
|
|
|
58 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
)
|
60 |
|
61 |
|
|
|
|
|
|
|
|
|
|
|
62 |
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
|
|
|
|
|
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 |
+
import time
|
10 |
+
|
11 |
+
MAX_MAX_NEW_TOKENS = 2048
|
12 |
+
DEFAULT_MAX_NEW_TOKENS = 1024
|
13 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
14 |
+
|
15 |
|
16 |
+
DESCRIPTION = """\
|
17 |
+
# Dorna-Llama3-8B-Instruct Chat
|
18 |
"""
|
19 |
+
|
20 |
+
PLACEHOLDER = """
|
21 |
+
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
|
22 |
+
<img src="https://avatars.githubusercontent.com/u/39557177?v=4" style="width: 80%; max-width: 550px; height: auto; opacity: 0.80; ">
|
23 |
+
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Dorna-Llama3-8B-Instruct</h1>
|
24 |
+
</div>
|
25 |
"""
|
|
|
26 |
|
27 |
+
custom_css = """
|
28 |
+
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn&display=swap');
|
29 |
|
30 |
+
body, .gradio-container, .gr-button, .gr-input, .gr-slider, .gr-dropdown, .gr-markdown {
|
31 |
+
font-family: 'Vazirmatn', sans-serif !important;
|
32 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
._button {
|
35 |
+
font-size: 20px;
|
36 |
+
}
|
|
|
|
|
37 |
|
38 |
+
pre, code {
|
39 |
+
direction: ltr !important;
|
40 |
+
unicode-bidi: plaintext !important;
|
41 |
+
}
|
42 |
+
"""
|
43 |
|
|
|
44 |
|
45 |
+
system_prompt = str(os.getenv("SYSTEM_PROMPT"))
|
46 |
+
|
47 |
+
|
48 |
+
def execution_time_calculator(start_time, log=True):
|
49 |
+
delta = time.time() - start_time
|
50 |
+
if log:
|
51 |
+
print("--- %s seconds ---" % (delta))
|
52 |
+
return delta
|
53 |
+
|
54 |
+
def token_per_second_calculator(tokens_count, time_delta):
|
55 |
+
return tokens_count/time_delta
|
56 |
+
|
57 |
+
if not torch.cuda.is_available():
|
58 |
+
DESCRIPTION = "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
|
59 |
+
|
60 |
+
|
61 |
+
if torch.cuda.is_available():
|
62 |
+
model_id = "PartAI/Dorna-Llama3-8B-Instruct"
|
63 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
|
64 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
65 |
+
|
66 |
+
generation_speed = 0
|
67 |
+
|
68 |
+
def get_generation_speed():
|
69 |
+
global generation_speed
|
70 |
+
|
71 |
+
return generation_speed
|
72 |
+
|
73 |
+
|
74 |
+
@spaces.GPU
|
75 |
+
def generate(
|
76 |
+
message: str,
|
77 |
+
chat_history: list[tuple[str, str]],
|
78 |
+
max_new_tokens: int = 1024,
|
79 |
+
temperature: float = 0.6,
|
80 |
+
top_p: float = 0.9,
|
81 |
+
top_k: int = 50,
|
82 |
+
repetition_penalty: float = 1.2,
|
83 |
+
do_sample: bool =True,
|
84 |
+
) -> Iterator[str]:
|
85 |
+
global generation_speed
|
86 |
+
global system_prompt
|
87 |
+
|
88 |
+
conversation = []
|
89 |
+
if system_prompt:
|
90 |
+
conversation.append({"role": "system", "content": system_prompt})
|
91 |
+
for user, assistant in chat_history:
|
92 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
93 |
+
conversation.append({"role": "user", "content": message})
|
94 |
+
|
95 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
|
96 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
97 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
98 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
99 |
+
input_ids = input_ids.to(model.device)
|
100 |
+
|
101 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
102 |
+
generate_kwargs = dict(
|
103 |
+
{"input_ids": input_ids},
|
104 |
+
streamer=streamer,
|
105 |
+
max_new_tokens=max_new_tokens,
|
106 |
+
do_sample=do_sample,
|
107 |
top_p=top_p,
|
108 |
+
top_k=top_k,
|
109 |
+
temperature=temperature,
|
110 |
+
num_beams=1,
|
111 |
+
repetition_penalty=repetition_penalty,
|
112 |
+
)
|
113 |
|
114 |
+
start_time = time.time()
|
115 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
116 |
+
t.start()
|
117 |
|
118 |
+
outputs = []
|
119 |
+
sum_tokens = 0
|
120 |
+
for text in streamer:
|
121 |
+
num_tokens = len(tokenizer.tokenize(text))
|
122 |
+
sum_tokens += num_tokens
|
123 |
+
|
124 |
+
outputs.append(text)
|
125 |
+
yield "".join(outputs)
|
126 |
+
|
127 |
+
time_delta = execution_time_calculator(start_time, log=False)
|
128 |
+
|
129 |
+
generation_speed = token_per_second_calculator(sum_tokens, time_delta)
|
130 |
+
|
131 |
+
print(f"generation_speed: {generation_speed}")
|
132 |
+
|
133 |
+
|
134 |
+
chatbot = gr.Chatbot(placeholder=PLACEHOLDER, scale=1, show_copy_button=True, height="68%", rtl=True) #, elem_classes=["chatbot"])
|
135 |
+
chat_input = gr.Textbox(show_label=False, lines=2, rtl=True, placeholder="ورودی", show_copy_button=True, scale=4)
|
136 |
+
submit_btn = gr.Button(variant="primary", value="ارسال", size="sm", scale=1, elem_classes=["_button"])
|
137 |
+
|
138 |
+
|
139 |
+
chat_interface = gr.ChatInterface(
|
140 |
+
fn=generate,
|
141 |
+
additional_inputs_accordion=gr.Accordion(label="ورودیهای اضافی", open=False),
|
142 |
additional_inputs=[
|
|
|
|
|
|
|
143 |
gr.Slider(
|
144 |
+
label="حداکثر تعداد توکن ها",
|
145 |
+
minimum=1,
|
146 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
147 |
+
step=1,
|
148 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
149 |
+
),
|
150 |
+
gr.Slider(
|
151 |
+
label="Temperature",
|
152 |
+
minimum=0.01,
|
153 |
+
maximum=4.0,
|
154 |
+
step=0.01,
|
155 |
+
value=0.6,
|
156 |
+
),
|
157 |
+
gr.Slider(
|
158 |
+
label="Top-p",
|
159 |
+
minimum=0.05,
|
160 |
maximum=1.0,
|
161 |
+
step=0.01,
|
162 |
+
value=0.65,
|
163 |
+
),
|
164 |
+
gr.Slider(
|
165 |
+
label="Top-k",
|
166 |
+
minimum=1,
|
167 |
+
maximum=1000,
|
168 |
+
step=1,
|
169 |
+
value=40,
|
170 |
+
),
|
171 |
+
gr.Slider(
|
172 |
+
label="جریمه تکرار",
|
173 |
+
minimum=1.0,
|
174 |
+
maximum=2.0,
|
175 |
step=0.05,
|
176 |
+
value=1.2,
|
177 |
),
|
178 |
+
gr.Dropdown(
|
179 |
+
label="نمونهگیری",
|
180 |
+
choices=[False, True],
|
181 |
+
value=True)
|
182 |
],
|
183 |
+
stop_btn="توقف",
|
184 |
+
chatbot=chatbot,
|
185 |
+
textbox=chat_input,
|
186 |
+
submit_btn=submit_btn,
|
187 |
+
retry_btn="🔄 تلاش مجدد",
|
188 |
+
undo_btn="↩️ بازگشت",
|
189 |
+
clear_btn="🗑️ پاک کردن",
|
190 |
+
title="درنا، محصول مرکز تحقیقات هوش مصنوعی پارت"
|
191 |
)
|
192 |
|
193 |
|
194 |
+
with gr.Blocks(css=custom_css, fill_height=False) as demo:
|
195 |
+
gr.Markdown(DESCRIPTION)
|
196 |
+
chat_interface.render()
|
197 |
+
|
198 |
+
|
199 |
if __name__ == "__main__":
|
200 |
+
demo.queue(max_size=20).launch()
|
201 |
+
|
202 |
+
|
203 |
+
|