Spaces:
Sleeping
Sleeping
from PyPDF2 import PdfReader | |
from transformers import pipeline, T5ForConditionalGeneration, T5Tokenizer | |
import torch | |
import gradio as gr | |
from gtts import gTTS | |
# Проверка доступности GPU | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
# Инициализация модели для суммаризации | |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=0 if device.type == "cuda" else -1) | |
# Инициализация модели для перевода | |
model_translation = T5ForConditionalGeneration.from_pretrained('utrobinmv/t5_translate_en_ru_zh_small_1024') | |
model_translation.to(device) | |
tokenizer_translation = T5Tokenizer.from_pretrained('utrobinmv/t5_translate_en_ru_zh_small_1024') | |
def parse_pdf(pdf_file): | |
"""Функция для извлечения текста из PDF файла.""" | |
reader = PdfReader(pdf_file) | |
extracted_text = "" | |
for page in reader.pages: | |
extracted_text += page.extract_text() or "" | |
return extracted_text | |
def summarize(text, max_length=1000, min_length=150): | |
"""Функция для суммаризации текста.""" | |
max_length = 1000 # Можно настроить | |
truncated_text = text[:max_length] | |
result = summarizer(truncated_text, max_length=max_length, min_length=min_length, do_sample=False) | |
return result[0]['summary_text'] | |
def translate(text): | |
"""Функция для перевода текста на русский.""" | |
prefix = 'translate to ru: ' | |
src_text = prefix + text | |
input_ids = tokenizer_translation(src_text, return_tensors="pt") | |
generated_tokens = model_translation.generate(**input_ids.to(device)) | |
result = tokenizer_translation.batch_decode(generated_tokens, skip_special_tokens=True) | |
return result[0] | |
def text_to_speech(text, language='ru'): | |
"""Функция для преобразования текста в аудиофайл.""" | |
tts = gTTS(text=text, lang=language) | |
audio_file = "output.mp3" | |
tts.save(audio_file) | |
return audio_file | |
def process_pdf(pdf_file): | |
"""Основная функция обработки PDF файла.""" | |
if not pdf_file: | |
return "No input provided." | |
# Извлечение текста из PDF | |
extracted_text = parse_pdf(pdf_file) | |
print(f"Extracted Text: {extracted_text}") | |
# Суммаризация текста | |
summary = summarize(extracted_text) | |
print(f"Summary: {summary}") | |
# Перевод текста на русский | |
translated_text = translate(summary) | |
print(f"Translated Text: {translated_text}") | |
# Преобразование текста в аудио | |
audio_file = text_to_speech(translated_text) | |
return translated_text, audio_file | |
# Создание Gradio интерфейса | |
with gr.Blocks() as demo: | |
gr.Markdown("# PDF Summarizer, Translator, and Text-to-Speech") | |
gr.Markdown("Upload a PDF file to summarize, translate to Russian, and convert to audio.") | |
pdf_input = gr.File(label="Upload PDF File", type="filepath") | |
text_output = gr.Textbox(label="Translated Text", lines=10) | |
audio_output = gr.Audio(label="Generated Audio", type="filepath") | |
process_button = gr.Button("Process PDF") | |
process_button.click(process_pdf, inputs=pdf_input, outputs=[text_output, audio_output]) | |
# Запуск приложения | |
demo.launch(debug=True) |