CoolFace
Apppublic

rohanphadke/markets

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py99 linesDownload Raw Back to root
1import gradio as gr2from GoogleNews import GoogleNews3import pandas as pd4import nltk5from nltk.sentiment.vader import SentimentIntensityAnalyzer6nltk.download('vader_lexicon')7nltk.download('punkt')8from newspaper import Article9import datetime as dt10 11 12googlenews = GoogleNews()13 14# Perform sentiment analysis on the headlines using NLTK's VADER15sia = SentimentIntensityAnalyzer()16 17# Define a function to calculate the sentiment scores for a given text18def get_sentiment_scores(text):19    return sia.polarity_scores(text)20 21def get_sentiment(sentiment_scores):22    compound_score = sentiment_scores['compound']23    if compound_score >= 0.05:24        return 'positive'25    elif compound_score <= -0.05:26        return 'negative'27    else:28        return 'neutral'29 30def convertToNews(df1):31    list1=[]32    for ind in df1.index:33        try:34            dict1={}35            article = Article(df1['link'][ind])36            article.download()37            article.parse()38            article.nlp()39            dict1['Date']=df1['date'][ind]40            dict1['Link'] = df1['link'][ind]41            dict1['Media']=df1['media'][ind]42            dict1['Title']=article.title43            dict1['Article']=article.text44            dict1['Summary']=article.summary45 46            list1.append(dict1)47        except:48            print('error occurred')49    news_df=pd.DataFrame(list1)50    return news_df51 52def getSentiment(keyword):53    googlenews.clear()54# Set Current Date and Yesterday's Date55    now = dt.date.today()56    now = now.strftime('%m-%d-%Y')57    yesterday = dt.date.today() - dt.timedelta(days = 1)58    yesterday = yesterday.strftime('%m-%d-%Y')59 60    googlenews.set_time_range(yesterday, now)61 62    keyword1 = keyword63    googlenews.search(keyword1)64    results = googlenews.results()65    df=pd.DataFrame(results)66 67    for i in range(2,4):68        googlenews.getpage(i)69        result=googlenews.result()70        df=pd.DataFrame(result)71 72    news = convertToNews(df)73 74    # Apply the sentiment analyzer to the article column and store the scores in a new column75    news['sentiment_scores'] = news['Summary'].apply(get_sentiment_scores)76 77    # Apply the get_sentiment function to the sentiment_scores column and store the results in a new column78    news['sentiment'] = news['sentiment_scores'].apply(get_sentiment)79 80    # Get the value with the highest count81    max_value = news['sentiment'].value_counts().idxmax()82 83    counts = list(news['sentiment'].value_counts())84 85    news['compound'] = news['sentiment_scores'].apply(lambda x: x['compound'])86    news['compound_abs'] = news['compound'].abs()87    toppers = news.sort_values(by='compound_abs', ascending=False)88    title_list = list(news.Title.head(3))89    summary_list = list(news.Summary.head(3))90    positive_count = news[news['sentiment']=='positive'].shape[0]91    neutral_count = news[news['sentiment']=='neutral'].shape[0]92    negative_count = news[news['sentiment']=='negative'].shape[0]93 94    # Print the value with the highest count95    return max_value.title(), title_list[0], title_list[1], title_list[2], summary_list[0], summary_list[1], summary_list[2], str(positive_count), str(neutral_count), str(negative_count)96 97 98iface = gr.Interface(fn=getSentiment, inputs="text", outputs=["text","text", "text", "text","text", "text", "text", "text", "text", "text"], theme=gr.themes.Soft())99iface.launch()