rombodawg commited on
Commit
73f6e79
β€’
1 Parent(s): fb57727

Create app.py

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