gottdammer commited on
Commit
c3ef433
1 Parent(s): dd891b3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+ from PIL import Image
4
+
5
+
6
+ def classify_img(im):
7
+ im = Image.fromarray(im.astype('uint8'), 'RGB')
8
+ ans = image_cla(im)
9
+ labels = {v["label"]: v["score"] for v in ans}
10
+ return labels
11
+
12
+
13
+ def voice2text(audio):
14
+ text = voice_cla(audio)["text"]
15
+ return text
16
+
17
+
18
+ def text2sentiment(text):
19
+ sentiment = text_cla(text)[0]["label"]
20
+ return sentiment
21
+
22
+
23
+ def make_block(dem):
24
+ with dem:
25
+ gr.Markdown("""
26
+
27
+ # Ejemplo de `space` multiclassifier: Curso Platzi""")
28
+ with gr.Tabs():
29
+ with gr.TabItem("Transcribe audio en español"):
30
+ with gr.Row():
31
+ audio = gr.Audio(source="microphone", type="filepath")
32
+ transcripcion = gr.Textbox()
33
+ b1 = gr.Button("Voz a Texto")
34
+
35
+ with gr.TabItem("Análisis de sentimiento en español"):
36
+ with gr.Row():
37
+ texto = gr.Textbox()
38
+ label = gr.Label()
39
+ b2 = gr.Button("Texto a Sentimiento")
40
+ with gr.TabItem("Clasificación de Imágenes"):
41
+ with gr.Row():
42
+ image = gr.Image(label="Carga una imagen aquí")
43
+ label_image = gr.Label(num_top_classes=5)
44
+ b3 = gr.Button("Clasifica")
45
+
46
+ b1.click(voice2text, inputs=audio, outputs=transcripcion)
47
+ b2.click(text2sentiment, inputs=texto, outputs=label)
48
+ b3.click(classify_img, inputs=image, outputs=label_image)
49
+
50
+
51
+ if __name__ == '__main__':
52
+ image_cla = pipeline("image-classification", model="microsoft/swin-tiny-patch4-window7-224")
53
+ voice_cla = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-large-xlsr-53-spanish")
54
+ text_cla = pipeline("text-classification", model="pysentimiento/robertuito-sentiment-analysis")
55
+
56
+ demo = gr.Blocks()
57
+ make_block(demo)
58
+ demo.launch()
59
+