lbiester commited on
Commit
f9278f9
·
verified ·
1 Parent(s): 95f5e4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -9
app.py CHANGED
@@ -1,23 +1,55 @@
1
  import gradio as gr
 
 
2
 
3
  def greet(name):
4
  return "Hello " + name + "!!"
5
 
6
- def flip_text(x):
7
- return x[::-1]
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  demo = gr.Blocks()
10
 
11
  with demo:
12
- gr.Markdown("Flip text or image files using this demo.")
13
  with gr.Tabs():
14
- with gr.TabItem("Flip Text"):
15
- with gr.Row():
16
- text_input = gr.Textbox()
17
- text_output = gr.Textbox()
18
- text_button = gr.Button("Flip")
19
- with gr.TabItem("Flip Image"):
20
  interface = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  text_button.click(flip_text, inputs=text_input, outputs=text_output)
23
 
 
1
  import gradio as gr
2
+ from nltk.sentiment.vader import SentimentIntensityAnalyzer
3
+ from transformers import pipeline
4
 
5
  def greet(name):
6
  return "Hello " + name + "!!"
7
 
8
+ def classify(text):
9
+ return {"cat": 0.3, "dog": 0.7}
10
+
11
+ def predict_sentiment(text, model):
12
+ if model == "finiteautomata/bertweet-base-sentiment-analysis":
13
+ pipe = pipeline("text-classification", model="finiteautomata/bertweet-base-sentiment-analysis")
14
+ out = pipe(text, return_all_scores=True)
15
+ return {pred["label"]: pred["score"] for pred in out}
16
+ elif model == "vader":
17
+ sia = SentimentIntensityAnalyzer()
18
+ return sia.polarity_scores(text)
19
+
20
+
21
 
22
  demo = gr.Blocks()
23
 
24
  with demo:
25
+ 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!")
26
  with gr.Tabs():
27
+ gr.Markdown('The most basic "Hello World"-type demo you can write')
28
+ with gr.TabItem("Basic Hello"):
 
 
 
 
29
  interface = gr.Interface(fn=greet, inputs="text", outputs="text")
30
+ with gr.TabItem("Label Output"):
31
+ gr.Markdown("An example of a basic interface with a classification label as output")
32
+ interface = gr.Interface(fn=classify, inputs="text", outputs="label")
33
+
34
+ with gr.TabItem("Multiple Inputs"):
35
+ gr.Markdown("A more complex interface for sentiment analysis with multiple inputs, including a dropdown, and some examples")
36
+ demo = gr.Interface(
37
+ sentence_builder,
38
+ [
39
+ gr.TextBox(placeholder="Your text input"),
40
+ gr.Dropdown(
41
+ ["finiteautomata/bertweet-base-sentiment-analysis", "vader"], label="Model"
42
+ ),
43
+ ],
44
+ "text",
45
+ examples=[
46
+ ["Happy smile", "vader"],
47
+ ["Happy smile", "finiteautomata/bertweet-base-sentiment-analysis"],
48
+ ["Sad frown", "vader"],
49
+ ["Sad frown", "finiteautomata/bertweet-base-sentiment-analysis"],
50
+ ]
51
+ )
52
+
53
 
54
  text_button.click(flip_text, inputs=text_input, outputs=text_output)
55