wira.indra commited on
Commit
faf61e8
1 Parent(s): 42535f1

add twitter feature

Browse files
Files changed (2) hide show
  1. app.py +68 -5
  2. requirements.txt +5 -1
app.py CHANGED
@@ -1,4 +1,10 @@
1
  from transformers import pipeline
 
 
 
 
 
 
2
 
3
  import gradio as gr
4
  from gradio.mix import Parallel
@@ -28,11 +34,28 @@ examples = [
28
  def sentiment_analysis(text):
29
  output = sentiment_pipeline(text)
30
  return {elm["label"]: elm["score"] for elm in output[0]}
31
-
32
  def ner(text):
33
  output = ner_pipeline(text)
34
  return {"text": text, "entities": output}
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  sentiment_demo = gr.Interface(
37
  fn=sentiment_analysis,
38
  inputs="text",
@@ -45,7 +68,47 @@ ner_demo = gr.Interface(
45
  examples=examples)
46
 
47
  if __name__ == "__main__":
48
- Parallel(sentiment_demo, ner_demo,
49
- inputs=gr.Textbox(lines=10, label="Input Text", placeholder="Enter sentences here..."),
50
- title="Entity Based Sentiment Analysis Indonesia",
51
- examples=examples).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from transformers import pipeline
2
+ import pandas as pd
3
+ import re
4
+ from tqdm import tqdm
5
+ import matplotlib.pyplot as plt
6
+ import twitter_scraper as ts
7
+
8
 
9
  import gradio as gr
10
  from gradio.mix import Parallel
 
34
  def sentiment_analysis(text):
35
  output = sentiment_pipeline(text)
36
  return {elm["label"]: elm["score"] for elm in output[0]}
37
+
38
  def ner(text):
39
  output = ner_pipeline(text)
40
  return {"text": text, "entities": output}
41
 
42
+ def sentiment_df(df):
43
+ text_list = list(df["Text"].astype(str).values)
44
+ result = [sentiment_analysis(text) for text in text_list]
45
+ df['Label'] = [pred['label'] for pred in result]
46
+ df['Score'] = [round(pred['Score'], 3) for pred in result]
47
+ return df
48
+
49
+
50
+ def twitter_analyzer(keyword, max_tweets):
51
+ df = ts.scrape_tweets(keyword, max_tweets=max_tweets)
52
+ df["Text"] = df["Text"].apply(ts.preprocess_text)
53
+ print("Analyzing sentiment...")
54
+ df = sentiment_df(df)
55
+ fig = plt.figure()
56
+ df.groupby(["Label"])["Text"].count().plot.pie(autopct="%.1f%%", figsize=(6,6))
57
+ return fig, df[["URL", "Text", "Label", "Score"]]
58
+
59
  sentiment_demo = gr.Interface(
60
  fn=sentiment_analysis,
61
  inputs="text",
 
68
  examples=examples)
69
 
70
  if __name__ == "__main__":
71
+
72
+ with gr.Blocks() as demo:
73
+
74
+ gr.Markdown("""Entity Based Sentiment Analysis Indonesia""")
75
+
76
+ gr.Markdown(
77
+ """
78
+ """
79
+ )
80
+
81
+ with gr.Tab("Single Input"):
82
+ Parallel(
83
+ sentiment_demo, ner_demo,
84
+ inputs=gr.Textbox(lines=10, label="Input Text", placeholder="Enter sentences here..."),
85
+ examples=examples
86
+ )
87
+
88
+ with gr.Tab("Twitter"):
89
+ with gr.Blocks():
90
+ with gr.Row():
91
+ with gr.Column():
92
+ keyword_textbox = gr.Textbox(lines=1, label="Keyword")
93
+ max_tweets_component = gr.Number(value=10, label="Total of Tweets to Scrape", precision=0)
94
+ button = gr.Button("Submit")
95
+
96
+ plot_component = gr.Plot(label="Pie Chart of Sentiments")
97
+ dataframe_component = gr.DataFrame(type="pandas",
98
+ label="Dataframe",
99
+ max_rows=(20,'fixed'),
100
+ overflow_row_behaviour='paginate',
101
+ wrap=True)
102
+
103
+ gr.Markdown(
104
+ """
105
+
106
+ """
107
+
108
+ )
109
+
110
+ button.click(twitter_analyzer,
111
+ inputs=[keyword_textbox, max_tweets_component],
112
+ outputs=[plot_component, dataframe_component])
113
+
114
+ demo.launch(inbrowser=True)
requirements.txt CHANGED
@@ -1,2 +1,6 @@
1
  torch
2
- transformers
 
 
 
 
 
1
  torch
2
+ transformers
3
+ snscrape
4
+ pandas
5
+ matplotlib
6
+ numpy