Spaces:
Running
on
Zero
Running
on
Zero
feat: 换成测试模型
Browse files- app.py +66 -24
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,11 +1,14 @@
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
4 |
|
5 |
-
""
|
6 |
-
|
7 |
-
""
|
8 |
-
|
9 |
|
10 |
@spaces.GPU
|
11 |
def respond(
|
@@ -16,35 +19,73 @@ def respond(
|
|
16 |
temperature,
|
17 |
top_p,
|
18 |
):
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
if val[0]:
|
23 |
-
messages.append({"role": "user", "content": val[0]})
|
24 |
-
if val[1]:
|
25 |
-
messages.append({"role": "assistant", "content": val[1]})
|
26 |
|
27 |
-
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
"""
|
44 |
-
|
|
|
|
|
|
|
|
|
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"),
|
@@ -57,6 +98,7 @@ demo = gr.ChatInterface(
|
|
57 |
label="Top-p (nucleus sampling)",
|
58 |
),
|
59 |
],
|
|
|
60 |
)
|
61 |
|
62 |
|
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
+
import torch
|
6 |
+
import subprocess
|
7 |
|
8 |
+
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
9 |
+
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct", trust_remote_code=True)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained("deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct", trust_remote_code=True, torch_dtype=torch.bfloat16).cuda()
|
12 |
|
13 |
@spaces.GPU
|
14 |
def respond(
|
|
|
19 |
temperature,
|
20 |
top_p,
|
21 |
):
|
22 |
+
if len(message) < 1:
|
23 |
+
message = "write a quick sort algorithm in python."
|
24 |
+
|
25 |
+
messages = [
|
26 |
+
{ 'role': 'user', 'content': message }
|
27 |
+
]
|
28 |
|
29 |
+
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
outputs = model.generate(inputs, max_new_tokens=max_tokens, do_sample=True, temperature=temperature, top_k=50, top_p=top_p, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
|
32 |
|
33 |
+
return tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
|
34 |
+
|
35 |
+
"""
|
36 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
37 |
+
"""
|
38 |
+
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
39 |
|
40 |
+
# @spaces.GPU
|
41 |
+
# def respond(
|
42 |
+
# message,
|
43 |
+
# history: list[tuple[str, str]],
|
44 |
+
# system_message,
|
45 |
+
# max_tokens,
|
46 |
+
# temperature,
|
47 |
+
# top_p,
|
48 |
+
# ):
|
49 |
+
# messages = [{"role": "system", "content": system_message}]
|
50 |
|
51 |
+
# for val in history:
|
52 |
+
# if val[0]:
|
53 |
+
# messages.append({"role": "user", "content": val[0]})
|
54 |
+
# if val[1]:
|
55 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
56 |
|
57 |
+
# if len(message) < 1:
|
58 |
+
# message = "write a quick sort algorithm in python."
|
59 |
+
|
60 |
+
# messages.append({"role": "user", "content": message})
|
61 |
+
|
62 |
+
# response = ""
|
63 |
+
|
64 |
+
# for message in client.chat_completion(
|
65 |
+
# messages,
|
66 |
+
# max_tokens=max_tokens,
|
67 |
+
# stream=True,
|
68 |
+
# temperature=temperature,
|
69 |
+
# top_p=top_p,
|
70 |
+
# ):
|
71 |
+
# token = message.choices[0].delta.content
|
72 |
+
|
73 |
+
# response += token
|
74 |
+
# yield response
|
75 |
+
|
76 |
+
"""
|
77 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/main/docs/gradio/chatinterface
|
78 |
"""
|
79 |
+
|
80 |
+
css = """
|
81 |
+
#msg_input {
|
82 |
+
flex-grow: 7;
|
83 |
+
}
|
84 |
"""
|
85 |
+
|
86 |
demo = gr.ChatInterface(
|
87 |
+
fn=respond,
|
88 |
+
textbox=gr.Textbox(elem_id="msg_input", placeholder="write a quick sort algorithm in python."),
|
89 |
additional_inputs=[
|
90 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
91 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
|
|
98 |
label="Top-p (nucleus sampling)",
|
99 |
),
|
100 |
],
|
101 |
+
css=css,
|
102 |
)
|
103 |
|
104 |
|
requirements.txt
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
huggingface_hub==0.22.2
|
|
|
|
1 |
+
huggingface_hub==0.22.2
|
2 |
+
transformers
|