Spaces:
Runtime error
Runtime error
mateoluksenberg
commited on
Commit
•
58324ac
1
Parent(s):
19867bb
Update app.py
Browse files
app.py
CHANGED
@@ -10,27 +10,38 @@ import pymupdf
|
|
10 |
import docx
|
11 |
from pptx import Presentation
|
12 |
|
|
|
13 |
MODEL_LIST = ["nikravan/glm-4vq"]
|
14 |
|
15 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
16 |
MODEL_ID = MODEL_LIST[0]
|
17 |
MODEL_NAME = "GLM-4vq"
|
18 |
|
19 |
-
TITLE = "<h1>
|
20 |
|
21 |
DESCRIPTION = f"""
|
22 |
<center>
|
23 |
-
<p
|
24 |
<br>
|
25 |
🚀 MODEL NOW: <a href="https://hf.co/nikravan/glm-4vq">{MODEL_NAME}</a>
|
26 |
</center>"""
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
30 |
|
|
|
|
|
31 |
def extract_text(path):
|
32 |
return open(path, 'r').read()
|
33 |
|
|
|
34 |
def extract_pdf(path):
|
35 |
doc = pymupdf.open(path)
|
36 |
text = ""
|
@@ -38,6 +49,7 @@ def extract_pdf(path):
|
|
38 |
text += page.get_text()
|
39 |
return text
|
40 |
|
|
|
41 |
def extract_docx(path):
|
42 |
doc = docx.Document(path)
|
43 |
data = []
|
@@ -46,6 +58,7 @@ def extract_docx(path):
|
|
46 |
content = '\n\n'.join(data)
|
47 |
return content
|
48 |
|
|
|
49 |
def extract_pptx(path):
|
50 |
prs = Presentation(path)
|
51 |
text = ""
|
@@ -55,6 +68,7 @@ def extract_pptx(path):
|
|
55 |
text += shape.text + "\n"
|
56 |
return text
|
57 |
|
|
|
58 |
def mode_load(path):
|
59 |
choice = ""
|
60 |
file_type = path.split(".")[-1]
|
@@ -71,15 +85,20 @@ def mode_load(path):
|
|
71 |
choice = "doc"
|
72 |
print(content[:100])
|
73 |
return choice, content[:5000]
|
|
|
|
|
74 |
elif file_type in ["png", "jpg", "jpeg", "bmp", "tiff", "webp"]:
|
75 |
content = Image.open(path).convert('RGB')
|
76 |
choice = "image"
|
77 |
return choice, content
|
|
|
78 |
else:
|
79 |
raise gr.Error("Oops, unsupported files.")
|
80 |
|
|
|
81 |
@spaces.GPU()
|
82 |
def stream_chat(message, history: list, temperature: float, max_length: int, top_p: float, top_k: int, penalty: float):
|
|
|
83 |
model = AutoModelForCausalLM.from_pretrained(
|
84 |
MODEL_ID,
|
85 |
torch_dtype=torch.bfloat16,
|
@@ -100,9 +119,11 @@ def stream_chat(message, history: list, temperature: float, max_length: int, top
|
|
100 |
conversation.append({"role": "user", "content": format_msg})
|
101 |
else:
|
102 |
if len(history) == 0:
|
|
|
103 |
contents = None
|
104 |
conversation.append({"role": "user", "content": message['text']})
|
105 |
else:
|
|
|
106 |
for prompt, answer in history:
|
107 |
if answer is None:
|
108 |
prompt_files.append(prompt[0])
|
@@ -114,6 +135,8 @@ def stream_chat(message, history: list, temperature: float, max_length: int, top
|
|
114 |
else:
|
115 |
choice = ""
|
116 |
conversation.append({"role": "user", "image": "", "content": message['text']})
|
|
|
|
|
117 |
if choice == "image":
|
118 |
conversation.append({"role": "user", "image": contents, "content": message['text']})
|
119 |
elif choice == "doc":
|
@@ -145,6 +168,7 @@ def stream_chat(message, history: list, temperature: float, max_length: int, top
|
|
145 |
buffer += new_text
|
146 |
yield buffer
|
147 |
|
|
|
148 |
chatbot = gr.Chatbot(
|
149 |
#rtl=True,
|
150 |
)
|
@@ -153,69 +177,74 @@ chat_input = gr.MultimodalTextbox(
|
|
153 |
placeholder="Enter message or upload a file ...",
|
154 |
show_label=False,
|
155 |
#rtl=True,
|
156 |
-
)
|
157 |
|
|
|
|
|
|
|
158 |
EXAMPLES = [
|
159 |
-
[{"text": "
|
160 |
-
[{"text": "
|
161 |
-
[{"text": "
|
|
|
162 |
]
|
163 |
|
164 |
with gr.Blocks(css=CSS, theme="soft", fill_height=True) as demo:
|
165 |
gr.HTML(TITLE)
|
166 |
gr.HTML(DESCRIPTION)
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
|
|
219 |
|
220 |
if __name__ == "__main__":
|
221 |
-
|
|
|
|
10 |
import docx
|
11 |
from pptx import Presentation
|
12 |
|
13 |
+
|
14 |
MODEL_LIST = ["nikravan/glm-4vq"]
|
15 |
|
16 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
17 |
MODEL_ID = MODEL_LIST[0]
|
18 |
MODEL_NAME = "GLM-4vq"
|
19 |
|
20 |
+
TITLE = "<h1>3ML-bot</h1>"
|
21 |
|
22 |
DESCRIPTION = f"""
|
23 |
<center>
|
24 |
+
<p>😊 A Multi-Modal Multi-Lingual(3ML) Chat.
|
25 |
<br>
|
26 |
🚀 MODEL NOW: <a href="https://hf.co/nikravan/glm-4vq">{MODEL_NAME}</a>
|
27 |
</center>"""
|
28 |
|
29 |
+
CSS = """
|
30 |
+
h1 {
|
31 |
+
text-align: center;
|
32 |
+
display: block;
|
33 |
+
}
|
34 |
+
"""
|
35 |
+
|
36 |
|
37 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
38 |
|
39 |
+
|
40 |
+
|
41 |
def extract_text(path):
|
42 |
return open(path, 'r').read()
|
43 |
|
44 |
+
|
45 |
def extract_pdf(path):
|
46 |
doc = pymupdf.open(path)
|
47 |
text = ""
|
|
|
49 |
text += page.get_text()
|
50 |
return text
|
51 |
|
52 |
+
|
53 |
def extract_docx(path):
|
54 |
doc = docx.Document(path)
|
55 |
data = []
|
|
|
58 |
content = '\n\n'.join(data)
|
59 |
return content
|
60 |
|
61 |
+
|
62 |
def extract_pptx(path):
|
63 |
prs = Presentation(path)
|
64 |
text = ""
|
|
|
68 |
text += shape.text + "\n"
|
69 |
return text
|
70 |
|
71 |
+
|
72 |
def mode_load(path):
|
73 |
choice = ""
|
74 |
file_type = path.split(".")[-1]
|
|
|
85 |
choice = "doc"
|
86 |
print(content[:100])
|
87 |
return choice, content[:5000]
|
88 |
+
|
89 |
+
|
90 |
elif file_type in ["png", "jpg", "jpeg", "bmp", "tiff", "webp"]:
|
91 |
content = Image.open(path).convert('RGB')
|
92 |
choice = "image"
|
93 |
return choice, content
|
94 |
+
|
95 |
else:
|
96 |
raise gr.Error("Oops, unsupported files.")
|
97 |
|
98 |
+
|
99 |
@spaces.GPU()
|
100 |
def stream_chat(message, history: list, temperature: float, max_length: int, top_p: float, top_k: int, penalty: float):
|
101 |
+
|
102 |
model = AutoModelForCausalLM.from_pretrained(
|
103 |
MODEL_ID,
|
104 |
torch_dtype=torch.bfloat16,
|
|
|
119 |
conversation.append({"role": "user", "content": format_msg})
|
120 |
else:
|
121 |
if len(history) == 0:
|
122 |
+
# raise gr.Error("Please upload an image first.")
|
123 |
contents = None
|
124 |
conversation.append({"role": "user", "content": message['text']})
|
125 |
else:
|
126 |
+
# image = Image.open(history[0][0][0])
|
127 |
for prompt, answer in history:
|
128 |
if answer is None:
|
129 |
prompt_files.append(prompt[0])
|
|
|
135 |
else:
|
136 |
choice = ""
|
137 |
conversation.append({"role": "user", "image": "", "content": message['text']})
|
138 |
+
|
139 |
+
|
140 |
if choice == "image":
|
141 |
conversation.append({"role": "user", "image": contents, "content": message['text']})
|
142 |
elif choice == "doc":
|
|
|
168 |
buffer += new_text
|
169 |
yield buffer
|
170 |
|
171 |
+
|
172 |
chatbot = gr.Chatbot(
|
173 |
#rtl=True,
|
174 |
)
|
|
|
177 |
placeholder="Enter message or upload a file ...",
|
178 |
show_label=False,
|
179 |
#rtl=True,
|
|
|
180 |
|
181 |
+
|
182 |
+
|
183 |
+
)
|
184 |
EXAMPLES = [
|
185 |
+
[{"text": "Write a poem about spring season in French Language", }],
|
186 |
+
[{"text": "what does this chart mean?", "files": ["sales.png"]}],
|
187 |
+
[{"text": "¿Qué está escrito a mano en esta foto?", "files": ["receipt1.png"]}],
|
188 |
+
[{"text": "در مورد این عکس توضیح بده و بگو این چه فصلی می تواند باشد", "files": ["nature.jpg"]}]
|
189 |
]
|
190 |
|
191 |
with gr.Blocks(css=CSS, theme="soft", fill_height=True) as demo:
|
192 |
gr.HTML(TITLE)
|
193 |
gr.HTML(DESCRIPTION)
|
194 |
+
gr.ChatInterface(
|
195 |
+
fn=stream_chat,
|
196 |
+
multimodal=True,
|
197 |
+
|
198 |
+
|
199 |
+
textbox=chat_input,
|
200 |
+
chatbot=chatbot,
|
201 |
+
fill_height=True,
|
202 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
203 |
+
additional_inputs=[
|
204 |
+
gr.Slider(
|
205 |
+
minimum=0,
|
206 |
+
maximum=1,
|
207 |
+
step=0.1,
|
208 |
+
value=0.8,
|
209 |
+
label="Temperature",
|
210 |
+
render=False,
|
211 |
+
),
|
212 |
+
gr.Slider(
|
213 |
+
minimum=1024,
|
214 |
+
maximum=8192,
|
215 |
+
step=1,
|
216 |
+
value=4096,
|
217 |
+
label="Max Length",
|
218 |
+
render=False,
|
219 |
+
),
|
220 |
+
gr.Slider(
|
221 |
+
minimum=0.0,
|
222 |
+
maximum=1.0,
|
223 |
+
step=0.1,
|
224 |
+
value=1.0,
|
225 |
+
label="top_p",
|
226 |
+
render=False,
|
227 |
+
),
|
228 |
+
gr.Slider(
|
229 |
+
minimum=1,
|
230 |
+
maximum=20,
|
231 |
+
step=1,
|
232 |
+
value=10,
|
233 |
+
label="top_k",
|
234 |
+
render=False,
|
235 |
+
),
|
236 |
+
gr.Slider(
|
237 |
+
minimum=0.0,
|
238 |
+
maximum=2.0,
|
239 |
+
step=0.1,
|
240 |
+
value=1.0,
|
241 |
+
label="Repetition penalty",
|
242 |
+
render=False,
|
243 |
+
),
|
244 |
+
],
|
245 |
+
),
|
246 |
+
gr.Examples(EXAMPLES, [chat_input])
|
247 |
|
248 |
if __name__ == "__main__":
|
249 |
+
|
250 |
+
demo.queue(api_open=False).launch(show_api=False, share=False, )#server_name="0.0.0.0", )
|