Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,8 +13,52 @@ summarization_model_name = 'sarahai/ruT5-base-summarizer'
|
|
13 |
summarization_model = T5ForConditionalGeneration.from_pretrained(summarization_model_name)
|
14 |
summarization_tokenizer = T5Tokenizer.from_pretrained(summarization_model_name)
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# Custom CSS styles
|
20 |
st.markdown("""
|
@@ -32,19 +76,20 @@ st.markdown("""
|
|
32 |
# Sidebar
|
33 |
st.sidebar.markdown('## Навигация')
|
34 |
uploaded_file = st.sidebar.file_uploader("Загрузите изображение с узбекским текстом...", type=["jpg", "jpeg", "png"])
|
|
|
35 |
|
36 |
if uploaded_file:
|
37 |
image = Image.open(uploaded_file)
|
38 |
st.sidebar.image(image, caption='Загруженное изображение', use_column_width=True)
|
39 |
process_btn = st.sidebar.button("Перевести и суммаризировать")
|
40 |
|
41 |
-
#
|
42 |
st.markdown('<h1 class="big-font">Текстовая обработка изображений</h1>', unsafe_allow_html=True)
|
43 |
st.markdown('<div class="big-font">Перевод с узбекского на русский и суммаризация</div>', unsafe_allow_html=True)
|
44 |
|
45 |
-
if process_btn:
|
46 |
st.write("Процесс извлечения текста...")
|
47 |
-
extracted_text, confidence = extract_text(image, '
|
48 |
st.write("Извлеченный текст:")
|
49 |
st.text_area("Результат", extracted_text, height=150)
|
50 |
st.write(f"Точность распознавания: {confidence*100:.2f}%")
|
|
|
13 |
summarization_model = T5ForConditionalGeneration.from_pretrained(summarization_model_name)
|
14 |
summarization_tokenizer = T5Tokenizer.from_pretrained(summarization_model_name)
|
15 |
|
16 |
+
def extract_text(image_path, lang='uzb_Cyrl'):
|
17 |
+
reader = easyocr.Reader([lang])
|
18 |
+
results = reader.readtext(np.array(image_path))
|
19 |
+
|
20 |
+
all_text = ''
|
21 |
+
confidences = []
|
22 |
+
|
23 |
+
for (bbox, text, prob) in results:
|
24 |
+
all_text += ' ' + text
|
25 |
+
confidences.append(prob)
|
26 |
+
|
27 |
+
final_confidence = sum(confidences) / len(confidences) if confidences else 0
|
28 |
+
return all_text.strip(), final_confidence
|
29 |
+
|
30 |
+
def split_into_chunks(text, tokenizer, max_length=150):
|
31 |
+
tokens = tokenizer.tokenize(text)
|
32 |
+
chunks = []
|
33 |
+
current_chunk = []
|
34 |
+
current_length = 0
|
35 |
+
for token in tokens:
|
36 |
+
current_chunk.append(token)
|
37 |
+
current_length += 1
|
38 |
+
if current_length >= max_length:
|
39 |
+
chunks.append(tokenizer.convert_tokens_to_string(current_chunk))
|
40 |
+
current_chunk = []
|
41 |
+
current_length = 0
|
42 |
+
if current_chunk:
|
43 |
+
chunks.append(tokenizer.convert_tokens_to_string(current_chunk))
|
44 |
+
return chunks
|
45 |
+
|
46 |
+
def translate(text, model, tokenizer, src_lang='uzb_Cyrl', tgt_lang='rus_Cyrl'):
|
47 |
+
tokenizer.src_lang = src_lang
|
48 |
+
tokenizer.tgt_lang = tgt_lang
|
49 |
+
chunks = split_into_chunks(text, tokenizer)
|
50 |
+
translated_chunks = []
|
51 |
+
for chunk in chunks:
|
52 |
+
inputs = tokenizer(chunk, return_tensors='pt', padding=True, truncation=True, max_length=128)
|
53 |
+
outputs = model.generate(inputs['input_ids'], forced_bos_token_id=tokenizer.lang_code_to_id[tgt_lang])
|
54 |
+
translated_chunks.append(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
55 |
+
return ' '.join(translated_chunks)
|
56 |
+
|
57 |
+
def summarize(text, model, tokenizer, max_length=250):
|
58 |
+
input_ids = tokenizer.encode("summarize: " + text, return_tensors="pt", max_length=2048, truncation=True)
|
59 |
+
summary_ids = model.generate(input_ids, max_length=max_length, length_penalty=2.0, num_beams=4, early_stopping=True)
|
60 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
61 |
+
return summary
|
62 |
|
63 |
# Custom CSS styles
|
64 |
st.markdown("""
|
|
|
76 |
# Sidebar
|
77 |
st.sidebar.markdown('## Навигация')
|
78 |
uploaded_file = st.sidebar.file_uploader("Загрузите изображение с узбекским текстом...", type=["jpg", "jpeg", "png"])
|
79 |
+
process_btn = False # Define button state here
|
80 |
|
81 |
if uploaded_file:
|
82 |
image = Image.open(uploaded_file)
|
83 |
st.sidebar.image(image, caption='Загруженное изображение', use_column_width=True)
|
84 |
process_btn = st.sidebar.button("Перевести и суммаризировать")
|
85 |
|
86 |
+
# Title and Description
|
87 |
st.markdown('<h1 class="big-font">Текстовая обработка изображений</h1>', unsafe_allow_html=True)
|
88 |
st.markdown('<div class="big-font">Перевод с узбекского на русский и суммаризация</div>', unsafe_allow_html=True)
|
89 |
|
90 |
+
if process_btn and uploaded_file:
|
91 |
st.write("Процесс извлечения текста...")
|
92 |
+
extracted_text, confidence = extract_text(image, 'uzb_Cyrl') # Adjust the language code if necessary
|
93 |
st.write("Извлеченный текст:")
|
94 |
st.text_area("Результат", extracted_text, height=150)
|
95 |
st.write(f"Точность распознавания: {confidence*100:.2f}%")
|