nimool commited on
Commit
f417b2c
1 Parent(s): cd8db40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -3
app.py CHANGED
@@ -3,15 +3,62 @@ from transformers import RobertaForQuestionAnswering
3
  from transformers import BertForQuestionAnswering
4
  from transformers import AutoTokenizer
5
  from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  model1 = RobertaForQuestionAnswering.from_pretrained("pedramyazdipoor/persian_xlm_roberta_large")
8
  tokenizer1 = AutoTokenizer.from_pretrained("pedramyazdipoor/persian_xlm_roberta_large")
9
 
 
10
  roberta_large = pipeline(task='question-answering', model=model1, tokenizer=tokenizer1)
11
 
12
- def Q_A(question, context):
13
- answer_pedram = roberta_large({"question":question, "context":context})['answer']
14
- return answer_pedram
 
 
 
 
15
 
16
 
17
 
@@ -23,6 +70,10 @@ article = "آموزش داده شده با مدل زبانی روبرتا"
23
 
24
  demo = gr.Interface(fn=Q_A, # mapping function from input to output
25
  inputs=[gr.Textbox(label='پرسش خود را وارد کنید:', show_label=True, text_align='right', lines=2),
 
 
 
 
26
  gr.Textbox(label='متن منبع خود را وارد کنید', show_label=True, text_align='right', lines=8)], # what are the inputs?
27
  outputs=gr.Text(show_copy_button=True), # what are the outputs?
28
  # 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
+ 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(text=None, audio=None, context):
56
+ if text is None:
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
 
63
 
64
 
 
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