File size: 2,263 Bytes
f9706ce
f9278f9
 
f9706ce
 
 
 
f9278f9
 
 
 
 
 
 
 
 
2a309b1
f9278f9
 
 
 
95f5e4b
470c325
 
9afe62f
f9278f9
9afe62f
f9278f9
 
9afe62f
f9278f9
 
 
 
 
 
 
2a309b1
f9278f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9afe62f
 
 
f9706ce
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
58
import gradio as gr
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from transformers import pipeline

def greet(name):
    return "Hello " + name + "!!"

def classify(text):
    return {"cat": 0.3, "dog": 0.7}

def predict_sentiment(text, model):
    if model == "finiteautomata/bertweet-base-sentiment-analysis":
        pipe = pipeline("text-classification", model="finiteautomata/bertweet-base-sentiment-analysis")
        out = pipe(text, return_all_scores=True)
        return {pred["label"]: pred["score"] for pred in out}
    elif model == "vader":
        nltk.download('vader_lexicon')
        sia = SentimentIntensityAnalyzer()
        return sia.polarity_scores(text)
        


demo = gr.Blocks()

with demo:
    gr.Markdown("A bunch of different Gradio demos in tabs.\n\nNote that generally, the code that is in each tab could be its own Gradio application!")
    with gr.Tabs():
        gr.Markdown('The most basic "Hello World"-type demo you can write')
        with gr.TabItem("Basic Hello"):
            interface = gr.Interface(fn=greet, inputs="text", outputs="text")
        with gr.TabItem("Label Output"):
            gr.Markdown("An example of a basic interface with a classification label as output")
            interface = gr.Interface(fn=classify, inputs="text", outputs="label")

        with gr.TabItem("Multiple Inputs"):
            gr.Markdown("A more complex interface for sentiment analysis with multiple inputs, including a dropdown, and some examples")
            demo = gr.Interface(
                predict_sentiment,
                [
                    gr.TextBox(placeholder="Your text input"),
                    gr.Dropdown(
                        ["finiteautomata/bertweet-base-sentiment-analysis", "vader"], label="Model"
                    ),
                ],
                "text",
                examples=[
                    ["Happy smile", "vader"],
                    ["Happy smile", "finiteautomata/bertweet-base-sentiment-analysis"],
                    ["Sad frown", "vader"],
                    ["Sad frown", "finiteautomata/bertweet-base-sentiment-analysis"],
                ]
            )


    text_button.click(flip_text, inputs=text_input, outputs=text_output)

demo.launch()