Update app.py
Browse files
app.py
CHANGED
@@ -3,60 +3,16 @@ from transformers import RobertaForQuestionAnswering
|
|
3 |
from transformers import BertForQuestionAnswering
|
4 |
from transformers import AutoTokenizer
|
5 |
from transformers import pipeline
|
6 |
-
import soundfile as sf
|
7 |
-
import torch
|
8 |
-
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
9 |
-
import sox
|
10 |
-
import subprocess
|
11 |
|
12 |
|
13 |
-
def read_file_and_process(wav_file):
|
14 |
-
filename = wav_file.split('.')[0]
|
15 |
-
filename_16k = filename + "16k.wav"
|
16 |
-
resampler(wav_file, filename_16k)
|
17 |
-
speech, _ = sf.read(filename_16k)
|
18 |
-
inputs = processor(speech, sampling_rate=16_000, return_tensors="pt", padding=True)
|
19 |
-
|
20 |
-
return inputs
|
21 |
-
|
22 |
-
|
23 |
-
def resampler(input_file_path, output_file_path):
|
24 |
-
command = (
|
25 |
-
f"ffmpeg -hide_banner -loglevel panic -i {input_file_path} -ar 16000 -ac 1 -bits_per_raw_sample 16 -vn "
|
26 |
-
f"{output_file_path}"
|
27 |
-
)
|
28 |
-
subprocess.call(command, shell=True)
|
29 |
-
|
30 |
-
|
31 |
-
def parse_transcription(logits):
|
32 |
-
predicted_ids = torch.argmax(logits, dim=-1)
|
33 |
-
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
34 |
-
return transcription
|
35 |
-
|
36 |
-
|
37 |
-
def parse(wav_file):
|
38 |
-
input_values = read_file_and_process(wav_file)
|
39 |
-
with torch.no_grad():
|
40 |
-
logits = model(**input_values).logits
|
41 |
-
user_question = parse_transcription(logits)
|
42 |
-
return user_question
|
43 |
-
|
44 |
-
|
45 |
-
model_id = "jonatasgrosman/wav2vec2-large-xlsr-53-persian"
|
46 |
-
processor = Wav2Vec2Processor.from_pretrained(model_id)
|
47 |
-
model = Wav2Vec2ForCTC.from_pretrained(model_id)
|
48 |
-
|
49 |
model1 = RobertaForQuestionAnswering.from_pretrained("pedramyazdipoor/persian_xlm_roberta_large")
|
50 |
tokenizer1 = AutoTokenizer.from_pretrained("pedramyazdipoor/persian_xlm_roberta_large")
|
51 |
|
52 |
|
53 |
roberta_large = pipeline(task='question-answering', model=model1, tokenizer=tokenizer1)
|
54 |
|
55 |
-
def Q_A(
|
56 |
-
|
57 |
-
question = parse(audio)
|
58 |
-
elif audio is None:
|
59 |
-
question = text
|
60 |
answer_pedram = roberta_large({"question":question, "context":context})['answer']
|
61 |
return answer_pedram
|
62 |
|
@@ -70,10 +26,6 @@ article = "آموزش داده شده با مدل زبانی روبرتا"
|
|
70 |
|
71 |
demo = gr.Interface(fn=Q_A, # mapping function from input to output
|
72 |
inputs=[gr.Textbox(label='پرسش خود را وارد کنید:', show_label=True, text_align='right', lines=2),
|
73 |
-
gr.Audio(source="microphone", type="filepath",
|
74 |
-
label="لطفا دکمه ضبط صدا را بزنید و شروع به صحبت کنید و بعذ از اتمام صحبت دوباره دکمه ضبط را فشار دهید.",
|
75 |
-
show_download_button=True,
|
76 |
-
show_edit_button=True,),
|
77 |
gr.Textbox(label='متن منبع خود را وارد کنید', show_label=True, text_align='right', lines=8)], # what are the inputs?
|
78 |
outputs=gr.Text(show_copy_button=True), # what are the outputs?
|
79 |
# our fn has two outputs, therefore we have two outputs
|
|
|
3 |
from transformers import BertForQuestionAnswering
|
4 |
from transformers import AutoTokenizer
|
5 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
model1 = RobertaForQuestionAnswering.from_pretrained("pedramyazdipoor/persian_xlm_roberta_large")
|
9 |
tokenizer1 = AutoTokenizer.from_pretrained("pedramyazdipoor/persian_xlm_roberta_large")
|
10 |
|
11 |
|
12 |
roberta_large = pipeline(task='question-answering', model=model1, tokenizer=tokenizer1)
|
13 |
|
14 |
+
def Q_A(question, context):
|
15 |
+
|
|
|
|
|
|
|
16 |
answer_pedram = roberta_large({"question":question, "context":context})['answer']
|
17 |
return answer_pedram
|
18 |
|
|
|
26 |
|
27 |
demo = gr.Interface(fn=Q_A, # mapping function from input to output
|
28 |
inputs=[gr.Textbox(label='پرسش خود را وارد کنید:', show_label=True, text_align='right', lines=2),
|
|
|
|
|
|
|
|
|
29 |
gr.Textbox(label='متن منبع خود را وارد کنید', show_label=True, text_align='right', lines=8)], # what are the inputs?
|
30 |
outputs=gr.Text(show_copy_button=True), # what are the outputs?
|
31 |
# our fn has two outputs, therefore we have two outputs
|