Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import (
|
| 2 |
+
AutoTokenizer,
|
| 3 |
+
AutoModelForSeq2SeqLM,
|
| 4 |
+
pipeline
|
| 5 |
+
)
|
| 6 |
+
from textblob import TextBlob as tb
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/GODEL-v1_1-base-seq2seq")
|
| 10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/GODEL-v1_1-base-seq2seq")
|
| 11 |
+
pipe = pipeline(model="aware-ai/whisper-base-german")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def translate(text):
|
| 16 |
+
blob = tb(text)
|
| 17 |
+
translation = str(blob.translate(from_lang='de',to='en'))
|
| 18 |
+
return translation
|
| 19 |
+
|
| 20 |
+
def translate_to_de(text):
|
| 21 |
+
blob = tb(text)
|
| 22 |
+
translation = str(blob.translate(from_lang='en',to='de'))
|
| 23 |
+
return translation
|
| 24 |
+
|
| 25 |
+
def transcribe(audio):
|
| 26 |
+
text = pipe(audio)["text"]
|
| 27 |
+
return text
|
| 28 |
+
|
| 29 |
+
def generate(input, knowledge):
|
| 30 |
+
|
| 31 |
+
if knowledge == '':
|
| 32 |
+
pass
|
| 33 |
+
else:
|
| 34 |
+
knowledge = translate(knowledge)
|
| 35 |
+
|
| 36 |
+
input = translate(input)
|
| 37 |
+
|
| 38 |
+
top_p = 1
|
| 39 |
+
min_length = 8
|
| 40 |
+
max_length = 64
|
| 41 |
+
|
| 42 |
+
instruction = 'given a dialog context and related knowledge, you need to answer the question based on the knowledge.'
|
| 43 |
+
|
| 44 |
+
if knowledge != '':
|
| 45 |
+
knowledge = '[KNOWLEDGE] ' + knowledge
|
| 46 |
+
|
| 47 |
+
dialog = ' EOS '.join([input])
|
| 48 |
+
query = f"{instruction} [CONTEXT] {dialog} {knowledge}"
|
| 49 |
+
|
| 50 |
+
input_ids = tokenizer(f"{query}", return_tensors="pt").input_ids
|
| 51 |
+
outputs = model.generate(input_ids, min_length=int(
|
| 52 |
+
min_length), max_length=int(max_length), top_p=top_p, do_sample=True)
|
| 53 |
+
output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 54 |
+
|
| 55 |
+
output = translate_to_de(output)
|
| 56 |
+
return output
|
| 57 |
+
|
| 58 |
+
with gr.Blocks() as app:
|
| 59 |
+
|
| 60 |
+
conocimiento = gr.Textbox(label='Conocimiento',lines=7,max_lines=7)
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
voice = gr.Audio(source='microphone',type='filepath')
|
| 64 |
+
send_button = gr.Button(value='Transcribir')
|
| 65 |
+
button2 = gr.Button(value='Respuesta de la IA')
|
| 66 |
+
|
| 67 |
+
transc = gr.Textbox(label='Transcripción',value='',)
|
| 68 |
+
respuesta = gr.Textbox(label='Respuesta',interactive=False,value='')
|
| 69 |
+
|
| 70 |
+
send_button.click(fn=transcribe,inputs=voice,outputs=transc)
|
| 71 |
+
button2.click(fn=generate,inputs=[transc,conocimiento],outputs=respuesta)
|
| 72 |
+
|
| 73 |
+
app.launch()
|