Update Main.py
Browse files
Main.py
CHANGED
|
@@ -81,7 +81,7 @@ if st.button("🔍 Распознать текст", use_container_width=True, t
|
|
| 81 |
else:
|
| 82 |
with st.spinner("Распознавание текста... (5–20 сек на CPU)"):
|
| 83 |
|
| 84 |
-
#
|
| 85 |
conversation = [
|
| 86 |
{
|
| 87 |
"role": "user",
|
|
@@ -92,7 +92,7 @@ if st.button("🔍 Распознать текст", use_container_width=True, t
|
|
| 92 |
}
|
| 93 |
]
|
| 94 |
|
| 95 |
-
# Применяем шаблон чата
|
| 96 |
inputs = processor.apply_chat_template(
|
| 97 |
conversation,
|
| 98 |
add_generation_prompt=True,
|
|
@@ -101,13 +101,23 @@ if st.button("🔍 Распознать текст", use_container_width=True, t
|
|
| 101 |
return_tensors="pt"
|
| 102 |
)
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# Переносим остальные тензоры
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
inputs[k] = v.to(device=device)
|
| 112 |
|
| 113 |
# Генерация
|
|
|
|
| 81 |
else:
|
| 82 |
with st.spinner("Распознавание текста... (5–20 сек на CPU)"):
|
| 83 |
|
| 84 |
+
# Шаблон разговора
|
| 85 |
conversation = [
|
| 86 |
{
|
| 87 |
"role": "user",
|
|
|
|
| 92 |
}
|
| 93 |
]
|
| 94 |
|
| 95 |
+
# Применяем шаблон чата (без изображения)
|
| 96 |
inputs = processor.apply_chat_template(
|
| 97 |
conversation,
|
| 98 |
add_generation_prompt=True,
|
|
|
|
| 101 |
return_tensors="pt"
|
| 102 |
)
|
| 103 |
|
| 104 |
+
# Обработка изображения
|
| 105 |
+
image_inputs = processor.image_processor(img, return_tensors="pt")
|
| 106 |
+
pixel_values = image_inputs.pixel_values.to(device=device, dtype=dtype)
|
| 107 |
+
|
| 108 |
+
# Получаем размеры изображения (важно для этой модели!)
|
| 109 |
+
height, width = img.height, img.width
|
| 110 |
+
image_sizes = torch.tensor([[height, width]], dtype=torch.long).to(device)
|
| 111 |
+
|
| 112 |
+
# Добавляем в inputs
|
| 113 |
+
inputs["pixel_values"] = pixel_values
|
| 114 |
+
inputs["image_sizes"] = image_sizes
|
| 115 |
|
| 116 |
# Переносим остальные тензоры
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
for k, v in list(inputs.items()):
|
| 120 |
+
if isinstance(v, torch.Tensor) and k not in ["pixel_values", "image_sizes"]:
|
| 121 |
inputs[k] = v.to(device=device)
|
| 122 |
|
| 123 |
# Генерация
|