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()