Spaces:
Runtime error
Runtime error
wira.indra
commited on
Commit
·
ca67adc
1
Parent(s):
acee695
add twitter feature
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import twitter_scraper as ts
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
|
|
@@ -17,7 +18,8 @@ sentiment_pipeline = pipeline(
|
|
| 17 |
ner_pipeline = pipeline(
|
| 18 |
"ner",
|
| 19 |
model=pretrained_ner,
|
| 20 |
-
tokenizer=pretrained_ner
|
|
|
|
| 21 |
)
|
| 22 |
|
| 23 |
examples = [
|
|
@@ -50,6 +52,23 @@ def sentiment_df(df):
|
|
| 50 |
df['Score'] = scores
|
| 51 |
return df
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def twitter_analyzer(keyword, max_tweets):
|
| 55 |
df = ts.scrape_tweets(keyword, max_tweets=max_tweets)
|
|
@@ -59,6 +78,11 @@ def twitter_analyzer(keyword, max_tweets):
|
|
| 59 |
df.groupby(["Label"])["Text"].count().plot.pie(autopct="%.1f%%", figsize=(6,6))
|
| 60 |
return fig, df[["URL", "Text", "Label", "Score"]]
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
|
| 64 |
with gr.Blocks() as demo:
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import twitter_scraper as ts
|
| 4 |
+
import pandas as pd
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
|
|
|
|
| 18 |
ner_pipeline = pipeline(
|
| 19 |
"ner",
|
| 20 |
model=pretrained_ner,
|
| 21 |
+
tokenizer=pretrained_ner,
|
| 22 |
+
grouped_entities=True
|
| 23 |
)
|
| 24 |
|
| 25 |
examples = [
|
|
|
|
| 52 |
df['Score'] = scores
|
| 53 |
return df
|
| 54 |
|
| 55 |
+
def ner_df(df):
|
| 56 |
+
text_list = list(df["Text"].astype(str).values)
|
| 57 |
+
label_list = list(df["Label"].astype(str).values)
|
| 58 |
+
result = [ner(text) for text in text_list]
|
| 59 |
+
terms = []
|
| 60 |
+
sentiments = []
|
| 61 |
+
ent = ['PER', 'NOR']
|
| 62 |
+
for i, preds in enumerate(result):
|
| 63 |
+
for pred in preds['entities']:
|
| 64 |
+
if pred['entity_group'] in ent:
|
| 65 |
+
terms.append(pred['word'])
|
| 66 |
+
sentiments.append(label_list[i])
|
| 67 |
+
df_ner = pd.DataFrame(columns=['Entity', 'Sentiment'])
|
| 68 |
+
df_ner['Entity'] = terms
|
| 69 |
+
df_ner['Sentiment'] = sentiments
|
| 70 |
+
return df_ner
|
| 71 |
+
|
| 72 |
|
| 73 |
def twitter_analyzer(keyword, max_tweets):
|
| 74 |
df = ts.scrape_tweets(keyword, max_tweets=max_tweets)
|
|
|
|
| 78 |
df.groupby(["Label"])["Text"].count().plot.pie(autopct="%.1f%%", figsize=(6,6))
|
| 79 |
return fig, df[["URL", "Text", "Label", "Score"]]
|
| 80 |
|
| 81 |
+
def ner_analyzer(keyword, df_ner):
|
| 82 |
+
df_ner = df_ner[df_ner.line_race != keyword]
|
| 83 |
+
fig = plt.figure()
|
| 84 |
+
return fig
|
| 85 |
+
|
| 86 |
if __name__ == "__main__":
|
| 87 |
|
| 88 |
with gr.Blocks() as demo:
|