mateoluksenberg commited on
Commit
4d43dca
1 Parent(s): ed89e35

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +292 -0
app.py ADDED
@@ -0,0 +1,292 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Hugging Face's logo
2
+ Hugging Face
3
+ Search models, datasets, users...
4
+ Models
5
+ Datasets
6
+ Spaces
7
+ Posts
8
+ Docs
9
+ Solutions
10
+ Pricing
11
+
12
+
13
+
14
+ Spaces:
15
+
16
+ nikravan
17
+ /
18
+ 3ML_bot
19
+
20
+
21
+ like
22
+ 1
23
+ App
24
+ Files
25
+ Community
26
+ 3ML_bot
27
+ /
28
+ app.py
29
+
30
+ nikravan's picture
31
+ nikravan
32
+ Update app.py
33
+ 51d0233
34
+ VERIFIED
35
+ about 1 month ago
36
+ raw
37
+
38
+ Copy download link
39
+ history
40
+ blame
41
+ contribute
42
+ delete
43
+ No virus
44
+
45
+ 7.36 kB
46
+ import torch
47
+ from PIL import Image
48
+ import gradio as gr
49
+ import spaces
50
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
51
+ import os
52
+ from threading import Thread
53
+
54
+ import pymupdf
55
+ import docx
56
+ from pptx import Presentation
57
+
58
+
59
+ MODEL_LIST = ["nikravan/glm-4vq"]
60
+
61
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
62
+ MODEL_ID = MODEL_LIST[0]
63
+ MODEL_NAME = "GLM-4vq"
64
+
65
+ TITLE = "<h1>3ML-bot</h1>"
66
+
67
+ DESCRIPTION = f"""
68
+ <center>
69
+ <p>😊 A Multi-Modal Multi-Lingual(3ML) Chat.
70
+ <br>
71
+ 🚀 MODEL NOW: <a href="https://hf.co/nikravan/glm-4vq">{MODEL_NAME}</a>
72
+ </center>"""
73
+
74
+ CSS = """
75
+ h1 {
76
+ text-align: center;
77
+ display: block;
78
+ }
79
+ """
80
+
81
+
82
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
83
+
84
+
85
+
86
+ def extract_text(path):
87
+ return open(path, 'r').read()
88
+
89
+
90
+ def extract_pdf(path):
91
+ doc = pymupdf.open(path)
92
+ text = ""
93
+ for page in doc:
94
+ text += page.get_text()
95
+ return text
96
+
97
+
98
+ def extract_docx(path):
99
+ doc = docx.Document(path)
100
+ data = []
101
+ for paragraph in doc.paragraphs:
102
+ data.append(paragraph.text)
103
+ content = '\n\n'.join(data)
104
+ return content
105
+
106
+
107
+ def extract_pptx(path):
108
+ prs = Presentation(path)
109
+ text = ""
110
+ for slide in prs.slides:
111
+ for shape in slide.shapes:
112
+ if hasattr(shape, "text"):
113
+ text += shape.text + "\n"
114
+ return text
115
+
116
+
117
+ def mode_load(path):
118
+ choice = ""
119
+ file_type = path.split(".")[-1]
120
+ print(file_type)
121
+ if file_type in ["pdf", "txt", "py", "docx", "pptx", "json", "cpp", "md"]:
122
+ if file_type.endswith("pdf"):
123
+ content = extract_pdf(path)
124
+ elif file_type.endswith("docx"):
125
+ content = extract_docx(path)
126
+ elif file_type.endswith("pptx"):
127
+ content = extract_pptx(path)
128
+ else:
129
+ content = extract_text(path)
130
+ choice = "doc"
131
+ print(content[:100])
132
+ return choice, content[:5000]
133
+
134
+
135
+ elif file_type in ["png", "jpg", "jpeg", "bmp", "tiff", "webp"]:
136
+ content = Image.open(path).convert('RGB')
137
+ choice = "image"
138
+ return choice, content
139
+
140
+ else:
141
+ raise gr.Error("Oops, unsupported files.")
142
+
143
+
144
+ @spaces.GPU()
145
+ def stream_chat(message, history: list, temperature: float, max_length: int, top_p: float, top_k: int, penalty: float):
146
+
147
+ model = AutoModelForCausalLM.from_pretrained(
148
+ MODEL_ID,
149
+ torch_dtype=torch.bfloat16,
150
+ low_cpu_mem_usage=True,
151
+ trust_remote_code=True
152
+ )
153
+
154
+ print(f'message is - {message}')
155
+ print(f'history is - {history}')
156
+ conversation = []
157
+ prompt_files = []
158
+ if message["files"]:
159
+ choice, contents = mode_load(message["files"][-1])
160
+ if choice == "image":
161
+ conversation.append({"role": "user", "image": contents, "content": message['text']})
162
+ elif choice == "doc":
163
+ format_msg = contents + "\n\n\n" + "{} files uploaded.\n" + message['text']
164
+ conversation.append({"role": "user", "content": format_msg})
165
+ else:
166
+ if len(history) == 0:
167
+ # raise gr.Error("Please upload an image first.")
168
+ contents = None
169
+ conversation.append({"role": "user", "content": message['text']})
170
+ else:
171
+ # image = Image.open(history[0][0][0])
172
+ for prompt, answer in history:
173
+ if answer is None:
174
+ prompt_files.append(prompt[0])
175
+ conversation.extend([{"role": "user", "content": ""}, {"role": "assistant", "content": ""}])
176
+ else:
177
+ conversation.extend([{"role": "user", "content": prompt}, {"role": "assistant", "content": answer}])
178
+ if len(prompt_files) > 0:
179
+ choice, contents = mode_load(prompt_files[-1])
180
+ else:
181
+ choice = ""
182
+ conversation.append({"role": "user", "image": "", "content": message['text']})
183
+
184
+
185
+ if choice == "image":
186
+ conversation.append({"role": "user", "image": contents, "content": message['text']})
187
+ elif choice == "doc":
188
+ format_msg = contents + "\n\n\n" + "{} files uploaded.\n" + message['text']
189
+ conversation.append({"role": "user", "content": format_msg})
190
+ print(f"Conversation is -\n{conversation}")
191
+
192
+ input_ids = tokenizer.apply_chat_template(conversation, tokenize=True, add_generation_prompt=True,
193
+ return_tensors="pt", return_dict=True).to(model.device)
194
+ streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
195
+
196
+ generate_kwargs = dict(
197
+ max_length=max_length,
198
+ streamer=streamer,
199
+ do_sample=True,
200
+ top_p=top_p,
201
+ top_k=top_k,
202
+ temperature=temperature,
203
+ repetition_penalty=penalty,
204
+ eos_token_id=[151329, 151336, 151338],
205
+ )
206
+ gen_kwargs = {**input_ids, **generate_kwargs}
207
+
208
+ with torch.no_grad():
209
+ thread = Thread(target=model.generate, kwargs=gen_kwargs)
210
+ thread.start()
211
+ buffer = ""
212
+ for new_text in streamer:
213
+ buffer += new_text
214
+ yield buffer
215
+
216
+
217
+ chatbot = gr.Chatbot(
218
+ #rtl=True,
219
+ )
220
+ chat_input = gr.MultimodalTextbox(
221
+ interactive=True,
222
+ placeholder="Enter message or upload a file ...",
223
+ show_label=False,
224
+ #rtl=True,
225
+
226
+
227
+
228
+ )
229
+ EXAMPLES = [
230
+
231
+ ]
232
+
233
+ with gr.Blocks(css=CSS, theme="soft", fill_height=True) as demo:
234
+ gr.HTML(TITLE)
235
+ gr.HTML(DESCRIPTION)
236
+ gr.ChatInterface(
237
+ fn=stream_chat,
238
+ multimodal=True,
239
+
240
+
241
+ textbox=chat_input,
242
+ chatbot=chatbot,
243
+ fill_height=True,
244
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
245
+ additional_inputs=[
246
+ gr.Slider(
247
+ minimum=0,
248
+ maximum=1,
249
+ step=0.1,
250
+ value=0.8,
251
+ label="Temperature",
252
+ render=False,
253
+ ),
254
+ gr.Slider(
255
+ minimum=1024,
256
+ maximum=8192,
257
+ step=1,
258
+ value=4096,
259
+ label="Max Length",
260
+ render=False,
261
+ ),
262
+ gr.Slider(
263
+ minimum=0.0,
264
+ maximum=1.0,
265
+ step=0.1,
266
+ value=1.0,
267
+ label="top_p",
268
+ render=False,
269
+ ),
270
+ gr.Slider(
271
+ minimum=1,
272
+ maximum=20,
273
+ step=1,
274
+ value=10,
275
+ label="top_k",
276
+ render=False,
277
+ ),
278
+ gr.Slider(
279
+ minimum=0.0,
280
+ maximum=2.0,
281
+ step=0.1,
282
+ value=1.0,
283
+ label="Repetition penalty",
284
+ render=False,
285
+ ),
286
+ ],
287
+ ),
288
+ gr.Examples(EXAMPLES, [chat_input])
289
+
290
+ if __name__ == "__main__":
291
+
292
+ demo.queue(api_open=False).launch(show_api=False, share=False, )#server_name="0.0.0.0", )