Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoProcessor, PaddleOCRVLForConditionalGeneration
|
|
|
|
| 4 |
|
| 5 |
print("Loading model...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
model = PaddleOCRVLForConditionalGeneration.from_pretrained(
|
| 7 |
"PaddlePaddle/PaddleOCR-VL",
|
| 8 |
torch_dtype=torch.bfloat16,
|
| 9 |
device_map="auto"
|
| 10 |
)
|
| 11 |
processor = AutoProcessor.from_pretrained("PaddlePaddle/PaddleOCR-VL")
|
|
|
|
|
|
|
| 12 |
print("Model loaded!")
|
| 13 |
|
| 14 |
PROMPTS = {
|
|
@@ -22,11 +29,17 @@ def predict(image, task):
|
|
| 22 |
if image is None:
|
| 23 |
return "Загрузите изображение"
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
messages = [{"role": "user", "content": [
|
| 26 |
{"type": "image", "image": image},
|
| 27 |
{"type": "text", "text": PROMPTS[task]},
|
| 28 |
]}]
|
| 29 |
|
|
|
|
| 30 |
inputs = processor.apply_chat_template(
|
| 31 |
messages,
|
| 32 |
tokenize=True,
|
|
@@ -35,16 +48,19 @@ def predict(image, task):
|
|
| 35 |
return_tensors="pt"
|
| 36 |
).to(model.device)
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
generated_ids_trimmed = [
|
| 40 |
out_ids[len(in_ids):]
|
| 41 |
for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 42 |
]
|
| 43 |
-
result = processor.batch_decode(
|
| 44 |
-
generated_ids_trimmed,
|
| 45 |
-
skip_special_tokens=True
|
| 46 |
-
)[0]
|
| 47 |
|
|
|
|
| 48 |
return result
|
| 49 |
|
| 50 |
gr.Interface(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoProcessor, PaddleOCRVLForConditionalGeneration
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
print("Loading model...")
|
| 7 |
+
print(f"CUDA available: {torch.cuda.is_available()}")
|
| 8 |
+
if torch.cuda.is_available():
|
| 9 |
+
print(f"GPU: {torch.cuda.get_device_name(0)}")
|
| 10 |
+
|
| 11 |
model = PaddleOCRVLForConditionalGeneration.from_pretrained(
|
| 12 |
"PaddlePaddle/PaddleOCR-VL",
|
| 13 |
torch_dtype=torch.bfloat16,
|
| 14 |
device_map="auto"
|
| 15 |
)
|
| 16 |
processor = AutoProcessor.from_pretrained("PaddlePaddle/PaddleOCR-VL")
|
| 17 |
+
|
| 18 |
+
print(f"Model device: {model.device}")
|
| 19 |
print("Model loaded!")
|
| 20 |
|
| 21 |
PROMPTS = {
|
|
|
|
| 29 |
if image is None:
|
| 30 |
return "Загрузите изображение"
|
| 31 |
|
| 32 |
+
print(f"[{time.strftime('%H:%M:%S')}] Processing started...")
|
| 33 |
+
print(f"Image size: {image.size}")
|
| 34 |
+
|
| 35 |
+
t0 = time.time()
|
| 36 |
+
|
| 37 |
messages = [{"role": "user", "content": [
|
| 38 |
{"type": "image", "image": image},
|
| 39 |
{"type": "text", "text": PROMPTS[task]},
|
| 40 |
]}]
|
| 41 |
|
| 42 |
+
print(f"[{time.strftime('%H:%M:%S')}] Tokenizing...")
|
| 43 |
inputs = processor.apply_chat_template(
|
| 44 |
messages,
|
| 45 |
tokenize=True,
|
|
|
|
| 48 |
return_tensors="pt"
|
| 49 |
).to(model.device)
|
| 50 |
|
| 51 |
+
print(f"[{time.strftime('%H:%M:%S')}] Generating... (input shape: {inputs.input_ids.shape})")
|
| 52 |
+
|
| 53 |
+
with torch.inference_mode():
|
| 54 |
+
generated_ids = model.generate(**inputs, max_new_tokens=1024)
|
| 55 |
+
|
| 56 |
+
print(f"[{time.strftime('%H:%M:%S')}] Decoding...")
|
| 57 |
generated_ids_trimmed = [
|
| 58 |
out_ids[len(in_ids):]
|
| 59 |
for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 60 |
]
|
| 61 |
+
result = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True)[0]
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
print(f"[{time.strftime('%H:%M:%S')}] Done in {time.time()-t0:.1f}s")
|
| 64 |
return result
|
| 65 |
|
| 66 |
gr.Interface(
|