File size: 1,726 Bytes
e06fb68
9338a37
1b1c1e7
 
c49fc60
 
9338a37
1b1c1e7
 
 
 
 
 
 
9338a37
 
 
 
 
 
 
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
import gradio as gr
from PIL import Image
from transformers import pipeline

transcribe = pipeline("automatic-speech-recognition", model = "facebook/wav2vec2-large-xlsr-53-spanish")
classifier = pipeline("text-classification", model = "pysentimiento/robertuito-sentiment-analysis")
image_classifier = pipeline("image-classification", model="microsoft/swin-tiny-patch4-window7-224")

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

def text_to_sentiment(text):
  return classifier(text)[0]["label"]
    
def classify_image(image): 
  image = Image.fromarray(image.astype('uint8'), 'RGB')
  answers = image_classifier(image)
  labels = {answer["label"]: answer["score"] for answer in answers}
  return labels
    
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()