truongghieu
commited on
Commit
•
db67a3a
1
Parent(s):
1d0015a
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import speech_recognition as sr
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig,BitsAndBytesConfig
|
5 |
+
import torch
|
6 |
+
|
7 |
+
Medical_finetunned_model = "truongghieu/deci-finetuned_Prj2"
|
8 |
+
question_text = "This is a question"
|
9 |
+
answer_text = "This is an answer"
|
10 |
+
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
bnb_config = BitsAndBytesConfig(
|
13 |
+
load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype="float16", bnb_4bit_use_double_quant=True
|
14 |
+
)
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(Medical_finetunned_model, trust_remote_code=True)
|
16 |
+
if torch.cuda.is_available():
|
17 |
+
model = AutoModelForCausalLM.from_pretrained(Medical_finetunned_model, trust_remote_code=True, quantization_config=bnb_config)
|
18 |
+
else:
|
19 |
+
model = AutoModelForCausalLM.from_pretrained("truongghieu/deci-finetuned", trust_remote_code=True)
|
20 |
+
|
21 |
+
|
22 |
+
generation_config = GenerationConfig(
|
23 |
+
penalty_alpha=0.6,
|
24 |
+
do_sample=True,
|
25 |
+
top_k=3,
|
26 |
+
temperature=0.5,
|
27 |
+
repetition_penalty=1.2,
|
28 |
+
max_new_tokens=50,
|
29 |
+
pad_token_id=tokenizer.eos_token_id
|
30 |
+
)
|
31 |
+
|
32 |
+
def generate_text(text):
|
33 |
+
input_text = f'###Human: \"{text}\"'
|
34 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
35 |
+
output_ids = model.generate(input_ids, generation_config=generation_config)
|
36 |
+
output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
37 |
+
return output_text
|
38 |
+
|
39 |
+
|
40 |
+
def recognize_speech(audio_data):
|
41 |
+
|
42 |
+
# return text
|
43 |
+
audio_data = sr.AudioData(np.array(audio_data[1]), sample_rate=audio_data[0] , sample_width=2)
|
44 |
+
|
45 |
+
recognizer = sr.Recognizer()
|
46 |
+
try:
|
47 |
+
text = recognizer.recognize_google(audio_data)
|
48 |
+
question_text = text
|
49 |
+
return f"Recognized Speech: {text}"
|
50 |
+
|
51 |
+
except sr.UnknownValueError:
|
52 |
+
return "Speech Recognition could not understand audio."
|
53 |
+
except sr.RequestError as e:
|
54 |
+
return f"Could not request results from Google Speech Recognition service; {e}"
|
55 |
+
|
56 |
+
|
57 |
+
# audio_input = gr.Audio(type="numpy")
|
58 |
+
|
59 |
+
# iface = gr.Interface(fn=recognize_speech, inputs=audio_input , outputs="text", title="Speech to Text")
|
60 |
+
# # create a place to generate answer
|
61 |
+
# answer_text = gr.Textbox(label="Answer")
|
62 |
+
# iface_answer = gr.Interface(fn=generate_text, inputs=question_text , outputs="text", title="Answer")
|
63 |
+
|
64 |
+
# iface.launch()
|
65 |
+
|
66 |
+
with gr.Blocks as demo:
|
67 |
+
with gr.Row():
|
68 |
+
gr.Label("Speech Recognition")
|
69 |
+
inp = gr.Audio(type="numpy")
|
70 |
+
out_text_predict = gr.Textbox(label="Recognized Speech")
|
71 |
+
button = gr.Button(label="Recognize Speech", type="button")
|
72 |
+
button.click(recognize_speech, inp, out_text_predict)
|
73 |
+
with gr.Row():
|
74 |
+
out_answer = gr.Textbox(label="Answer")
|
75 |
+
button_answer = gr.Button(label="Generate Answer", type="button")
|
76 |
+
button_answer.click(generate_text, out_text_predict, out_answer)
|
77 |
+
demo.launch()
|