Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,119 +1,75 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
-
import yfinance as yf
|
| 7 |
-
import plotly.graph_objects as go
|
| 8 |
-
from datetime import datetime, timedelta
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
def encode_special_characters(text):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
try:
|
| 27 |
-
response = requests.get(url, verify=False)
|
| 28 |
-
response.raise_for_status()
|
| 29 |
-
except requests.RequestException as e:
|
| 30 |
-
print(f"Error fetching news: {e}")
|
| 31 |
-
return pd.DataFrame()
|
| 32 |
-
|
| 33 |
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
|
| 34 |
articles = soup.find_all('article')
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
for article in articles[:num_articles]:
|
| 38 |
-
link = article.find('a')['href'].replace("./articles/", "https://news.google.com/articles/")
|
| 39 |
-
text_parts = article.get_text(separator='\n').split('\n')
|
| 40 |
-
|
| 41 |
-
news_data.append({
|
| 42 |
-
'Title': text_parts[2] if len(text_parts) > 2 else 'Missing',
|
| 43 |
-
'Source': text_parts[0] if len(text_parts) > 0 else 'Missing',
|
| 44 |
-
'Time': text_parts[3] if len(text_parts) > 3 else 'Missing',
|
| 45 |
-
'Author': text_parts[4].split('By ')[-1] if len(text_parts) > 4 else 'Missing',
|
| 46 |
-
'Link': link
|
| 47 |
-
})
|
| 48 |
-
|
| 49 |
-
return pd.DataFrame(news_data)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
result = sentiment_model(text)[0]
|
| 54 |
-
return result['label'], result['score']
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
news_df['Sentiment'], news_df['Sentiment_Score'] = zip(*news_df['Title'].apply(analyze_sentiment))
|
| 72 |
-
|
| 73 |
-
# Fetch stock data (last 30 days)
|
| 74 |
-
end_date = datetime.now()
|
| 75 |
-
start_date = end_date - timedelta(days=30)
|
| 76 |
-
stock_data = fetch_stock_data(stock_symbol, start_date, end_date)
|
| 77 |
-
|
| 78 |
-
# Create sentiment plot
|
| 79 |
-
sentiment_fig = go.Figure(data=[go.Bar(
|
| 80 |
-
x=news_df['Time'],
|
| 81 |
-
y=news_df['Sentiment_Score'],
|
| 82 |
-
marker_color=news_df['Sentiment'].map({'positive': 'green', 'neutral': 'gray', 'negative': 'red'})
|
| 83 |
-
)])
|
| 84 |
-
sentiment_fig.update_layout(title='News Sentiment Over Time', xaxis_title='Time', yaxis_title='Sentiment Score')
|
| 85 |
-
|
| 86 |
-
# Create stock price plot
|
| 87 |
-
stock_fig = go.Figure(data=[go.Candlestick(
|
| 88 |
-
x=stock_data.index,
|
| 89 |
-
open=stock_data['Open'],
|
| 90 |
-
high=stock_data['High'],
|
| 91 |
-
low=stock_data['Low'],
|
| 92 |
-
close=stock_data['Close']
|
| 93 |
-
)])
|
| 94 |
-
stock_fig.update_layout(title=f'{stock_symbol} Stock Price', xaxis_title='Date', yaxis_title='Price')
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
# Gradio interface
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
-
gr.Markdown("# Financial News Sentiment Analysis and Market Impact")
|
| 101 |
-
|
| 102 |
-
with gr.Row():
|
| 103 |
-
topic = gr.Textbox(label="Enter a financial topic or company name")
|
| 104 |
-
stock_symbol = gr.Textbox(label="Enter the stock symbol (e.g., RELIANCE.NS for Reliance Industries)")
|
| 105 |
-
|
| 106 |
-
analyze_btn = gr.Button(value="Analyze")
|
| 107 |
-
|
| 108 |
-
news_output = gr.DataFrame(label="News and Sentiment Analysis")
|
| 109 |
-
sentiment_plot = gr.Plot(label="Sentiment Analysis")
|
| 110 |
-
stock_plot = gr.Plot(label="Stock Price Movement")
|
| 111 |
-
|
| 112 |
-
analyze_btn.click(
|
| 113 |
-
news_and_analysis,
|
| 114 |
-
inputs=[topic, stock_symbol],
|
| 115 |
-
outputs=[news_output, sentiment_plot, stock_plot]
|
| 116 |
-
)
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import selenium
|
| 3 |
import requests
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
import pandas as pd
|
| 6 |
+
|
| 7 |
+
from selenium import webdriver
|
| 8 |
+
from selenium.webdriver.common.keys import Keys
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import time
|
| 11 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Search Query
|
| 14 |
+
def news_and_analysis(query):
|
| 15 |
|
| 16 |
+
# Encode special characters in a text string
|
| 17 |
+
def encode_special_characters(text):
|
| 18 |
+
encoded_text = ''
|
| 19 |
+
special_characters = {'&': '%26', '=': '%3D', '+': '%2B', ' ': '%20'} # Add more special characters as needed
|
| 20 |
+
for char in text.lower():
|
| 21 |
+
encoded_text += special_characters.get(char, char)
|
| 22 |
+
return encoded_text
|
| 23 |
|
| 24 |
+
query2 = encode_special_characters(query)
|
| 25 |
+
url = f"https://news.google.com/search?q={query2}&hl=en-US&gl=in&ceid=US%3Aen&num=3"
|
| 26 |
+
|
| 27 |
+
response = requests.get(url, verify = False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 29 |
+
|
| 30 |
articles = soup.find_all('article')
|
| 31 |
+
links = [article.find('a')['href'] for article in articles]
|
| 32 |
+
links = [link.replace("./articles/", "https://news.google.com/articles/") for link in links]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
news_text = [article.get_text(separator='\n') for article in articles]
|
| 35 |
+
news_text_split = [text.split('\n') for text in news_text]
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
news_df = pd.DataFrame({
|
| 38 |
+
'Title': [text[2] for text in news_text_split],
|
| 39 |
+
'Source': [text[0] for text in news_text_split],
|
| 40 |
+
'Time': [text[3] if len(text) > 3 else 'Missing' for text in news_text_split],
|
| 41 |
+
'Author': [text[4].split('By ')[-1] if len(text) > 4 else 'Missing' for text in news_text_split],
|
| 42 |
+
'Link': links
|
| 43 |
+
})
|
| 44 |
|
| 45 |
+
news_df = news_df.loc[0:5,:]
|
| 46 |
+
options = webdriver.ChromeOptions()
|
| 47 |
+
options.add_argument('--headless')
|
| 48 |
+
options.add_argument('--no-sandbox')
|
| 49 |
+
options.add_argument('--disable-dev-shm-usage')
|
| 50 |
+
options.use_chromium = True
|
| 51 |
+
driver = webdriver.Chrome(options = options)
|
| 52 |
+
|
| 53 |
+
classification= pipeline(model="finiteautomata/bertweet-base-sentiment-analysis")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
news_df['Sentiment'] = ''
|
| 56 |
+
for i in range(0, len(news_df)):
|
| 57 |
+
# driver.get(news_df.loc[i,'Link'])
|
| 58 |
+
# time.sleep(10)
|
| 59 |
+
# headline = driver.find_element('xpath', '//h1').text
|
| 60 |
+
#news_df.loc[i, 'Headline'] = headline
|
| 61 |
+
title = news_df.loc[i, 'Title']
|
| 62 |
+
news_df.loc[i, 'Sentiment'] = str(classification(title))
|
| 63 |
+
print(news_df)
|
| 64 |
+
|
| 65 |
+
return(news_df)
|
| 66 |
|
|
|
|
| 67 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
|
| 70 |
+
topic= gr.Textbox(label="Topic for which you want Google news and sentiment analysis")
|
| 71 |
+
|
| 72 |
+
btn = gr.Button(value="Submit")
|
| 73 |
+
btn.click(news_and_analysis, inputs=topic, outputs= gr.Dataframe())
|
| 74 |
|
| 75 |
demo.launch()
|