Alex11-321 commited on
Commit
2ffbc3f
·
verified ·
1 Parent(s): de6c8cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -31
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 transcribe_audio(audio_file):
20
- # Преобразование аудио в текст
21
- result = asr_pipeline(audio_file)
22
- return result["text"]
 
 
 
23
 
24
- def summarize(text, max_length=300, min_length=150):
25
- # Суммаризация текста
26
- result = summarizer(text, max_length=max_length, min_length=min_length, do_sample=False)
 
 
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 process_audio(audio_file, language):
41
- if not audio_file:
 
 
 
 
 
 
 
 
42
  return "No input provided."
43
-
44
- # Преобразование аудио в текст
45
- transcribed_text = transcribe_audio(audio_file)
46
- print(f"Transcribed Text: {transcribed_text}")
47
-
48
  # Суммаризация текста
49
- summary = summarize(transcribed_text)
50
  print(f"Summary: {summary}")
51
-
52
- # Перевод, если выбран русский язык
53
- if language == "rus":
54
- return translate(summary)
55
- return summary
 
 
 
56
 
57
  # Создание Gradio интерфейса
58
  with gr.Blocks() as demo:
59
- gr.Markdown("# Audio Summarizer and Translator")
60
- gr.Markdown("Upload an audio file to summarize and translate (if needed).")
61
 
62
- language = gr.Radio(choices=["rus", "eng"], label="Output Language", value="rus")
63
- audio_input = gr.Audio(label="Upload Audio File", type="filepath")
64
- text_output = gr.Textbox(label="Processed Text", lines=10)
65
- process_button = gr.Button("Process Audio")
66
 
67
- process_button.click(process_audio, inputs=[audio_input, language], outputs=text_output)
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)