Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -102,26 +102,26 @@ def load_and_preview_pdf(file_path: Optional[str]) -> Tuple[Optional[Image.Image
|
|
| 102 |
"""
|
| 103 |
state = get_initial_pdf_state()
|
| 104 |
if not file_path:
|
| 105 |
-
return None, state, '<div
|
| 106 |
|
| 107 |
try:
|
| 108 |
pages = convert_pdf_to_images(file_path)
|
| 109 |
if not pages:
|
| 110 |
-
return None, state, '<div
|
| 111 |
|
| 112 |
state["pages"] = pages
|
| 113 |
state["total_pages"] = len(pages)
|
| 114 |
-
page_info_html = f'<div
|
| 115 |
return pages[0], state, page_info_html
|
| 116 |
except Exception as e:
|
| 117 |
-
return None, state, f'<div
|
| 118 |
|
| 119 |
def navigate_pdf_page(direction: str, state: Dict[str, Any]):
|
| 120 |
"""
|
| 121 |
Navigates to the previous or next page in the PDF preview.
|
| 122 |
"""
|
| 123 |
if not state or not state["pages"]:
|
| 124 |
-
return None, state, '<div
|
| 125 |
|
| 126 |
current_index = state["current_page_index"]
|
| 127 |
total_pages = state["total_pages"]
|
|
@@ -136,7 +136,7 @@ def navigate_pdf_page(direction: str, state: Dict[str, Any]):
|
|
| 136 |
state["current_page_index"] = new_index
|
| 137 |
|
| 138 |
image_preview = state["pages"][new_index]
|
| 139 |
-
page_info_html = f'<div
|
| 140 |
|
| 141 |
return image_preview, state, page_info_html
|
| 142 |
|
|
@@ -258,168 +258,177 @@ video_examples = [["Explain the video in detail.", "videos/2.mp4"]]
|
|
| 258 |
pdf_examples = [["examples/sample-doc.pdf"]]
|
| 259 |
|
| 260 |
css = """
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
}
|
| 269 |
|
| 270 |
-
|
| 271 |
-
font-
|
| 272 |
-
|
|
|
|
|
|
|
| 273 |
}
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
padding: 20px;
|
| 278 |
-
background: var(--form-bg);
|
| 279 |
-
gap: 20px;
|
| 280 |
-
border-radius: 5px;
|
| 281 |
-
border: 2px solid var(--main-color);
|
| 282 |
-
box-shadow: 4px 4px var(--main-color);
|
| 283 |
}
|
| 284 |
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
font-weight: 900;
|
| 289 |
-
font-size: 20px;
|
| 290 |
-
margin-bottom: 25px;
|
| 291 |
-
text-align: center;
|
| 292 |
}
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
border-radius: 5px !important;
|
| 297 |
-
border: 2px solid var(--main-color) !important;
|
| 298 |
-
background-color: var(--bg-color) !important;
|
| 299 |
-
box-shadow: 4px 4px var(--main-color) !important;
|
| 300 |
}
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
}
|
| 307 |
-
|
| 308 |
-
|
|
|
|
| 309 |
}
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
}
|
| 315 |
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
border-radius: 5px !important;
|
| 319 |
-
border: 2px solid var(--main-color) !important;
|
| 320 |
-
background-color: var(--bg-color) !important;
|
| 321 |
-
box-shadow: 4px 4px var(--main-color) !important;
|
| 322 |
-
font-size: 17px !important;
|
| 323 |
-
font-weight: 600 !important;
|
| 324 |
-
color: var(--font-color) !important;
|
| 325 |
-
cursor: pointer !important;
|
| 326 |
-
transition: all 0.1s ease-in-out !important;
|
| 327 |
}
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
| 331 |
}
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
gap: 0.5rem;
|
| 336 |
}
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
background: lightcyan !important; /* Slightly different for inactive */
|
| 341 |
}
|
| 342 |
-
|
| 343 |
-
|
|
|
|
| 344 |
}
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
}
|
| 349 |
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
border: 2px solid var(--main-color) !important;
|
| 353 |
-
box-shadow: 4px 4px var(--main-color) !important;
|
| 354 |
-
background-color: lightcyan !important;
|
| 355 |
-
border-radius: 5px !important;
|
| 356 |
}
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
}
|
| 362 |
|
| 363 |
-
.
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
}
|
| 369 |
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
border-radius:
|
|
|
|
|
|
|
| 376 |
}
|
| 377 |
"""
|
| 378 |
|
| 379 |
-
with gr.Blocks(css=css
|
| 380 |
pdf_state = gr.State(value=get_initial_pdf_state())
|
| 381 |
-
gr.Markdown("# **Qwen3-VL-Processor**"
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
# --- INPUT COLUMN ---
|
| 385 |
-
with gr.Column(scale=2, min_width=400, elem_classes="retro-form"):
|
| 386 |
-
gr.Markdown("### INPUT", elem_classes="form-title")
|
| 387 |
-
|
| 388 |
with gr.Tabs():
|
| 389 |
-
with gr.TabItem("Image"):
|
| 390 |
-
image_query = gr.Textbox(label="Query", placeholder="
|
| 391 |
-
image_upload = gr.Image(type="pil", label="Image
|
| 392 |
-
image_submit = gr.Button("
|
| 393 |
gr.Examples(examples=image_examples, inputs=[image_query, image_upload])
|
| 394 |
|
| 395 |
-
with gr.TabItem("Video"):
|
| 396 |
-
video_query = gr.Textbox(label="Query", placeholder="
|
| 397 |
-
video_upload = gr.Video(label="Video
|
| 398 |
-
video_submit = gr.Button("
|
| 399 |
gr.Examples(examples=video_examples, inputs=[video_query, video_upload])
|
| 400 |
|
| 401 |
-
with gr.TabItem("PDF"):
|
| 402 |
-
pdf_query = gr.Textbox(label="Query", placeholder="Extract key points...")
|
| 403 |
-
pdf_upload = gr.File(label="PDF Upload", file_types=[".pdf"])
|
| 404 |
-
pdf_preview_img = gr.Image(label="PDF Preview")
|
| 405 |
with gr.Row():
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
|
| 411 |
with gr.Accordion("Advanced options", open=False):
|
| 412 |
max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
|
| 413 |
temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, step=0.1, value=0.6)
|
| 414 |
-
top_p = gr.Slider(label="Top-p", minimum=0.05, maximum=1.0, step=0.05, value=0.9)
|
| 415 |
top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
|
| 416 |
repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
|
| 417 |
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
|
|
|
| 423 |
|
| 424 |
# Event handlers
|
| 425 |
image_submit.click(
|
|
@@ -454,4 +463,4 @@ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
|
|
| 454 |
)
|
| 455 |
|
| 456 |
if __name__ == "__main__":
|
| 457 |
-
demo.queue(max_size=50).launch(ssr_mode=False, show_error=True)
|
|
|
|
| 102 |
"""
|
| 103 |
state = get_initial_pdf_state()
|
| 104 |
if not file_path:
|
| 105 |
+
return None, state, '<div style="text-align:center;">No file loaded</div>'
|
| 106 |
|
| 107 |
try:
|
| 108 |
pages = convert_pdf_to_images(file_path)
|
| 109 |
if not pages:
|
| 110 |
+
return None, state, '<div style="text-align:center;">Could not load file</div>'
|
| 111 |
|
| 112 |
state["pages"] = pages
|
| 113 |
state["total_pages"] = len(pages)
|
| 114 |
+
page_info_html = f'<div style="text-align:center;">Page 1 / {state["total_pages"]}</div>'
|
| 115 |
return pages[0], state, page_info_html
|
| 116 |
except Exception as e:
|
| 117 |
+
return None, state, f'<div style="text-align:center;">Failed to load preview: {e}</div>'
|
| 118 |
|
| 119 |
def navigate_pdf_page(direction: str, state: Dict[str, Any]):
|
| 120 |
"""
|
| 121 |
Navigates to the previous or next page in the PDF preview.
|
| 122 |
"""
|
| 123 |
if not state or not state["pages"]:
|
| 124 |
+
return None, state, '<div style="text-align:center;">No file loaded</div>'
|
| 125 |
|
| 126 |
current_index = state["current_page_index"]
|
| 127 |
total_pages = state["total_pages"]
|
|
|
|
| 136 |
state["current_page_index"] = new_index
|
| 137 |
|
| 138 |
image_preview = state["pages"][new_index]
|
| 139 |
+
page_info_html = f'<div style="text-align:center;">Page {new_index + 1} / {total_pages}</div>'
|
| 140 |
|
| 141 |
return image_preview, state, page_info_html
|
| 142 |
|
|
|
|
| 258 |
pdf_examples = [["examples/sample-doc.pdf"]]
|
| 259 |
|
| 260 |
css = """
|
| 261 |
+
/* From Uiverse.io by mi-series */
|
| 262 |
+
body {
|
| 263 |
+
background: #FCEDDA;
|
| 264 |
+
}
|
| 265 |
+
.gradio-container {
|
| 266 |
+
background: #FCEDDA;
|
| 267 |
+
}
|
| 268 |
+
.container {
|
| 269 |
+
position: relative;
|
| 270 |
+
max-width: 500px;
|
| 271 |
+
width: 100%;
|
| 272 |
+
background: #FCEDDA;
|
| 273 |
+
padding: 25px;
|
| 274 |
+
border-radius: 8px;
|
| 275 |
+
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
|
| 276 |
}
|
| 277 |
|
| 278 |
+
.container header {
|
| 279 |
+
font-size: 1.2rem;
|
| 280 |
+
color: #000;
|
| 281 |
+
font-weight: 600;
|
| 282 |
+
text-align: center;
|
| 283 |
}
|
| 284 |
|
| 285 |
+
.container .form {
|
| 286 |
+
margin-top: 15px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
}
|
| 288 |
|
| 289 |
+
.form .input-box {
|
| 290 |
+
width: 100%;
|
| 291 |
+
margin-top: 10px;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
}
|
| 293 |
|
| 294 |
+
.input-box label {
|
| 295 |
+
color: #000;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
}
|
| 297 |
+
|
| 298 |
+
.form :where(.input-box input, .select-box) {
|
| 299 |
+
position: relative;
|
| 300 |
+
height: 35px;
|
| 301 |
+
width: 100%;
|
| 302 |
+
outline: none;
|
| 303 |
+
font-size: 1rem;
|
| 304 |
+
color: #808080;
|
| 305 |
+
margin-top: 5px;
|
| 306 |
+
border: 1px solid #EE4E34;
|
| 307 |
+
border-radius: 6px;
|
| 308 |
+
padding: 0 15px;
|
| 309 |
+
background: #FCEDDA;
|
| 310 |
}
|
| 311 |
+
|
| 312 |
+
.input-box input:focus {
|
| 313 |
+
box-shadow: 0 1px 0 rgba(0, 0, 0, 0.1);
|
| 314 |
}
|
| 315 |
+
|
| 316 |
+
.form .column {
|
| 317 |
+
display: flex;
|
| 318 |
+
column-gap: 15px;
|
| 319 |
}
|
| 320 |
|
| 321 |
+
.form .gender-box {
|
| 322 |
+
margin-top: 10px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
}
|
| 324 |
+
|
| 325 |
+
.form :where(.gender-option, .gender) {
|
| 326 |
+
display: flex;
|
| 327 |
+
align-items: center;
|
| 328 |
+
column-gap: 50px;
|
| 329 |
+
flex-wrap: wrap;
|
| 330 |
}
|
| 331 |
|
| 332 |
+
.form .gender {
|
| 333 |
+
column-gap: 5px;
|
|
|
|
| 334 |
}
|
| 335 |
+
|
| 336 |
+
.gender input {
|
| 337 |
+
accent-color: #EE4E34;
|
|
|
|
| 338 |
}
|
| 339 |
+
|
| 340 |
+
.form :where(.gender input, .gender label) {
|
| 341 |
+
cursor: pointer;
|
| 342 |
}
|
| 343 |
+
|
| 344 |
+
.gender label {
|
| 345 |
+
color: #000;
|
| 346 |
}
|
| 347 |
|
| 348 |
+
.address :where(input, .select-box) {
|
| 349 |
+
margin-top: 10px;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
}
|
| 351 |
+
|
| 352 |
+
.select-box select {
|
| 353 |
+
height: 100%;
|
| 354 |
+
width: 100%;
|
| 355 |
+
outline: none;
|
| 356 |
+
border: none;
|
| 357 |
+
color: #808080;
|
| 358 |
+
font-size: 1rem;
|
| 359 |
+
background: #FCEDDA;
|
| 360 |
}
|
| 361 |
|
| 362 |
+
.form button, .submit-btn {
|
| 363 |
+
height: 40px;
|
| 364 |
+
width: 100%;
|
| 365 |
+
color: #000 !important;
|
| 366 |
+
font-size: 1rem;
|
| 367 |
+
font-weight: 400;
|
| 368 |
+
margin-top: 15px;
|
| 369 |
+
border: none;
|
| 370 |
+
border-radius: 6px;
|
| 371 |
+
cursor: pointer;
|
| 372 |
+
transition: all 0.2s ease;
|
| 373 |
+
background: #EE4E34 !important;
|
| 374 |
}
|
| 375 |
|
| 376 |
+
.form button:hover, .submit-btn:hover {
|
| 377 |
+
background: #EE3E34 !important;
|
| 378 |
+
}
|
| 379 |
+
.canvas-output {
|
| 380 |
+
border: 2px solid #EE4E34;
|
| 381 |
+
border-radius: 10px;
|
| 382 |
+
padding: 20px;
|
| 383 |
+
background: #FCEDDA;
|
| 384 |
}
|
| 385 |
"""
|
| 386 |
|
| 387 |
+
with gr.Blocks(css=css) as demo:
|
| 388 |
pdf_state = gr.State(value=get_initial_pdf_state())
|
| 389 |
+
gr.Markdown("# **Qwen3-VL-Processor**")
|
| 390 |
+
with gr.Row():
|
| 391 |
+
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
with gr.Tabs():
|
| 393 |
+
with gr.TabItem("Image Inference"):
|
| 394 |
+
image_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
|
| 395 |
+
image_upload = gr.Image(type="pil", label="Image", height=290)
|
| 396 |
+
image_submit = gr.Button("Submit", elem_classes="submit-btn")
|
| 397 |
gr.Examples(examples=image_examples, inputs=[image_query, image_upload])
|
| 398 |
|
| 399 |
+
with gr.TabItem("Video Inference"):
|
| 400 |
+
video_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
|
| 401 |
+
video_upload = gr.Video(label="Video", height=290)
|
| 402 |
+
video_submit = gr.Button("Submit", elem_classes="submit-btn")
|
| 403 |
gr.Examples(examples=video_examples, inputs=[video_query, video_upload])
|
| 404 |
|
| 405 |
+
with gr.TabItem("PDF Inference"):
|
|
|
|
|
|
|
|
|
|
| 406 |
with gr.Row():
|
| 407 |
+
with gr.Column(scale=1):
|
| 408 |
+
pdf_query = gr.Textbox(label="Query Input", placeholder="e.g., 'Summarize this document'")
|
| 409 |
+
pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 410 |
+
#gr.Examples(examples=pdf_examples, inputs=[pdf_upload])
|
| 411 |
+
pdf_submit = gr.Button("Submit", elem_classes="submit-btn")
|
| 412 |
+
with gr.Column(scale=1):
|
| 413 |
+
pdf_preview_img = gr.Image(label="PDF Preview", height=290)
|
| 414 |
+
with gr.Row():
|
| 415 |
+
prev_page_btn = gr.Button("◀ Previous")
|
| 416 |
+
page_info = gr.HTML('<div style="text-align:center;">No file loaded</div>')
|
| 417 |
+
next_page_btn = gr.Button("Next ▶")
|
| 418 |
|
| 419 |
with gr.Accordion("Advanced options", open=False):
|
| 420 |
max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
|
| 421 |
temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, step=0.1, value=0.6)
|
| 422 |
+
top_p = gr.Slider(label="Top-p (nucleus sampling)", minimum=0.05, maximum=1.0, step=0.05, value=0.9)
|
| 423 |
top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
|
| 424 |
repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
|
| 425 |
|
| 426 |
+
with gr.Column(scale=3):
|
| 427 |
+
with gr.Column(elem_classes="canvas-output"):
|
| 428 |
+
gr.Markdown("## Output")
|
| 429 |
+
output = gr.Textbox(label="Raw Output Stream", interactive=False, lines=14, show_copy_button=True)
|
| 430 |
+
with gr.Accordion("(Result.md)", open=False):
|
| 431 |
+
markdown_output = gr.Markdown(label="(Result.Md)")
|
| 432 |
|
| 433 |
# Event handlers
|
| 434 |
image_submit.click(
|
|
|
|
| 463 |
)
|
| 464 |
|
| 465 |
if __name__ == "__main__":
|
| 466 |
+
demo.queue(max_size=50).launch(mcp_server=True, ssr_mode=False, show_error=True)
|