beyoru commited on
Commit
afec331
·
verified ·
1 Parent(s): c0003ca

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ from threading import Thread
3
+
4
+ import torch
5
+ import spaces
6
+ import gradio as gr
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextIteratorStreamer
8
+
9
+ #subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
10
+
11
+ MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
12
+ CHAT_TEMPLATE = "َAuto"
13
+ MODEL_NAME = MODEL_ID.split("/")[-1]
14
+ CONTEXT_LENGTH = 16000
15
+
16
+
17
+
18
+ latex_delimiters_set = [{
19
+ "left": "\\(",
20
+ "right": "\\)",
21
+ "display": False
22
+ }, {
23
+ "left": "\\begin{equation}",
24
+ "right": "\\end{equation}",
25
+ "display": True
26
+ }, {
27
+ "left": "\\begin{align}",
28
+ "right": "\\end{align}",
29
+ "display": True
30
+ }, {
31
+ "left": "\\begin{alignat}",
32
+ "right": "\\end{alignat}",
33
+ "display": True
34
+ }, {
35
+ "left": "\\begin{gather}",
36
+ "right": "\\end{gather}",
37
+ "display": True
38
+ }, {
39
+ "left": "\\begin{CD}",
40
+ "right": "\\end{CD}",
41
+ "display": True
42
+ }, {
43
+ "left": "\\[",
44
+ "right": "\\]",
45
+ "display": True
46
+ }]
47
+
48
+
49
+ def predict(message, history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p):
50
+ # Format history with a given chat template
51
+
52
+
53
+ stop_tokens = ["<|endoftext|>", "<|im_end|>","|im_end|"]
54
+ instruction = '<|im_start|>system\n' + system_prompt + '\n<|im_end|>\n'
55
+ for user, assistant in history:
56
+ instruction += f'<|im_start|>user\n{user}\n<|im_end|>\n<|im_start|>assistant\n{assistant}\n<|im_end|>\n'
57
+ instruction += f'<|im_start|>user\n{message}\n<|im_end|>\n<|im_start|>assistant\n'
58
+
59
+ print(instruction)
60
+
61
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=False)
62
+ enc = tokenizer(instruction, return_tensors="pt", padding=True, truncation=True)
63
+ input_ids, attention_mask = enc.input_ids, enc.attention_mask
64
+
65
+ if input_ids.shape[1] > CONTEXT_LENGTH:
66
+ input_ids = input_ids[:, -CONTEXT_LENGTH:]
67
+ attention_mask = attention_mask[:, -CONTEXT_LENGTH:]
68
+
69
+ generate_kwargs = dict(
70
+ input_ids=input_ids.to(device),
71
+ attention_mask=attention_mask.to(device),
72
+ streamer=streamer,
73
+ do_sample=True,
74
+ temperature=temperature,
75
+ max_new_tokens=max_new_tokens,
76
+ top_k=top_k,
77
+ repetition_penalty=repetition_penalty,
78
+ top_p=top_p
79
+ )
80
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
81
+ t.start()
82
+ outputs = []
83
+ for new_token in streamer:
84
+ outputs.append(new_token)
85
+ if new_token in stop_tokens:
86
+
87
+ break
88
+ yield "".join(outputs)
89
+
90
+
91
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
92
+ model = AutoModelForCausalLM.from_pretrained(
93
+ MODEL_ID,
94
+ device_map="auto",
95
+ )
96
+
97
+ # Create Gradio interface
98
+ gr.ChatInterface(
99
+ predict,
100
+
101
+ additional_inputs_accordion=gr.Accordion(label="Parameters", open=False),
102
+ additional_inputs=[
103
+ gr.Textbox("You are a useful assistant. first recognize user request and then reply carfuly and thinking", label="System prompt"),
104
+ gr.Slider(0, 1, 0.6, label="Temperature"),
105
+ gr.Slider(0, 32000, 10000, label="Max new tokens"),
106
+ gr.Slider(1, 80, 40, label="Top K sampling"),
107
+ gr.Slider(0, 2, 1.1, label="Repetition penalty"),
108
+ gr.Slider(0, 1, 0.95, label="Top P sampling"),
109
+ ],
110
+ ).queue().launch()