File size: 1,755 Bytes
530d73e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a4b086
530d73e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbf5345
58d23bb
530d73e
 
 
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
import matplotlib.pyplot as plt
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
import tweepy


model = AutoModelForSequenceClassification.from_pretrained("savasy/bert-base-turkish-sentiment-cased")
tokenizer = AutoTokenizer.from_pretrained("savasy/bert-base-turkish-sentiment-cased")
sa= pipeline("sentiment-analysis", tokenizer=tokenizer, model=model)


def adjust(x):
  if x<0:
    return 2*x+1
  return 2*x-1
def sa2(s):
  res= sa(s)
  return [adjust(-1*r['score']) if r['label']=='negative' else adjust(r['score']) for r in res ]
   
def get_examples():
  #return [e for e in  open("examplesTR.csv").readlines()]
  return [["#demokrasi","100","","","",""]]

def grfunc(key, number_of_tweets,consumer_key, consumer_secret,acc_token,acc_secret):
  auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  auth.set_access_token(acc_token, acc_secret)
  api = tweepy.API(auth)
  msgs = []
  for tweet in tweepy.Cursor(api.search, q=key, lang='tr', rpp=50).items(number_of_tweets):
    msgs.append(tweet.text)
  c2=[s.strip() for s in msgs if len(s.split())>2]
  df["scores"]= sa2(c2)
  df.plot(kind='hist')
  return plt.gcf()
 
import gradio as gr
iface = gr.Interface(
  fn=grfunc, 
  inputs=[gr.inputs.Textbox(placeholder="put your #hashtag"),
          gr.inputs.Textbox(placeholder="the number of tweets",default=100),
          gr.inputs.Textbox(placeholder="consumer_key"),
          gr.inputs.Textbox(placeholder="consumer_secret"),
          gr.inputs.Textbox(placeholder="access_key"),
          gr.inputs.Textbox(placeholder="access_secret")
          ],
  description="Please provide your API keys from https://developer.twitter.com/en/apps", 
  outputs="plot",
  examples=get_examples())
iface.launch()