Update Main.py
Browse files
Main.py
CHANGED
|
@@ -81,28 +81,35 @@ if st.button("🔍 Распознать текст", use_container_width=True, t
|
|
| 81 |
else:
|
| 82 |
with st.spinner("Распознавание текста... (5–30 сек на CPU)"):
|
| 83 |
|
| 84 |
-
# Пр
|
|
|
|
|
|
|
|
|
|
| 85 |
conversation = [
|
| 86 |
{
|
| 87 |
"role": "user",
|
| 88 |
"content": [
|
| 89 |
{"type": "image"},
|
| 90 |
-
{"type": "text", "text":
|
| 91 |
]
|
| 92 |
}
|
| 93 |
]
|
| 94 |
-
|
| 95 |
-
# Применяем шаблон + передаём само изображение
|
| 96 |
-
inputs = processor.apply_chat_template(
|
| 97 |
conversation,
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
return_tensors="pt",
|
| 102 |
-
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
-
# Переносим
|
| 106 |
inputs = {
|
| 107 |
k: (v.to(device=device, dtype=dtype) if v.is_floating_point() else v.to(device))
|
| 108 |
for k, v in inputs.items()
|
|
@@ -119,7 +126,7 @@ if st.button("🔍 Распознать текст", use_container_width=True, t
|
|
| 119 |
eos_token_id=processor.tokenizer.eos_token_id,
|
| 120 |
)
|
| 121 |
|
| 122 |
-
# Убираем промпт
|
| 123 |
prompt_length = inputs["input_ids"].shape[1]
|
| 124 |
generated_ids = output_ids[0, prompt_length:]
|
| 125 |
|
|
@@ -129,21 +136,19 @@ if st.button("🔍 Распознать текст", use_container_width=True, t
|
|
| 129 |
clean_up_tokenization_spaces=True
|
| 130 |
).strip()
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
mime="text/plain"
|
| 146 |
-
)
|
| 147 |
|
| 148 |
st.markdown("---")
|
| 149 |
st.caption("Сделано на базе [lightonai/LightOnOCR-1B-1025](https://huggingface.co/lightonai/LightOnOCR-1B-1025)")
|
|
|
|
| 81 |
else:
|
| 82 |
with st.spinner("Распознавание текста... (5–30 сек на CPU)"):
|
| 83 |
|
| 84 |
+
# Промпт для модели
|
| 85 |
+
prompt = "Extract all the text from this image as accurately as possible. Preserve line breaks, formatting and tables."
|
| 86 |
+
|
| 87 |
+
# 1. Получаем текстовый шаблон чата (без токенизации)
|
| 88 |
conversation = [
|
| 89 |
{
|
| 90 |
"role": "user",
|
| 91 |
"content": [
|
| 92 |
{"type": "image"},
|
| 93 |
+
{"type": "text", "text": prompt}
|
| 94 |
]
|
| 95 |
}
|
| 96 |
]
|
| 97 |
+
text_prompt = processor.apply_chat_template(
|
|
|
|
|
|
|
| 98 |
conversation,
|
| 99 |
+
tokenize=False,
|
| 100 |
+
add_generation_prompt=True
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# 2. Правильный вызов процессора (ключевой момент!)
|
| 104 |
+
inputs = processor(
|
| 105 |
+
text=[text_prompt],
|
| 106 |
+
images=[[img]], # двойной список — обязательно!
|
| 107 |
return_tensors="pt",
|
| 108 |
+
padding=True,
|
| 109 |
+
size={"longest_edge": 1540} # рекомендуемый размер модели
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# Переносим на устройство
|
| 113 |
inputs = {
|
| 114 |
k: (v.to(device=device, dtype=dtype) if v.is_floating_point() else v.to(device))
|
| 115 |
for k, v in inputs.items()
|
|
|
|
| 126 |
eos_token_id=processor.tokenizer.eos_token_id,
|
| 127 |
)
|
| 128 |
|
| 129 |
+
# Убираем промпт
|
| 130 |
prompt_length = inputs["input_ids"].shape[1]
|
| 131 |
generated_ids = output_ids[0, prompt_length:]
|
| 132 |
|
|
|
|
| 136 |
clean_up_tokenization_spaces=True
|
| 137 |
).strip()
|
| 138 |
|
| 139 |
+
# Результат
|
| 140 |
+
st.success("✅ Распознавание завершено!")
|
| 141 |
+
st.markdown('<div class="result-box">', unsafe_allow_html=True)
|
| 142 |
+
st.subheader("📝 Распознанный текст")
|
| 143 |
+
st.code(generated_text, language=None)
|
| 144 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 145 |
+
|
| 146 |
+
st.download_button(
|
| 147 |
+
label="💾 Скачать как .txt",
|
| 148 |
+
data=generated_text,
|
| 149 |
+
file_name="recognized_text.txt",
|
| 150 |
+
mime="text/plain"
|
| 151 |
+
)
|
|
|
|
|
|
|
| 152 |
|
| 153 |
st.markdown("---")
|
| 154 |
st.caption("Сделано на базе [lightonai/LightOnOCR-1B-1025](https://huggingface.co/lightonai/LightOnOCR-1B-1025)")
|