Spaces:
Runtime error
Runtime error
upd: voice flow
Browse files
app.py
CHANGED
|
@@ -21,7 +21,9 @@ model_ru2en = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-ru-en"
|
|
| 21 |
tokenizer_en2ru = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ru")
|
| 22 |
model_en2ru = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-ru")
|
| 23 |
|
| 24 |
-
transcriber = pipeline(
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
def translate_ru2en(text):
|
|
@@ -70,7 +72,7 @@ def generate_answer(image, question):
|
|
| 70 |
return answer_ru
|
| 71 |
|
| 72 |
|
| 73 |
-
def transcribe(
|
| 74 |
sr, y = new_chunk
|
| 75 |
|
| 76 |
# Convert to mono if stereo
|
|
@@ -80,22 +82,35 @@ def transcribe(stream, new_chunk):
|
|
| 80 |
y = y.astype(np.float32)
|
| 81 |
y /= np.max(np.abs(y))
|
| 82 |
|
| 83 |
-
|
| 84 |
-
stream = np.concatenate([stream, y])
|
| 85 |
-
else:
|
| 86 |
-
stream = y
|
| 87 |
-
return stream, transcriber({"sampling_rate": sr, "raw": stream})["text"]
|
| 88 |
|
| 89 |
|
| 90 |
-
|
| 91 |
fn=generate_answer,
|
| 92 |
inputs=[
|
| 93 |
gr.Image(type="pil"),
|
| 94 |
gr.Textbox(label="Вопрос (на русском)", placeholder="Ваш вопрос"),
|
| 95 |
-
gr.Audio(sources="microphone", streaming=True, label="Голосовой ввод"),
|
| 96 |
],
|
| 97 |
outputs=gr.Textbox(label="Ответ (на русском)"),
|
| 98 |
examples=[["doc.png", "О чем данный документ?"]],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
title="Демо визуального ответчика на вопросы (на русском)",
|
| 100 |
description=(
|
| 101 |
"Gradio демо для модели doc-qa с переводом вопросов и ответов"
|
|
|
|
| 21 |
tokenizer_en2ru = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ru")
|
| 22 |
model_en2ru = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-ru")
|
| 23 |
|
| 24 |
+
transcriber = pipeline(
|
| 25 |
+
"automatic-speech-recognition", model="artyomboyko/whisper-base-fine_tuned-ru"
|
| 26 |
+
)
|
| 27 |
|
| 28 |
|
| 29 |
def translate_ru2en(text):
|
|
|
|
| 72 |
return answer_ru
|
| 73 |
|
| 74 |
|
| 75 |
+
def transcribe(image, new_chunk):
|
| 76 |
sr, y = new_chunk
|
| 77 |
|
| 78 |
# Convert to mono if stereo
|
|
|
|
| 82 |
y = y.astype(np.float32)
|
| 83 |
y /= np.max(np.abs(y))
|
| 84 |
|
| 85 |
+
return generate_answer(image, transcriber({"sampling_rate": sr, "raw": y})["text"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
|
| 88 |
+
qa_interface = gr.Interface(
|
| 89 |
fn=generate_answer,
|
| 90 |
inputs=[
|
| 91 |
gr.Image(type="pil"),
|
| 92 |
gr.Textbox(label="Вопрос (на русском)", placeholder="Ваш вопрос"),
|
|
|
|
| 93 |
],
|
| 94 |
outputs=gr.Textbox(label="Ответ (на русском)"),
|
| 95 |
examples=[["doc.png", "О чем данный документ?"]],
|
| 96 |
+
live=False,
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Interface for real-time speech recognition
|
| 100 |
+
speech_interface = gr.Interface(
|
| 101 |
+
fn=transcribe,
|
| 102 |
+
inputs=[
|
| 103 |
+
gr.Image(type="pil"),
|
| 104 |
+
gr.Audio(source="microphone", label="Голосовой ввод"),
|
| 105 |
+
],
|
| 106 |
+
outputs=gr.Textbox(label="Распознанный текст"),
|
| 107 |
+
live=True,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Combine the interfaces in a Gradio Tabbed layout
|
| 111 |
+
interface = gr.TabbedInterface(
|
| 112 |
+
[qa_interface, speech_interface],
|
| 113 |
+
["Текстовый вопрос", "Голосовой вопрос"],
|
| 114 |
title="Демо визуального ответчика на вопросы (на русском)",
|
| 115 |
description=(
|
| 116 |
"Gradio демо для модели doc-qa с переводом вопросов и ответов"
|