Alex11-321 commited on
Commit
adc6df0
·
verified ·
1 Parent(s): 6b74a74

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
14
+ # Инициализация модели для перевода
15
+ model_translation = T5ForConditionalGeneration.from_pretrained('utrobinmv/t5_translate_en_ru_zh_small_1024')
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
+
34
+ input_ids = tokenizer_translation(src_text, return_tensors="pt")
35
+ generated_tokens = model_translation.generate(**input_ids.to(device))
36
+
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)