File size: 1,907 Bytes
e06fb68
1b1c1e7
 
e06fb68
1b1c1e7
 
e06fb68
1b1c1e7
 
 
c49fc60
 
 
1b1c1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
import requests
from numpy import asarray
import tensorflow as tf
from transformers import pipeline

inception_net = tf.keras.applications.MobileNetV2()
answer = requests.get("https://git.io/JJkYN")
labels =answer.text.split("\n")

transcribe = pipeline("automatic-speech-recognition", model = "facebook/wav2vec2-large-xlsr-53-spanish")
classifier = pipeline("text-classification", model = "pysentimiento/robertuito-sentiment-analysis")

def classify_image(inp): 
  inp = asarray(inp.resize((224, 224)))
  inp = inp.reshape((-1,) + inp.shape) 
  inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp) 
  prediction = inception_net.predict(inp).flatten() 
  confidences = {labels[k]: float(prediction[k]) for k in range(1000)} 
  return confidences

def audio_to_text(audio):
  text = transcribe(audio)["text"]
  return text

def text_to_sentiment(text):
  return classifier(text)[0]["label"]

demo = gr.Blocks()

with demo:
  gr.Markdown("Example with Gradio Blocks")
  with gr.Tabs():
    with gr.TabItem("Transcribe audio in Spanish"):
      with gr.Row():
        audio = gr.Audio(sources="microphone", type="filepath")
        transcription = gr.Textbox()
      transcribeButton = gr.Button("Transcribe")

    with gr.TabItem("Sentiment analysis in English and Spanish"):
      with gr.Row():
        text = gr.Textbox()
        label = gr.Label()
      sentimentButton = gr.Button("Calculate sentiment")

    with gr.TabItem("Image Classification"):
      with gr.Row():
        image = gr.Image(label="Upload an image here")
        label_image = gr.Label(num_top_classes=3)
      classifyButton = gr.Button("Classify image")

    transcribeButton.click(audio_to_text, inputs = audio, outputs=transcription)
    sentimentButton.click(text_to_sentiment, inputs=text, outputs=label)
    classifyButton. click(classify_image, inputs=image, outputs=label_image)

demo.launch()