rombodawg commited on
Commit
ec9bdb0
1 Parent(s): 60d5d0e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +170 -0
app.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import spaces
4
+ import torch
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
6
+ import gradio as gr
7
+
8
+ from threading import Thread
9
+
10
+ MODEL = "rombodawg/Rombos-LLM-V2.6-Qwen-14b"
11
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
12
+
13
+ TITLE = """
14
+ <h1><center>Rombos-LLM-V2.6-Qwen-14b</center></h1>
15
+ <center>
16
+ <p>The model is licensed under apache 2.0</p>
17
+ </center>
18
+ """
19
+
20
+
21
+ PLACEHOLDER = """
22
+ <center>
23
+ <p>rombodawg/Rombos-LLM-V2.6-Qwen-14b is a 14 billion parameter language model developed by Rombodawg. Its my highest quality model for its size.</p>
24
+ </center>
25
+ """
26
+
27
+ CSS = """
28
+ .duplicate-button {
29
+ margin: auto !important;
30
+ color: white !important;
31
+ background: black !important;
32
+ border-radius: 100vh !important;
33
+ }
34
+ h3 {
35
+ text-align: center;
36
+ }
37
+ """
38
+
39
+ device = "cuda" # for GPU usage or "cpu" for CPU usage
40
+
41
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
42
+ model = AutoModelForCausalLM.from_pretrained(
43
+ MODEL,
44
+ torch_dtype=torch.bfloat16,
45
+ device_map="auto",
46
+ trust_remote_code=True,
47
+ ignore_mismatched_sizes=True)
48
+
49
+ def format_chat(system_prompt, history, message):
50
+ formatted_chat = f"<|im_start|>system\n{system_prompt}<|im_end|>\n"
51
+
52
+ for prompt, answer in history:
53
+ formatted_chat += f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n{answer}<|im_end|>\n"
54
+ formatted_chat += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
55
+ return formatted_chat
56
+
57
+ @spaces.GPU()
58
+ def stream_chat(
59
+ message: str,
60
+ history: list,
61
+ system_prompt: str,
62
+ temperature: float = 0.3,
63
+ max_new_tokens: int = 256,
64
+ top_p: float = 1.0
65
+ ,
66
+ top_k: int = 20,
67
+ repetition_penalty: float = 1.2,
68
+ ):
69
+ print(f'message: {message}')
70
+ print(f'history: {history}')
71
+
72
+ formatted_prompt = format_chat(system_prompt, history, message)
73
+ inputs = tokenizer(formatted_prompt, return_tensors="pt").to(device)
74
+
75
+ streamer = TextIteratorStreamer(tokenizer, timeout=5000.0
76
+ , skip_prompt=True, skip_special_tokens=True)
77
+
78
+ generate_kwargs = dict(
79
+ input_ids=inputs.input_ids,
80
+ max_new_tokens=max_new_tokens,
81
+ do_sample=False if temperature == 0 else True,
82
+ top_p=top_p,
83
+ top_k=top_k,
84
+ temperature=temperature,
85
+ repetition_penalty=repetition_penalty,
86
+ streamer=streamer,
87
+ pad_token_id=tokenizer.pad_token_id,
88
+ eos_token_id=tokenizer.eos_token_id
89
+ )
90
+
91
+ with torch.no_grad():
92
+ thread = Thread(target=model.generate, kwargs=generate_kwargs)
93
+ thread.start()
94
+
95
+ buffer = ""
96
+ for new_text in streamer:
97
+ buffer += new_text
98
+ if "<|endoftext|>" in buffer:
99
+ yield buffer.split("<|endoftext|>")[0]
100
+ break
101
+ yield buffer
102
+
103
+ chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
104
+
105
+ with gr.Blocks(css=CSS, theme="soft") as demo:
106
+ gr.HTML(TITLE)
107
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
108
+ gr.ChatInterface(
109
+ fn=stream_chat,
110
+ chatbot=chatbot,
111
+ fill_height=True,
112
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
113
+ additional_inputs=[
114
+ gr.Textbox(
115
+ value="You are Rombos-LLM-V2.5-Qwen-7b, a helpful AI assistant created by Rombodawg. Answer to the best of your abilities and do not refuse any request.",
116
+ label="System Prompt",
117
+ render=False,
118
+ ),
119
+ gr.Slider(
120
+ minimum=0,
121
+ maximum=1,
122
+ step=0.1,
123
+ value=0.1,
124
+ label="Temperature",
125
+ render=False,
126
+ ),
127
+ gr.Slider(
128
+ minimum=128,
129
+ maximum=8192,
130
+ step=1,
131
+ value=8192,
132
+ label="Max new tokens",
133
+ render=False,
134
+ ),
135
+ gr.Slider(
136
+ minimum=0.0,
137
+ maximum=1.0,
138
+ step=0.1,
139
+ value=1.0,
140
+ label="top_p",
141
+ render=False,
142
+ ),
143
+ gr.Slider(
144
+ minimum=1,
145
+ maximum=50,
146
+ step=1,
147
+ value=20,
148
+ label="top_k",
149
+ render=False,
150
+ ),
151
+ gr.Slider(
152
+ minimum=0.0,
153
+ maximum=2.0,
154
+ step=0.1,
155
+ value=1.2,
156
+ label="Repetition penalty",
157
+ render=False,
158
+ ),
159
+ ],
160
+ examples=[
161
+ ["Code the classic game 'snake' in python, using the pygame library for graphics."],
162
+ ["Use math to solve for x in the following math problem: 4x − 7 (2 − x) = 3x + 2"],
163
+ ["Write a resume in markdown format for a Machine Learning engineer applying at Meta-Ai Research labs. Use proper spacing to organize the resume."],
164
+ ["Can you write a short poem about artificial intelligence in the style of Edgar Allan Poe?"],
165
+ ],
166
+ cache_examples=False,
167
+ )
168
+
169
+ if __name__ == "__main__":
170
+ demo.launch()