Spaces:
Sleeping
Sleeping
Commit ·
286aa99
1
Parent(s): c96a8ff
app.py
CHANGED
|
@@ -1,50 +1,81 @@
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
-
import numpy as np
|
| 4 |
-
from paligemma2 import PaliGemma2Handler, MODELS as PALIGEMMA_MODELS
|
| 5 |
-
from gemma import GemmaHandler, MODELS as GEMMA_MODELS
|
| 6 |
-
from gemma_multiline import GemmaMultilineHandler, MODELS as GEMMA_MULTILINE_MODELS
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@spaces.GPU
|
| 14 |
def process_image_paligemma(model_name, image, progress=gr.Progress()):
|
| 15 |
"""Process a single image with PaliGemma2"""
|
| 16 |
-
return
|
| 17 |
|
| 18 |
@spaces.GPU
|
| 19 |
def process_image_gemma(model_name, image, progress=gr.Progress()):
|
| 20 |
"""Process a single image with Gemma"""
|
| 21 |
-
return
|
| 22 |
|
| 23 |
@spaces.GPU
|
| 24 |
def process_pdf_paligemma(pdf_path, model_name, progress=gr.Progress()):
|
| 25 |
"""Process a PDF file with PaliGemma2"""
|
| 26 |
-
return
|
| 27 |
|
| 28 |
@spaces.GPU
|
| 29 |
def process_pdf_gemma(pdf_path, model_name, progress=gr.Progress()):
|
| 30 |
"""Process a PDF file with Gemma"""
|
| 31 |
-
return
|
| 32 |
|
| 33 |
@spaces.GPU
|
| 34 |
def process_image_multiline(model_name, image, temp, top_p, repetition_penalty, progress=gr.Progress()):
|
| 35 |
-
return
|
| 36 |
|
| 37 |
@spaces.GPU
|
| 38 |
def process_image_multiline_stream(model_name, image, temp, top_p, repetition_penalty, progress=gr.Progress()):
|
| 39 |
-
yield from
|
| 40 |
|
| 41 |
@spaces.GPU
|
| 42 |
def process_pdf_multiline(model_name, pdf, temp, top_p, repetition_penalty, progress=gr.Progress()):
|
| 43 |
-
return
|
| 44 |
|
| 45 |
@spaces.GPU
|
| 46 |
def process_pdf_multiline_stream(model_name, pdf, temp, top_p, repetition_penalty, progress=gr.Progress()):
|
| 47 |
-
yield from
|
| 48 |
|
| 49 |
# Example images for document-level OCR
|
| 50 |
document_examples = [
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Model names defined here to avoid importing torch/transformers at startup.
|
| 5 |
+
# Heavy imports (torch, transformers, peft) are deferred until first GPU call.
|
| 6 |
+
PALIGEMMA_MODELS = {
|
| 7 |
+
"Medium-14k, Single Line": {},
|
| 8 |
+
"Medium-16k, Single Line": {},
|
| 9 |
+
"Small, Single Line": {},
|
| 10 |
+
}
|
| 11 |
+
GEMMA_MODELS = {
|
| 12 |
+
"Gemma-3 10k": {},
|
| 13 |
+
}
|
| 14 |
+
GEMMA_MULTILINE_MODELS = {
|
| 15 |
+
"Gemma Multiline - no-format": "",
|
| 16 |
+
"Gemma Multiline - line": "",
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
_paligemma_handler = None
|
| 20 |
+
_gemma_handler = None
|
| 21 |
+
_gemma_multiline_handler = None
|
| 22 |
+
|
| 23 |
+
def get_paligemma_handler():
|
| 24 |
+
global _paligemma_handler
|
| 25 |
+
if _paligemma_handler is None:
|
| 26 |
+
from paligemma2 import PaliGemma2Handler
|
| 27 |
+
_paligemma_handler = PaliGemma2Handler()
|
| 28 |
+
return _paligemma_handler
|
| 29 |
+
|
| 30 |
+
def get_gemma_handler():
|
| 31 |
+
global _gemma_handler
|
| 32 |
+
if _gemma_handler is None:
|
| 33 |
+
from gemma import GemmaHandler
|
| 34 |
+
_gemma_handler = GemmaHandler()
|
| 35 |
+
return _gemma_handler
|
| 36 |
+
|
| 37 |
+
def get_gemma_multiline_handler():
|
| 38 |
+
global _gemma_multiline_handler
|
| 39 |
+
if _gemma_multiline_handler is None:
|
| 40 |
+
from gemma_multiline import GemmaMultilineHandler
|
| 41 |
+
_gemma_multiline_handler = GemmaMultilineHandler()
|
| 42 |
+
return _gemma_multiline_handler
|
| 43 |
|
| 44 |
@spaces.GPU
|
| 45 |
def process_image_paligemma(model_name, image, progress=gr.Progress()):
|
| 46 |
"""Process a single image with PaliGemma2"""
|
| 47 |
+
return get_paligemma_handler().process_image(model_name, image, progress)
|
| 48 |
|
| 49 |
@spaces.GPU
|
| 50 |
def process_image_gemma(model_name, image, progress=gr.Progress()):
|
| 51 |
"""Process a single image with Gemma"""
|
| 52 |
+
return get_gemma_handler().process_image(model_name, image, progress)
|
| 53 |
|
| 54 |
@spaces.GPU
|
| 55 |
def process_pdf_paligemma(pdf_path, model_name, progress=gr.Progress()):
|
| 56 |
"""Process a PDF file with PaliGemma2"""
|
| 57 |
+
return get_paligemma_handler().process_pdf(pdf_path, model_name, progress)
|
| 58 |
|
| 59 |
@spaces.GPU
|
| 60 |
def process_pdf_gemma(pdf_path, model_name, progress=gr.Progress()):
|
| 61 |
"""Process a PDF file with Gemma"""
|
| 62 |
+
return get_gemma_handler().process_pdf(pdf_path, model_name, progress)
|
| 63 |
|
| 64 |
@spaces.GPU
|
| 65 |
def process_image_multiline(model_name, image, temp, top_p, repetition_penalty, progress=gr.Progress()):
|
| 66 |
+
return get_gemma_multiline_handler().generate_text_from_image(model_name, image, temp, top_p, repetition_penalty, progress)
|
| 67 |
|
| 68 |
@spaces.GPU
|
| 69 |
def process_image_multiline_stream(model_name, image, temp, top_p, repetition_penalty, progress=gr.Progress()):
|
| 70 |
+
yield from get_gemma_multiline_handler().generate_text_stream(model_name, image, temp, top_p, repetition_penalty, progress)
|
| 71 |
|
| 72 |
@spaces.GPU
|
| 73 |
def process_pdf_multiline(model_name, pdf, temp, top_p, repetition_penalty, progress=gr.Progress()):
|
| 74 |
+
return get_gemma_multiline_handler().process_pdf(model_name, pdf, temp, top_p, repetition_penalty, progress)
|
| 75 |
|
| 76 |
@spaces.GPU
|
| 77 |
def process_pdf_multiline_stream(model_name, pdf, temp, top_p, repetition_penalty, progress=gr.Progress()):
|
| 78 |
+
yield from get_gemma_multiline_handler().process_pdf_stream(model_name, pdf, temp, top_p, repetition_penalty, progress)
|
| 79 |
|
| 80 |
# Example images for document-level OCR
|
| 81 |
document_examples = [
|