Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,85 @@
|
|
1 |
-
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
for val in history:
|
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 |
-
response = ""
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
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=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
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 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from threading import Thread
|
|
|
2 |
|
3 |
+
import gradio as gr
|
4 |
+
import spaces
|
5 |
+
import torch
|
6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
7 |
|
8 |
+
MAX_NEW_TOKENS = 2048
|
9 |
+
MODEL_NAME = "Azure99/Blossom-V6-7B"
|
10 |
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.bfloat16, device_map="auto")
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
def get_input_ids(inst, history):
|
16 |
+
conversation = []
|
17 |
+
for user, assistant in history:
|
18 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
19 |
+
conversation.append({"role": "user", "content": inst})
|
20 |
+
return tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
21 |
|
|
|
22 |
|
23 |
+
@spaces.GPU
|
24 |
+
def chat(inst, history, temperature, top_p, repetition_penalty):
|
25 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
26 |
+
input_ids = get_input_ids(inst, history)
|
27 |
+
generation_kwargs = dict(input_ids=input_ids,
|
28 |
+
streamer=streamer, do_sample=True, max_new_tokens=MAX_NEW_TOKENS,
|
29 |
+
temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty)
|
|
|
30 |
|
31 |
+
Thread(target=model.generate, kwargs=generation_kwargs).start()
|
|
|
32 |
|
33 |
+
outputs = ""
|
34 |
+
for new_text in streamer:
|
35 |
+
outputs += new_text
|
36 |
+
yield outputs
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
additional_inputs = [
|
40 |
+
gr.Slider(
|
41 |
+
label="Temperature",
|
42 |
+
value=0.5,
|
43 |
+
minimum=0.0,
|
44 |
+
maximum=1.0,
|
45 |
+
step=0.05,
|
46 |
+
interactive=True,
|
47 |
+
info="Controls randomness in choosing words.",
|
48 |
+
),
|
49 |
+
gr.Slider(
|
50 |
+
label="Top-P",
|
51 |
+
value=0.85,
|
52 |
+
minimum=0.0,
|
53 |
+
maximum=1.0,
|
54 |
+
step=0.05,
|
55 |
+
interactive=True,
|
56 |
+
info="Picks words until their combined probability is at least top_p.",
|
57 |
+
),
|
58 |
+
gr.Slider(
|
59 |
+
label="Repetition penalty",
|
60 |
+
value=1.05,
|
61 |
+
minimum=1.0,
|
62 |
+
maximum=1.2,
|
63 |
+
step=0.01,
|
64 |
+
interactive=True,
|
65 |
+
info="Repetition Penalty: Controls how much repetition is penalized.",
|
66 |
+
)
|
67 |
+
]
|
68 |
|
69 |
+
gr.ChatInterface(chat,
|
70 |
+
chatbot=gr.Chatbot(show_label=False, height=500, show_copy_button=True, render_markdown=True),
|
71 |
+
textbox=gr.Textbox(placeholder="", container=False, scale=7),
|
72 |
+
title="Blossom-V6-7B Demo",
|
73 |
+
description='Hello, I am Blossom, an open source conversational large language model.🌠'
|
74 |
+
'<a href="https://github.com/Azure99/BlossomLM">GitHub</a>',
|
75 |
+
theme="soft",
|
76 |
+
examples=[["Hello"], ["What is MBTI"], ["用Python实现二分查找"],
|
77 |
+
["为switch写一篇小红书种草文案,带上emoji"]],
|
78 |
+
cache_examples=False,
|
79 |
+
additional_inputs=additional_inputs,
|
80 |
+
additional_inputs_accordion=gr.Accordion(label="Config", open=True),
|
81 |
+
clear_btn="🗑️Clear",
|
82 |
+
undo_btn="↩️Undo",
|
83 |
+
retry_btn="🔄Retry",
|
84 |
+
submit_btn="➡️Submit",
|
85 |
+
).queue().launch()
|