Spaces:
Runtime error
Runtime error
udp: voice input
Browse files
app.py
CHANGED
|
@@ -4,23 +4,28 @@ from transformers import (
|
|
| 4 |
AutoModelForSeq2SeqLM,
|
| 5 |
AutoProcessor,
|
| 6 |
AutoModelForDocumentQuestionAnswering,
|
|
|
|
| 7 |
)
|
| 8 |
import torch
|
|
|
|
| 9 |
|
| 10 |
processor = AutoProcessor.from_pretrained(
|
| 11 |
-
"
|
| 12 |
)
|
| 13 |
model = AutoModelForDocumentQuestionAnswering.from_pretrained(
|
| 14 |
-
"
|
| 15 |
)
|
| 16 |
|
| 17 |
tokenizer_ru2en = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ru-en")
|
| 18 |
model_ru2en = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-ru-en")
|
| 19 |
-
|
| 20 |
tokenizer_en2ru = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ru")
|
| 21 |
model_en2ru = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-ru")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
| 24 |
def translate_ru2en(text):
|
| 25 |
inputs = tokenizer_ru2en(text, return_tensors="pt")
|
| 26 |
outputs = model_ru2en.generate(**inputs)
|
|
@@ -35,8 +40,8 @@ def translate_en2ru(text):
|
|
| 35 |
return translated_text
|
| 36 |
|
| 37 |
|
|
|
|
| 38 |
def generate_answer_git(image, question):
|
| 39 |
-
|
| 40 |
with torch.no_grad():
|
| 41 |
encoding = processor(
|
| 42 |
images=image,
|
|
@@ -68,25 +73,40 @@ def generate_answer(image, question):
|
|
| 68 |
return answer_ru
|
| 69 |
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
|
|
|
| 75 |
interface = gr.Interface(
|
| 76 |
fn=generate_answer,
|
| 77 |
inputs=[
|
| 78 |
gr.Image(type="pil"),
|
| 79 |
gr.Textbox(label="Вопрос (на русском)", placeholder="Ваш вопрос"),
|
|
|
|
| 80 |
],
|
| 81 |
outputs=gr.Textbox(label="Ответ (на русском)"),
|
| 82 |
-
examples=
|
| 83 |
title="Демо визуального ответчика на вопросы (на русском)",
|
| 84 |
description=(
|
| 85 |
"Gradio демо для модели doc-qa с переводом вопросов и ответов"
|
| 86 |
"на русский язык. Загрузите изображение и задайте вопрос, чтобы"
|
| 87 |
"получить ответ. Вы также можете использовать голосовой ввод!"
|
| 88 |
),
|
| 89 |
-
|
| 90 |
)
|
| 91 |
|
| 92 |
interface.launch(debug=True, share=True)
|
|
|
|
| 4 |
AutoModelForSeq2SeqLM,
|
| 5 |
AutoProcessor,
|
| 6 |
AutoModelForDocumentQuestionAnswering,
|
| 7 |
+
pipeline,
|
| 8 |
)
|
| 9 |
import torch
|
| 10 |
+
import numpy as np
|
| 11 |
|
| 12 |
processor = AutoProcessor.from_pretrained(
|
| 13 |
+
"andgrt/layoutlmv2-base-uncased_finetuned_docvqa"
|
| 14 |
)
|
| 15 |
model = AutoModelForDocumentQuestionAnswering.from_pretrained(
|
| 16 |
+
"andgrt/layoutlmv2-base-uncased_finetuned_docvqa"
|
| 17 |
)
|
| 18 |
|
| 19 |
tokenizer_ru2en = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ru-en")
|
| 20 |
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 |
+
# Load the speech recognition model
|
| 25 |
+
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
|
| 26 |
+
|
| 27 |
|
| 28 |
+
# Functions for translation
|
| 29 |
def translate_ru2en(text):
|
| 30 |
inputs = tokenizer_ru2en(text, return_tensors="pt")
|
| 31 |
outputs = model_ru2en.generate(**inputs)
|
|
|
|
| 40 |
return translated_text
|
| 41 |
|
| 42 |
|
| 43 |
+
# Function to generate answers
|
| 44 |
def generate_answer_git(image, question):
|
|
|
|
| 45 |
with torch.no_grad():
|
| 46 |
encoding = processor(
|
| 47 |
images=image,
|
|
|
|
| 73 |
return answer_ru
|
| 74 |
|
| 75 |
|
| 76 |
+
def transcribe(stream, new_chunk):
|
| 77 |
+
sr, y = new_chunk
|
| 78 |
+
|
| 79 |
+
# Convert to mono if stereo
|
| 80 |
+
if y.ndim > 1:
|
| 81 |
+
y = y.mean(axis=1)
|
| 82 |
+
|
| 83 |
+
y = y.astype(np.float32)
|
| 84 |
+
y /= np.max(np.abs(y))
|
| 85 |
+
|
| 86 |
+
if stream is not None:
|
| 87 |
+
stream = np.concatenate([stream, y])
|
| 88 |
+
else:
|
| 89 |
+
stream = y
|
| 90 |
+
return stream, transcriber({"sampling_rate": sr, "raw": stream})["text"]
|
| 91 |
+
|
| 92 |
|
| 93 |
+
# Gradio Interface
|
| 94 |
interface = gr.Interface(
|
| 95 |
fn=generate_answer,
|
| 96 |
inputs=[
|
| 97 |
gr.Image(type="pil"),
|
| 98 |
gr.Textbox(label="Вопрос (на русском)", placeholder="Ваш вопрос"),
|
| 99 |
+
gr.Audio(source="microphone", streaming=True, label="Голосовой ввод"),
|
| 100 |
],
|
| 101 |
outputs=gr.Textbox(label="Ответ (на русском)"),
|
| 102 |
+
examples=[["doc.png", "О чем данный документ?"]],
|
| 103 |
title="Демо визуального ответчика на вопросы (на русском)",
|
| 104 |
description=(
|
| 105 |
"Gradio демо для модели doc-qa с переводом вопросов и ответов"
|
| 106 |
"на русский язык. Загрузите изображение и задайте вопрос, чтобы"
|
| 107 |
"получить ответ. Вы также можете использовать голосовой ввод!"
|
| 108 |
),
|
| 109 |
+
live=True,
|
| 110 |
)
|
| 111 |
|
| 112 |
interface.launch(debug=True, share=True)
|