letao670982 commited on
Commit
578de86
1 Parent(s): 0a8b2c6

Upload testapp.py

Browse files
Files changed (1) hide show
  1. testapp.py +138 -0
testapp.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+ MAX_MAX_NEW_TOKENS = 1024
11
+ DEFAULT_MAX_NEW_TOKENS = 512
12
+ total_count=0
13
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "2048"))
14
+
15
+ DESCRIPTION = """\
16
+ # DeepSeek-1.3B-Chat
17
+ """
18
+
19
+ if not torch.cuda.is_available():
20
+ DESCRIPTION += "\n<p>Running on CPU 🥶.</p>"
21
+ model_id = "deepseek-ai/deepseek-coder-1.3b-instruct"
22
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32, device_map="auto")
23
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
24
+
25
+
26
+ if torch.cuda.is_available():
27
+ model_id = "deepseek-ai/deepseek-coder-1.3b-instruct"
28
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
29
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
30
+ tokenizer.use_default_system_prompt = False
31
+
32
+
33
+
34
+ #@spaces.GPU
35
+ def generate(
36
+ message: str,
37
+ chat_history: list[tuple[str, str]],
38
+ system_prompt: str,
39
+ max_new_tokens: int = 1024,
40
+ temperature: float = 0.6,
41
+ top_p: float = 0.9,
42
+ top_k: int = 50,
43
+ repetition_penalty: float = 1,
44
+ ) -> Iterator[str]:
45
+ global total_count
46
+ total_count += 1
47
+ print(total_count)
48
+ if total_count % 50 == 0 :
49
+ os.system("nvidia-smi")
50
+ conversation = []
51
+ if system_prompt:
52
+ conversation.append({"role": "system", "content": system_prompt})
53
+ for user, assistant in chat_history:
54
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
55
+ conversation.append({"role": "user", "content": message})
56
+
57
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt", add_generation_prompt=True)
58
+ if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
59
+ input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
60
+ gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
61
+ input_ids = input_ids.to(model.device)
62
+
63
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
64
+ generate_kwargs = dict(
65
+ {"input_ids": input_ids},
66
+ streamer=streamer,
67
+ max_new_tokens=max_new_tokens,
68
+ do_sample=False,
69
+ top_p=top_p,
70
+ top_k=top_k,
71
+ num_beams=1,
72
+ # temperature=temperature,
73
+ repetition_penalty=repetition_penalty,
74
+ eos_token_id=32021
75
+ )
76
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
77
+ t.start()
78
+
79
+ outputs = []
80
+ for text in streamer:
81
+ outputs.append(text)
82
+ yield "".join(outputs).replace("<|EOT|>","")
83
+
84
+
85
+ chat_interface = gr.ChatInterface(
86
+ fn=generate,
87
+ additional_inputs=[
88
+ gr.Textbox(label="System prompt", lines=6),
89
+ gr.Slider(
90
+ label="Max new tokens",
91
+ minimum=1,
92
+ maximum=MAX_MAX_NEW_TOKENS,
93
+ step=1,
94
+ value=DEFAULT_MAX_NEW_TOKENS,
95
+ ),
96
+ # gr.Slider(
97
+ # label="Temperature",
98
+ # minimum=0,
99
+ # maximum=4.0,
100
+ # step=0.1,
101
+ # value=0,
102
+ # ),
103
+ gr.Slider(
104
+ label="Top-p (nucleus sampling)",
105
+ minimum=0.05,
106
+ maximum=1.0,
107
+ step=0.05,
108
+ value=0.9,
109
+ ),
110
+ gr.Slider(
111
+ label="Top-k",
112
+ minimum=1,
113
+ maximum=1000,
114
+ step=1,
115
+ value=50,
116
+ ),
117
+ gr.Slider(
118
+ label="Repetition penalty",
119
+ minimum=1.0,
120
+ maximum=2.0,
121
+ step=0.05,
122
+ value=1,
123
+ ),
124
+ ],
125
+ stop_btn=None,
126
+ examples=[
127
+ ["implement snake game using pygame"],
128
+ ["Can you explain briefly to me what is the Python programming language?"],
129
+ ["write a program to find the factorial of a number"],
130
+ ],
131
+ )
132
+
133
+ with gr.Blocks() as demo:
134
+ gr.Markdown(DESCRIPTION)
135
+ chat_interface.render()
136
+
137
+ if __name__ == "__main__":
138
+ demo.queue(max_size=5).launch()