Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,296 +1,33 @@
|
|
| 1 |
-
#!/usr/bin/env python
|
| 2 |
-
# encoding: utf-8
|
| 3 |
-
import spaces
|
| 4 |
import gradio as gr
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
import re
|
| 8 |
-
import torch
|
| 9 |
-
import argparse
|
| 10 |
-
from transformers import AutoModel, AutoTokenizer
|
| 11 |
-
import fitz # PyMuPDF
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# For Mac with MPS (Apple silicon or AMD GPUs).
|
| 19 |
-
# PYTORCH_ENABLE_MPS_FALLBACK=1 python web_demo_2.5.py --device mps
|
| 20 |
-
|
| 21 |
-
# Argparser
|
| 22 |
-
parser = argparse.ArgumentParser(description='demo')
|
| 23 |
-
parser.add_argument('--device', type=str, default='cuda', help='cuda or mps')
|
| 24 |
-
args = parser.parse_args()
|
| 25 |
-
device = args.device
|
| 26 |
-
assert device in ['cuda', 'mps']
|
| 27 |
-
|
| 28 |
-
# Load model
|
| 29 |
-
model_path = 'openbmb/MiniCPM-Llama3-V-2_5'
|
| 30 |
-
if 'int4' in model_path:
|
| 31 |
-
if device == 'mps':
|
| 32 |
-
print('Error: running int4 model with bitsandbytes on Mac is not supported right now.')
|
| 33 |
-
exit()
|
| 34 |
-
model = AutoModel.from_pretrained(model_path, trust_remote_code=True)
|
| 35 |
-
else:
|
| 36 |
-
model = AutoModel.from_pretrained(model_path, trust_remote_code=True).to(dtype=torch.float16)
|
| 37 |
-
model = model.to(device=device)
|
| 38 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
| 39 |
-
model.eval()
|
| 40 |
-
|
| 41 |
-
ERROR_MSG = "Error, please retry"
|
| 42 |
-
model_name = 'MiniCPM-Llama3-V 2.5'
|
| 43 |
-
|
| 44 |
-
form_radio = {
|
| 45 |
-
'choices': ['Beam Search', 'Sampling'],
|
| 46 |
-
'value': 'Sampling',
|
| 47 |
-
'interactive': True,
|
| 48 |
-
'label': 'Decode Type'
|
| 49 |
-
}
|
| 50 |
-
# Beam Form
|
| 51 |
-
num_beams_slider = {
|
| 52 |
-
'minimum': 0,
|
| 53 |
-
'maximum': 5,
|
| 54 |
-
'value': 3,
|
| 55 |
-
'step': 1,
|
| 56 |
-
'interactive': True,
|
| 57 |
-
'label': 'Num Beams'
|
| 58 |
-
}
|
| 59 |
-
repetition_penalty_slider = {
|
| 60 |
-
'minimum': 0,
|
| 61 |
-
'maximum': 3,
|
| 62 |
-
'value': 1.2,
|
| 63 |
-
'step': 0.01,
|
| 64 |
-
'interactive': True,
|
| 65 |
-
'label': 'Repetition Penalty'
|
| 66 |
-
}
|
| 67 |
-
repetition_penalty_slider2 = {
|
| 68 |
-
'minimum': 0,
|
| 69 |
-
'maximum': 3,
|
| 70 |
-
'value': 1.05,
|
| 71 |
-
'step': 0.01,
|
| 72 |
-
'interactive': True,
|
| 73 |
-
'label': 'Repetition Penalty'
|
| 74 |
-
}
|
| 75 |
-
max_new_tokens_slider = {
|
| 76 |
-
'minimum': 1,
|
| 77 |
-
'maximum': 4096,
|
| 78 |
-
'value': 1024,
|
| 79 |
-
'step': 1,
|
| 80 |
-
'interactive': True,
|
| 81 |
-
'label': 'Max New Tokens'
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
top_p_slider = {
|
| 85 |
-
'minimum': 0,
|
| 86 |
-
'maximum': 1,
|
| 87 |
-
'value': 0.8,
|
| 88 |
-
'step': 0.05,
|
| 89 |
-
'interactive': True,
|
| 90 |
-
'label': 'Top P'
|
| 91 |
-
}
|
| 92 |
-
top_k_slider = {
|
| 93 |
-
'minimum': 0,
|
| 94 |
-
'maximum': 200,
|
| 95 |
-
'value': 100,
|
| 96 |
-
'step': 1,
|
| 97 |
-
'interactive': True,
|
| 98 |
-
'label': 'Top K'
|
| 99 |
-
}
|
| 100 |
-
temperature_slider = {
|
| 101 |
-
'minimum': 0,
|
| 102 |
-
'maximum': 2,
|
| 103 |
-
'value': 0.7,
|
| 104 |
-
'step': 0.05,
|
| 105 |
-
'interactive': True,
|
| 106 |
-
'label': 'Temperature'
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
def create_component(params, comp='Slider'):
|
| 110 |
-
if comp == 'Slider':
|
| 111 |
-
return gr.Slider(
|
| 112 |
-
minimum=params['minimum'],
|
| 113 |
-
maximum=params['maximum'],
|
| 114 |
-
value=params['value'],
|
| 115 |
-
step=params['step'],
|
| 116 |
-
interactive=params['interactive'],
|
| 117 |
-
label=params['label']
|
| 118 |
-
)
|
| 119 |
-
elif comp == 'Radio':
|
| 120 |
-
return gr.Radio(
|
| 121 |
-
choices=params['choices'],
|
| 122 |
-
value=params['value'],
|
| 123 |
-
interactive=params['interactive'],
|
| 124 |
-
label=params['label']
|
| 125 |
-
)
|
| 126 |
-
elif comp == 'Button':
|
| 127 |
-
return gr.Button(
|
| 128 |
-
value=params['value'],
|
| 129 |
-
interactive=True
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
@spaces.GPU(duration=120)
|
| 133 |
-
def chat(img, msgs, ctx, params=None, vision_hidden_states=None):
|
| 134 |
-
default_params = {"stream": False, "sampling": False, "num_beams":3, "repetition_penalty": 1.2, "max_new_tokens": 1024}
|
| 135 |
-
if params is None:
|
| 136 |
-
params = default_params
|
| 137 |
-
if img is None:
|
| 138 |
-
yield "Error, invalid image, please upload a new image"
|
| 139 |
-
else:
|
| 140 |
-
try:
|
| 141 |
-
image = img.convert('RGB')
|
| 142 |
-
answer = model.chat(
|
| 143 |
-
image=image,
|
| 144 |
-
msgs=msgs,
|
| 145 |
-
tokenizer=tokenizer,
|
| 146 |
-
**params
|
| 147 |
-
)
|
| 148 |
-
for char in answer:
|
| 149 |
-
yield char
|
| 150 |
-
except Exception as err:
|
| 151 |
-
print(err)
|
| 152 |
-
traceback.print_exc()
|
| 153 |
-
yield ERROR_MSG
|
| 154 |
-
|
| 155 |
-
def pdf_to_images(pdf_file):
|
| 156 |
-
doc = fitz.open(stream=pdf_file, filetype="pdf")
|
| 157 |
-
images = []
|
| 158 |
-
for page_num in range(len(doc)):
|
| 159 |
-
page = doc.load_page(page_num)
|
| 160 |
-
pix = page.get_pixmap()
|
| 161 |
-
img = Image.open(BytesIO(pix.tobytes()))
|
| 162 |
-
images.append(img)
|
| 163 |
-
return images
|
| 164 |
-
|
| 165 |
-
def upload_pdf(pdf_file, _chatbot, _app_session):
|
| 166 |
-
images = pdf_to_images(pdf_file)
|
| 167 |
-
if images:
|
| 168 |
-
_app_session['img'] = images[0]
|
| 169 |
-
_chatbot.append(('', 'PDF uploaded successfully, the first page has been selected. You can talk to me now.'))
|
| 170 |
-
else:
|
| 171 |
-
_chatbot.append(('', 'Error processing PDF.'))
|
| 172 |
-
return _chatbot, _app_session
|
| 173 |
-
|
| 174 |
-
def upload_img(image, _chatbot, _app_session):
|
| 175 |
-
image = Image.fromarray(image)
|
| 176 |
-
_app_session['sts'] = None
|
| 177 |
-
_app_session['ctx'] = []
|
| 178 |
-
_app_session['img'] = image
|
| 179 |
-
_chatbot.append(('', 'Image uploaded successfully, you can talk to me now'))
|
| 180 |
-
return _chatbot, _app_session
|
| 181 |
-
|
| 182 |
-
def respond(_chat_bot, _app_cfg, params_form, num_beams, repetition_penalty, repetition_penalty_2, top_p, top_k, temperature):
|
| 183 |
-
_question = _chat_bot[-1][0]
|
| 184 |
-
print('<Question>:', _question)
|
| 185 |
-
if _app_cfg.get('ctx', None) is None:
|
| 186 |
-
_chat_bot[-1][1] = 'Please upload an image to start'
|
| 187 |
-
yield (_chat_bot, _app_cfg)
|
| 188 |
-
else:
|
| 189 |
-
_context = _app_cfg['ctx'].copy()
|
| 190 |
-
if _context:
|
| 191 |
-
_context.append({"role": "user", "content": _question})
|
| 192 |
-
else:
|
| 193 |
-
_context = [{"role": "user", "content": _question}]
|
| 194 |
-
if params_form == 'Beam Search':
|
| 195 |
-
params = {
|
| 196 |
-
'sampling': False,
|
| 197 |
-
'stream': False,
|
| 198 |
-
'num_beams': num_beams,
|
| 199 |
-
'repetition_penalty': repetition_penalty,
|
| 200 |
-
"max_new_tokens": 896
|
| 201 |
-
}
|
| 202 |
-
else:
|
| 203 |
-
params = {
|
| 204 |
-
'sampling': True,
|
| 205 |
-
'stream': True,
|
| 206 |
-
'top_p': top_p,
|
| 207 |
-
'top_k': top_k,
|
| 208 |
-
'temperature': temperature,
|
| 209 |
-
'repetition_penalty': repetition_penalty_2,
|
| 210 |
-
"max_new_tokens": 896
|
| 211 |
-
}
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
return
|
| 223 |
-
|
| 224 |
-
def regenerate_button_clicked(_question, _chat_bot, _app_cfg):
|
| 225 |
-
if len(_chat_bot) <= 1:
|
| 226 |
-
_chat_bot.append(('Regenerate', 'No question for regeneration.'))
|
| 227 |
-
return '', _chat_bot, _app_cfg
|
| 228 |
-
elif _chat_bot[-1][0] == 'Regenerate':
|
| 229 |
-
return '', _chat_bot, _app_cfg
|
| 230 |
-
else:
|
| 231 |
-
_question = _chat_bot[-1][0]
|
| 232 |
-
_chat_bot = _chat_bot[:-1]
|
| 233 |
-
_app_cfg['ctx'] = _app_cfg['ctx'][:-2]
|
| 234 |
-
return request(_question, _chat_bot, _app_cfg)
|
| 235 |
-
|
| 236 |
-
def clear_button_clicked(_question, _chat_bot, _app_cfg, _bt_pic):
|
| 237 |
-
_chat_bot.clear()
|
| 238 |
-
_app_cfg['sts'] = None
|
| 239 |
-
_app_cfg['ctx'] = None
|
| 240 |
-
_app_cfg['img'] = None
|
| 241 |
-
_bt_pic = None
|
| 242 |
-
return '', _chat_bot, _app_cfg, _bt_pic
|
| 243 |
|
|
|
|
| 244 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
| 245 |
with gr.Row():
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
repetition_penalty = create_component(repetition_penalty_slider)
|
| 251 |
-
with gr.Accordion("Sampling") as sampling_according:
|
| 252 |
-
top_p = create_component(top_p_slider)
|
| 253 |
-
top_k = create_component(top_k_slider)
|
| 254 |
-
temperature = create_component(temperature_slider)
|
| 255 |
-
repetition_penalty_2 = create_component(repetition_penalty_slider2)
|
| 256 |
-
regenerate = create_component({'value': 'Regenerate'}, comp='Button')
|
| 257 |
-
clear = create_component({'value': 'Clear'}, comp='Button')
|
| 258 |
-
with gr.Column(scale=3, min_width=500):
|
| 259 |
-
app_session = gr.State({'sts': None, 'ctx': None, 'img': None})
|
| 260 |
-
bt_pic = gr.Image(label="Upload an image to start")
|
| 261 |
-
bt_pdf = gr.File(label="Upload a PDF to start")
|
| 262 |
-
chat_bot = gr.Chatbot(label=f"Chat with {model_name}")
|
| 263 |
-
txt_message = gr.Textbox(label="Input text")
|
| 264 |
-
|
| 265 |
-
clear.click(
|
| 266 |
-
clear_button_clicked,
|
| 267 |
-
[txt_message, chat_bot, app_session, bt_pic],
|
| 268 |
-
[txt_message, chat_bot, app_session, bt_pic],
|
| 269 |
-
queue=False
|
| 270 |
-
)
|
| 271 |
-
txt_message.submit(
|
| 272 |
-
request,
|
| 273 |
-
[txt_message, chat_bot, app_session],
|
| 274 |
-
[txt_message, chat_bot, app_session],
|
| 275 |
-
queue=False
|
| 276 |
-
).then(
|
| 277 |
-
respond,
|
| 278 |
-
[chat_bot, app_session, params_form, num_beams, repetition_penalty, repetition_penalty_2, top_p, top_k, temperature],
|
| 279 |
-
[chat_bot, app_session]
|
| 280 |
-
)
|
| 281 |
-
regenerate.click(
|
| 282 |
-
regenerate_button_clicked,
|
| 283 |
-
[txt_message, chat_bot, app_session],
|
| 284 |
-
[txt_message, chat_bot, app_session],
|
| 285 |
-
queue=False
|
| 286 |
-
).then(
|
| 287 |
-
respond,
|
| 288 |
-
[chat_bot, app_session, params_form, num_beams, repetition_penalty, repetition_penalty_2, top_p, top_k, temperature],
|
| 289 |
-
[chat_bot, app_session]
|
| 290 |
-
)
|
| 291 |
-
bt_pic.upload(lambda: None, None, chat_bot, queue=False).then(upload_img, inputs=[bt_pic, chat_bot, app_session], outputs=[chat_bot, app_session])
|
| 292 |
-
bt_pdf.upload(lambda: None, None, chat_bot, queue=False).then(upload_pdf, inputs=[bt_pdf, chat_bot, app_session], outputs=[chat_bot, app_session])
|
| 293 |
|
| 294 |
-
|
| 295 |
-
demo.
|
| 296 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import base64
|
| 3 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Function to upload a PDF and return its content as HTML for display
|
| 6 |
+
def upload_and_display_pdf(pdf_file):
|
| 7 |
+
# Save the uploaded PDF with its original filename
|
| 8 |
+
pdf_path = os.path.join("/content", pdf_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
with open(pdf_path, "wb") as f:
|
| 11 |
+
f.write(pdf_file.read())
|
| 12 |
+
|
| 13 |
+
# Convert the PDF to base64 and create an embedded HTML
|
| 14 |
+
with open(pdf_path, "rb") as pdf_file:
|
| 15 |
+
base64_pdf = base64.b64encode(pdf_file.read()).decode('utf-8')
|
| 16 |
+
|
| 17 |
+
pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width="800" height="500" type="application/pdf">'
|
| 18 |
+
|
| 19 |
+
return pdf_display
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Set up the Gradio interface
|
| 22 |
with gr.Blocks() as demo:
|
| 23 |
+
gr.Markdown("# PDF Viewer")
|
| 24 |
+
gr.Markdown("Upload a PDF file to view it in the browser.")
|
| 25 |
+
|
| 26 |
with gr.Row():
|
| 27 |
+
file_input = gr.File(label="Upload your PDF file", type="binary", file_types=[".pdf"])
|
| 28 |
+
pdf_output = gr.HTML()
|
| 29 |
+
|
| 30 |
+
file_input.change(upload_and_display_pdf, inputs=file_input, outputs=pdf_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
demo.launch()
|
|
|