admin commited on
Commit
9d250e2
·
1 Parent(s): 9269410
Files changed (1) hide show
  1. app.py +69 -48
app.py CHANGED
@@ -1,64 +1,85 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
4
- """
5
- 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
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
9
 
10
- def respond(
 
11
  message,
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
- messages.append({"role": "user", "content": message})
27
 
28
- response = ""
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
 
34
  temperature=temperature,
 
 
 
35
  top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
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
  if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
  import gradio as gr
3
+ from threading import Thread
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
5
 
 
 
 
 
6
 
7
+ MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"
8
+ MODEL_NAME = MODEL_ID.split("/")[-1]
9
+ CONTEXT_LENGTH = 16000
10
+ DESCRIPTION = f"This is {MODEL_NAME} model designed for testing thinking for general AI tasks. <br>当前仅提供 HuggingFace 版部署实例,有算力的可自行克隆至本地或复刻至购买了 GPU 环境的账号测试"
11
 
12
+
13
+ def predict(
14
  message,
15
+ history,
16
+ system_prompt,
 
17
  temperature,
18
+ max_new_tokens,
19
+ top_k,
20
+ repetition_penalty,
21
  top_p,
22
  ):
23
+ # Format history with a given chat template
24
+ stop_tokens = ["<|endoftext|>", "<|im_end|>", "|im_end|"]
25
+ instruction = "<|im_start|>system\n" + system_prompt + "\n<|im_end|>\n"
26
+ for user, assistant in history:
27
+ instruction += f"<|im_start|>user\n{user}\n<|im_end|>\n<|im_start|>assistant\n{assistant}\n<|im_end|>\n"
 
 
 
 
28
 
29
+ instruction += f"<|im_start|>user\n{message}\n<|im_end|>\n<|im_start|>assistant\n"
30
+ print(instruction)
31
+ streamer = TextIteratorStreamer(
32
+ tokenizer,
33
+ skip_prompt=True,
34
+ skip_special_tokens=True,
35
+ )
36
+ enc = tokenizer(instruction, return_tensors="pt", padding=True, truncation=True)
37
+ input_ids, attention_mask = enc.input_ids, enc.attention_mask
38
+ if input_ids.shape[1] > CONTEXT_LENGTH:
39
+ input_ids = input_ids[:, -CONTEXT_LENGTH:]
40
+ attention_mask = attention_mask[:, -CONTEXT_LENGTH:]
41
 
42
+ generate_kwargs = dict(
43
+ input_ids=input_ids.to(device),
44
+ attention_mask=attention_mask.to(device),
45
+ streamer=streamer,
46
+ do_sample=True,
47
  temperature=temperature,
48
+ max_new_tokens=max_new_tokens,
49
+ top_k=top_k,
50
+ repetition_penalty=repetition_penalty,
51
  top_p=top_p,
52
+ )
53
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
54
+ t.start()
55
+ outputs = []
56
+ for new_token in streamer:
57
+ outputs.append(new_token)
58
+ if new_token in stop_tokens:
59
+ break
60
 
61
+ yield "".join(outputs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
 
64
  if __name__ == "__main__":
65
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
66
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
67
+ model = AutoModelForCausalLM.from_pretrained(MODEL_ID, device_map="auto")
68
+ # Create Gradio interface
69
+ gr.ChatInterface(
70
+ predict,
71
+ title=f"{MODEL_NAME} 部署实例",
72
+ description=DESCRIPTION,
73
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False),
74
+ additional_inputs=[
75
+ gr.Textbox(
76
+ "You are a useful assistant. first recognize user request and then reply carfuly and thinking",
77
+ label="System prompt",
78
+ ),
79
+ gr.Slider(0, 1, 0.6, label="Temperature"),
80
+ gr.Slider(0, 32000, 10000, label="Max new tokens"),
81
+ gr.Slider(1, 80, 40, label="Top K sampling"),
82
+ gr.Slider(0, 2, 1.1, label="Repetition penalty"),
83
+ gr.Slider(0, 1, 0.95, label="Top P sampling"),
84
+ ],
85
+ ).queue().launch()