Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,12 @@
|
|
|
|
1 |
from transformers import pipeline, T5ForConditionalGeneration, T5Tokenizer
|
2 |
import torch
|
3 |
import gradio as gr
|
|
|
4 |
|
5 |
# Проверка доступности GPU
|
6 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
7 |
|
8 |
-
# Инициализация модели для распознавания речи (ASR)
|
9 |
-
asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-medium", device=0 if device.type == "cuda" else -1)
|
10 |
-
|
11 |
# Инициализация модели для суммаризации
|
12 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=0 if device.type == "cuda" else -1)
|
13 |
|
@@ -16,18 +15,23 @@ model_translation = T5ForConditionalGeneration.from_pretrained('utrobinmv/t5_tra
|
|
16 |
model_translation.to(device)
|
17 |
tokenizer_translation = T5Tokenizer.from_pretrained('utrobinmv/t5_translate_en_ru_zh_small_1024')
|
18 |
|
19 |
-
def
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
23 |
|
24 |
-
def summarize(text, max_length=
|
25 |
-
|
26 |
-
|
|
|
|
|
27 |
return result[0]['summary_text']
|
28 |
|
29 |
def translate(text):
|
30 |
-
|
31 |
prefix = 'translate to ru: '
|
32 |
src_text = prefix + text
|
33 |
|
@@ -37,34 +41,45 @@ def translate(text):
|
|
37 |
result = tokenizer_translation.batch_decode(generated_tokens, skip_special_tokens=True)
|
38 |
return result[0]
|
39 |
|
40 |
-
def
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
return "No input provided."
|
43 |
-
|
44 |
-
#
|
45 |
-
|
46 |
-
print(f"
|
47 |
-
|
48 |
# Суммаризация текста
|
49 |
-
summary = summarize(
|
50 |
print(f"Summary: {summary}")
|
51 |
-
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
56 |
|
57 |
# Создание Gradio интерфейса
|
58 |
with gr.Blocks() as demo:
|
59 |
-
gr.Markdown("#
|
60 |
-
gr.Markdown("Upload
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
process_button = gr.Button("Process
|
66 |
|
67 |
-
process_button.click(
|
68 |
|
69 |
# Запуск приложения
|
70 |
demo.launch(debug=True)
|
|
|
1 |
+
from PyPDF2 import PdfReader
|
2 |
from transformers import pipeline, T5ForConditionalGeneration, T5Tokenizer
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
+
from gtts import gTTS
|
6 |
|
7 |
# Проверка доступности GPU
|
8 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
|
|
|
|
|
|
|
10 |
# Инициализация модели для суммаризации
|
11 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=0 if device.type == "cuda" else -1)
|
12 |
|
|
|
15 |
model_translation.to(device)
|
16 |
tokenizer_translation = T5Tokenizer.from_pretrained('utrobinmv/t5_translate_en_ru_zh_small_1024')
|
17 |
|
18 |
+
def parse_pdf(pdf_file):
|
19 |
+
"""Функция для извлечения текста из PDF файла."""
|
20 |
+
reader = PdfReader(pdf_file)
|
21 |
+
extracted_text = ""
|
22 |
+
for page in reader.pages:
|
23 |
+
extracted_text += page.extract_text() or ""
|
24 |
+
return extracted_text
|
25 |
|
26 |
+
def summarize(text, max_length=1000, min_length=150):
|
27 |
+
"""Функция для суммаризации текста."""
|
28 |
+
max_length = 1000 # Можно настроить
|
29 |
+
truncated_text = text[:max_length]
|
30 |
+
result = summarizer(truncated_text, max_length=max_length, min_length=min_length, do_sample=False)
|
31 |
return result[0]['summary_text']
|
32 |
|
33 |
def translate(text):
|
34 |
+
"""Функция для перевода текста на русский."""
|
35 |
prefix = 'translate to ru: '
|
36 |
src_text = prefix + text
|
37 |
|
|
|
41 |
result = tokenizer_translation.batch_decode(generated_tokens, skip_special_tokens=True)
|
42 |
return result[0]
|
43 |
|
44 |
+
def text_to_speech(text, language='ru'):
|
45 |
+
"""Функция для преобразования текста в аудиофайл."""
|
46 |
+
tts = gTTS(text=text, lang=language)
|
47 |
+
audio_file = "output.mp3"
|
48 |
+
tts.save(audio_file)
|
49 |
+
return audio_file
|
50 |
+
|
51 |
+
def process_pdf(pdf_file):
|
52 |
+
"""Основная функция обработки PDF файла."""
|
53 |
+
if not pdf_file:
|
54 |
return "No input provided."
|
55 |
+
|
56 |
+
# Извлечение текста из PDF
|
57 |
+
extracted_text = parse_pdf(pdf_file)
|
58 |
+
print(f"Extracted Text: {extracted_text}")
|
59 |
+
|
60 |
# Суммаризация текста
|
61 |
+
summary = summarize(extracted_text)
|
62 |
print(f"Summary: {summary}")
|
63 |
+
|
64 |
+
# Перевод текста на русский
|
65 |
+
translated_text = translate(summary)
|
66 |
+
print(f"Translated Text: {translated_text}")
|
67 |
+
|
68 |
+
# Преобразование текста в аудио
|
69 |
+
audio_file = text_to_speech(translated_text)
|
70 |
+
return translated_text, audio_file
|
71 |
|
72 |
# Создание Gradio интерфейса
|
73 |
with gr.Blocks() as demo:
|
74 |
+
gr.Markdown("# PDF Summarizer, Translator, and Text-to-Speech")
|
75 |
+
gr.Markdown("Upload a PDF file to summarize, translate to Russian, and convert to audio.")
|
76 |
|
77 |
+
pdf_input = gr.File(label="Upload PDF File", type="filepath")
|
78 |
+
text_output = gr.Textbox(label="Translated Text", lines=10)
|
79 |
+
audio_output = gr.Audio(label="Generated Audio", type="filepath")
|
80 |
+
process_button = gr.Button("Process PDF")
|
81 |
|
82 |
+
process_button.click(process_pdf, inputs=pdf_input, outputs=[text_output, audio_output])
|
83 |
|
84 |
# Запуск приложения
|
85 |
demo.launch(debug=True)
|