Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib.pyplot as plt
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
3 |
+
import tweepy
|
4 |
+
|
5 |
+
|
6 |
+
model = AutoModelForSequenceClassification.from_pretrained("savasy/bert-base-turkish-sentiment-cased")
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("savasy/bert-base-turkish-sentiment-cased")
|
8 |
+
sa= pipeline("sentiment-analysis", tokenizer=tokenizer, model=model)
|
9 |
+
|
10 |
+
|
11 |
+
def adjust(x):
|
12 |
+
if x<0:
|
13 |
+
return 2*x+1
|
14 |
+
return 2*x-1
|
15 |
+
def sa2(s):
|
16 |
+
res= sa(s)
|
17 |
+
return [adjust(-1*r['score']) if r['label']=='negative' else adjust(r['score']) for r in res ]
|
18 |
+
|
19 |
+
def get_examples():
|
20 |
+
#return [e for e in open("examplesTR.csv").readlines()]
|
21 |
+
return [["#demokrasi","100","","","",""]]
|
22 |
+
|
23 |
+
def grfunc(key, number_of_tweets,consumer_key, consumer_secret,acc_token,acc_secret):
|
24 |
+
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
|
25 |
+
auth.set_access_token(acc_token, acc_secret)
|
26 |
+
api = tweepy.API(auth)
|
27 |
+
msgs = []
|
28 |
+
for tweet in tweepy.Cursor(api.search, q=key, lang='tr', rpp=100).items(50):
|
29 |
+
msgs.append(tweet.text)
|
30 |
+
c2=[s.strip() for s in msgs if len(s.split())>2]
|
31 |
+
df["scores"]= sa2(c2)
|
32 |
+
df.plot(kind='hist')
|
33 |
+
return plt.gcf()
|
34 |
+
|
35 |
+
import gradio as gr
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=grfunc,
|
38 |
+
inputs=[gr.inputs.Textbox(placeholder="put your #hashtag"),
|
39 |
+
gr.inputs.Textbox(placeholder="the number of tweets",default=100),
|
40 |
+
gr.inputs.Textbox(placeholder="consumer_key"),
|
41 |
+
gr.inputs.Textbox(placeholder="consumer_secret"),
|
42 |
+
gr.inputs.Textbox(placeholder="access_key"),
|
43 |
+
gr.inputs.Textbox(placeholder="access_secret")
|
44 |
+
],
|
45 |
+
outputs="plot",
|
46 |
+
examples=get_examples())
|
47 |
+
iface.launch()
|