Spaces:
Sleeping
Sleeping
Update api.py
Browse files
api.py
CHANGED
|
@@ -1,206 +1,208 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, send_file
|
| 2 |
-
from bs4 import BeautifulSoup
|
| 3 |
-
from newspaper import Article
|
| 4 |
-
from textblob import TextBlob
|
| 5 |
-
# from newsapi import NewsApiClient
|
| 6 |
-
from transformers import pipeline
|
| 7 |
-
import requests
|
| 8 |
-
from utils import *
|
| 9 |
-
import pandas as pd
|
| 10 |
-
import base64
|
| 11 |
-
import json
|
| 12 |
-
# import nest_asyncio
|
| 13 |
-
|
| 14 |
-
app = Flask(__name__)
|
| 15 |
-
|
| 16 |
-
# newsapi = NewsApiClient(api_key='YOUR_NEWS_API_KEY') # Replace with your API key
|
| 17 |
-
|
| 18 |
-
# @app.route('/analyze_news', methods=['GET'])
|
| 19 |
-
# def analyze_news():
|
| 20 |
-
def analyze_news(company, source):
|
| 21 |
-
company = company
|
| 22 |
-
source = source
|
| 23 |
-
# company = request.args.get('company')
|
| 24 |
-
# source = request.args.get('source')
|
| 25 |
-
if not company or not source:
|
| 26 |
-
return jsonify({"error": "Please provide a company name as a query parameter"}), 400
|
| 27 |
-
|
| 28 |
-
all_articles = []
|
| 29 |
-
output = {"Company": f"{company}", "Articles": all_articles}
|
| 30 |
-
|
| 31 |
-
overall_sentiment_count = 0
|
| 32 |
-
sentiment_count = {"POSITIVE": 0, "NEGATIVE": 0, "NEUTRAL": 0}
|
| 33 |
-
|
| 34 |
-
if source == "NewsOrg":
|
| 35 |
-
# Fetch articles from News API
|
| 36 |
-
# response = newsapi.get_everything(q=company, page_size=5, sort_by='publishedAt', language='en')
|
| 37 |
-
|
| 38 |
-
params = {"q":"tesla","apiKey":"7396bdb0bc0a42c5b5b0c9c5945d32fa", "pagesize":10, "sortBy": "publishedAt", "language":'en'}
|
| 39 |
-
articles = requests.get(url = "https://newsapi.org/v2/everything", params= params)
|
| 40 |
-
articles = json.loads(articles.text)
|
| 41 |
-
|
| 42 |
-
# results = []
|
| 43 |
-
# sentiment_count = {"POSITIVE": 0, "NEGATIVE": 0, "NEUTRAL": 0}
|
| 44 |
-
|
| 45 |
-
# print(f">>>>>>>>>>>>>>>>>>>>>{articles}")
|
| 46 |
-
for idx, article in enumerate(articles["articles"]):
|
| 47 |
-
# print(f">>>>>>>>>>>>>>>>>>>>>{article}")
|
| 48 |
-
url = article.get("url")
|
| 49 |
-
news_article = Article(url)
|
| 50 |
-
try:
|
| 51 |
-
news_article.download()
|
| 52 |
-
news_article.parse()
|
| 53 |
-
except:
|
| 54 |
-
continue
|
| 55 |
-
|
| 56 |
-
blob = TextBlob(news_article.text)
|
| 57 |
-
polarity = blob.sentiment.polarity
|
| 58 |
-
|
| 59 |
-
if polarity > 0.3:
|
| 60 |
-
sentiment = "POSITIVE"
|
| 61 |
-
overall_sentiment_count += 1
|
| 62 |
-
elif polarity < -0.3:
|
| 63 |
-
sentiment = "NEGATIVE"
|
| 64 |
-
overall_sentiment_count -= 1
|
| 65 |
-
else:
|
| 66 |
-
sentiment = "NEUTRAL"
|
| 67 |
-
# neutral_sentiment_count += 1
|
| 68 |
-
|
| 69 |
-
sentiment_count[sentiment] += 1
|
| 70 |
-
|
| 71 |
-
all_articles.append({
|
| 72 |
-
"Title": article.get("title"),
|
| 73 |
-
"Summary": article.get("description"),
|
| 74 |
-
"Sentiment": sentiment
|
| 75 |
-
})
|
| 76 |
-
|
| 77 |
-
output["Comparitive Sentiment Score"] = {
|
| 78 |
-
"Sentiment Distribution": sentiment_count
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
if overall_sentiment_count>0:
|
| 82 |
-
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly positive. Potential stock growth expected."
|
| 83 |
-
elif overall_sentiment_count<0:
|
| 84 |
-
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly negative. Potential stock decline expected."
|
| 85 |
-
else:
|
| 86 |
-
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly neutral. Stocks going to stay stagnant for some time."
|
| 87 |
-
|
| 88 |
-
print(output)
|
| 89 |
-
print(f"{'>'*5} Starting text summarization.")
|
| 90 |
-
|
| 91 |
-
# return jsonify({
|
| 92 |
-
# "company": company,
|
| 93 |
-
# "sentiment_distribution": sentiment_count,
|
| 94 |
-
# "articles": results
|
| 95 |
-
# })
|
| 96 |
-
# df = pd.DataFrame(all_articles)
|
| 97 |
-
text_to_summarize = " ".join([d['Title'] + " " + d['Summary'] for d in all_articles[:5]])
|
| 98 |
-
summary_final = summarize_text(text_to_summarize)
|
| 99 |
-
|
| 100 |
-
audio_path = generate_hindi_tts(summary_final)
|
| 101 |
-
if audio_path:# and os.path.exists(audio_path):
|
| 102 |
-
# Convert audio file to base64
|
| 103 |
-
with open(audio_path, "rb") as f:
|
| 104 |
-
audio_base64 = base64.b64encode(f.read()).decode('utf-8')
|
| 105 |
-
|
| 106 |
-
output["Audio"] = audio_base64
|
| 107 |
-
|
| 108 |
-
return output
|
| 109 |
-
|
| 110 |
-
else:
|
| 111 |
-
return jsonify({"error": "Failed to generate audio"}), 500
|
| 112 |
-
|
| 113 |
-
elif source == "Yahoo News":
|
| 114 |
-
url = f"https://finance.yahoo.com/quote/{company}/news/"
|
| 115 |
-
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'}
|
| 116 |
-
response = requests.get(url, headers=headers)
|
| 117 |
-
|
| 118 |
-
if response.status_code != 200:
|
| 119 |
-
print("Failed to fetch news articles")
|
| 120 |
-
return {}
|
| 121 |
-
|
| 122 |
-
paragraphs = []
|
| 123 |
-
titles = []
|
| 124 |
-
summaries = []
|
| 125 |
-
soup = BeautifulSoup(response.content, 'html.parser')
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
for news in soup.find_all("div", class_="holder yf-1napat3"):
|
| 129 |
-
title_all = news.find_all('h3', class_="clamp yf-82qtw3")
|
| 130 |
-
summary_all = news.find_all('p', class_="clamp yf-82qtw3")
|
| 131 |
-
for title, summary in zip(title_all, summary_all):
|
| 132 |
-
title_text = title.get_text()
|
| 133 |
-
summary_text = summary.get_text()
|
| 134 |
-
paragraph = title_text + ' ' + summary_text
|
| 135 |
-
titles.append(title_text)
|
| 136 |
-
summaries.append(summary_text)
|
| 137 |
-
paragraphs.append(paragraph)
|
| 138 |
-
|
| 139 |
-
# Analyze sentiment and prepare the output
|
| 140 |
-
for i, paragraph in enumerate(paragraphs):
|
| 141 |
-
sentiment = analyze_sentiment(paragraph)
|
| 142 |
-
if sentiment == "POSITIVE":
|
| 143 |
-
# positive_sentiment_count += 1
|
| 144 |
-
overall_sentiment_count += 1
|
| 145 |
-
elif sentiment == "NEGATIVE":
|
| 146 |
-
# negative_sentiment_count += 1
|
| 147 |
-
overall_sentiment_count -= 1
|
| 148 |
-
# else:
|
| 149 |
-
# neutral_sentiment_count += 1
|
| 150 |
-
# top_words =
|
| 151 |
-
sentiment_count[sentiment] += 1
|
| 152 |
-
|
| 153 |
-
article = {
|
| 154 |
-
"Title": titles[i],
|
| 155 |
-
"Summary": summaries[i],
|
| 156 |
-
"Sentiment": sentiment
|
| 157 |
-
}
|
| 158 |
-
|
| 159 |
-
all_articles.append(article)
|
| 160 |
-
|
| 161 |
-
output["Comparitive Sentiment Score"]
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly
|
| 167 |
-
|
| 168 |
-
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
#
|
| 189 |
-
|
| 190 |
-
#
|
| 191 |
-
|
| 192 |
-
#
|
| 193 |
-
#
|
| 194 |
-
#
|
| 195 |
-
|
| 196 |
-
#
|
| 197 |
-
|
| 198 |
-
#
|
| 199 |
-
|
| 200 |
-
#
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
return jsonify({"error": "
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_file
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
from newspaper import Article
|
| 4 |
+
from textblob import TextBlob
|
| 5 |
+
# from newsapi import NewsApiClient
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import requests
|
| 8 |
+
from utils import *
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import base64
|
| 11 |
+
import json
|
| 12 |
+
# import nest_asyncio
|
| 13 |
+
|
| 14 |
+
app = Flask(__name__)
|
| 15 |
+
|
| 16 |
+
# newsapi = NewsApiClient(api_key='YOUR_NEWS_API_KEY') # Replace with your API key
|
| 17 |
+
|
| 18 |
+
# @app.route('/analyze_news', methods=['GET'])
|
| 19 |
+
# def analyze_news():
|
| 20 |
+
def analyze_news(company, source):
|
| 21 |
+
company = company
|
| 22 |
+
source = source
|
| 23 |
+
# company = request.args.get('company')
|
| 24 |
+
# source = request.args.get('source')
|
| 25 |
+
if not company or not source:
|
| 26 |
+
return jsonify({"error": "Please provide a company name as a query parameter"}), 400
|
| 27 |
+
|
| 28 |
+
all_articles = []
|
| 29 |
+
output = {"Company": f"{company}", "Articles": all_articles}
|
| 30 |
+
|
| 31 |
+
overall_sentiment_count = 0
|
| 32 |
+
sentiment_count = {"POSITIVE": 0, "NEGATIVE": 0, "NEUTRAL": 0}
|
| 33 |
+
|
| 34 |
+
if source == "NewsOrg":
|
| 35 |
+
# Fetch articles from News API
|
| 36 |
+
# response = newsapi.get_everything(q=company, page_size=5, sort_by='publishedAt', language='en')
|
| 37 |
+
|
| 38 |
+
params = {"q":"tesla","apiKey":"7396bdb0bc0a42c5b5b0c9c5945d32fa", "pagesize":10, "sortBy": "publishedAt", "language":'en'}
|
| 39 |
+
articles = requests.get(url = "https://newsapi.org/v2/everything", params= params)
|
| 40 |
+
articles = json.loads(articles.text)
|
| 41 |
+
|
| 42 |
+
# results = []
|
| 43 |
+
# sentiment_count = {"POSITIVE": 0, "NEGATIVE": 0, "NEUTRAL": 0}
|
| 44 |
+
|
| 45 |
+
# print(f">>>>>>>>>>>>>>>>>>>>>{articles}")
|
| 46 |
+
for idx, article in enumerate(articles["articles"]):
|
| 47 |
+
# print(f">>>>>>>>>>>>>>>>>>>>>{article}")
|
| 48 |
+
url = article.get("url")
|
| 49 |
+
news_article = Article(url)
|
| 50 |
+
try:
|
| 51 |
+
news_article.download()
|
| 52 |
+
news_article.parse()
|
| 53 |
+
except:
|
| 54 |
+
continue
|
| 55 |
+
|
| 56 |
+
blob = TextBlob(news_article.text)
|
| 57 |
+
polarity = blob.sentiment.polarity
|
| 58 |
+
|
| 59 |
+
if polarity > 0.3:
|
| 60 |
+
sentiment = "POSITIVE"
|
| 61 |
+
overall_sentiment_count += 1
|
| 62 |
+
elif polarity < -0.3:
|
| 63 |
+
sentiment = "NEGATIVE"
|
| 64 |
+
overall_sentiment_count -= 1
|
| 65 |
+
else:
|
| 66 |
+
sentiment = "NEUTRAL"
|
| 67 |
+
# neutral_sentiment_count += 1
|
| 68 |
+
|
| 69 |
+
sentiment_count[sentiment] += 1
|
| 70 |
+
|
| 71 |
+
all_articles.append({
|
| 72 |
+
"Title": article.get("title"),
|
| 73 |
+
"Summary": article.get("description"),
|
| 74 |
+
"Sentiment": sentiment
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
+
output["Comparitive Sentiment Score"] = {
|
| 78 |
+
"Sentiment Distribution": sentiment_count
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
if overall_sentiment_count>0:
|
| 82 |
+
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly positive. Potential stock growth expected."
|
| 83 |
+
elif overall_sentiment_count<0:
|
| 84 |
+
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly negative. Potential stock decline expected."
|
| 85 |
+
else:
|
| 86 |
+
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly neutral. Stocks going to stay stagnant for some time."
|
| 87 |
+
|
| 88 |
+
print(output)
|
| 89 |
+
print(f"{'>'*5} Starting text summarization.")
|
| 90 |
+
|
| 91 |
+
# return jsonify({
|
| 92 |
+
# "company": company,
|
| 93 |
+
# "sentiment_distribution": sentiment_count,
|
| 94 |
+
# "articles": results
|
| 95 |
+
# })
|
| 96 |
+
# df = pd.DataFrame(all_articles)
|
| 97 |
+
text_to_summarize = " ".join([d['Title'] + " " + d['Summary'] for d in all_articles[:5]])
|
| 98 |
+
summary_final = summarize_text(text_to_summarize)
|
| 99 |
+
|
| 100 |
+
audio_path = generate_hindi_tts(summary_final)
|
| 101 |
+
if audio_path:# and os.path.exists(audio_path):
|
| 102 |
+
# Convert audio file to base64
|
| 103 |
+
with open(audio_path, "rb") as f:
|
| 104 |
+
audio_base64 = base64.b64encode(f.read()).decode('utf-8')
|
| 105 |
+
|
| 106 |
+
output["Audio"] = audio_base64
|
| 107 |
+
|
| 108 |
+
return output
|
| 109 |
+
|
| 110 |
+
else:
|
| 111 |
+
return jsonify({"error": "Failed to generate audio"}), 500
|
| 112 |
+
|
| 113 |
+
elif source == "Yahoo News":
|
| 114 |
+
url = f"https://finance.yahoo.com/quote/{company}/news/"
|
| 115 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'}
|
| 116 |
+
response = requests.get(url, headers=headers)
|
| 117 |
+
|
| 118 |
+
if response.status_code != 200:
|
| 119 |
+
print("Failed to fetch news articles")
|
| 120 |
+
return {}
|
| 121 |
+
|
| 122 |
+
paragraphs = []
|
| 123 |
+
titles = []
|
| 124 |
+
summaries = []
|
| 125 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
for news in soup.find_all("div", class_="holder yf-1napat3"):
|
| 129 |
+
title_all = news.find_all('h3', class_="clamp yf-82qtw3")
|
| 130 |
+
summary_all = news.find_all('p', class_="clamp yf-82qtw3")
|
| 131 |
+
for title, summary in zip(title_all, summary_all):
|
| 132 |
+
title_text = title.get_text()
|
| 133 |
+
summary_text = summary.get_text()
|
| 134 |
+
paragraph = title_text + ' ' + summary_text
|
| 135 |
+
titles.append(title_text)
|
| 136 |
+
summaries.append(summary_text)
|
| 137 |
+
paragraphs.append(paragraph)
|
| 138 |
+
|
| 139 |
+
# Analyze sentiment and prepare the output
|
| 140 |
+
for i, paragraph in enumerate(paragraphs):
|
| 141 |
+
sentiment = analyze_sentiment(paragraph)
|
| 142 |
+
if sentiment == "POSITIVE":
|
| 143 |
+
# positive_sentiment_count += 1
|
| 144 |
+
overall_sentiment_count += 1
|
| 145 |
+
elif sentiment == "NEGATIVE":
|
| 146 |
+
# negative_sentiment_count += 1
|
| 147 |
+
overall_sentiment_count -= 1
|
| 148 |
+
# else:
|
| 149 |
+
# neutral_sentiment_count += 1
|
| 150 |
+
# top_words =
|
| 151 |
+
sentiment_count[sentiment] += 1
|
| 152 |
+
|
| 153 |
+
article = {
|
| 154 |
+
"Title": titles[i],
|
| 155 |
+
"Summary": summaries[i],
|
| 156 |
+
"Sentiment": sentiment
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
all_articles.append(article)
|
| 160 |
+
|
| 161 |
+
output["Comparitive Sentiment Score"] = {
|
| 162 |
+
"Sentiment Distribution": sentiment_count
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
if overall_sentiment_count>0:
|
| 166 |
+
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly positive. Potential stock growth expected."
|
| 167 |
+
elif overall_sentiment_count<0:
|
| 168 |
+
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly negative. Potential stock decline expected."
|
| 169 |
+
else:
|
| 170 |
+
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly neutral. Stocks going to stay stagnant for some time."
|
| 171 |
+
|
| 172 |
+
df = pd.DataFrame(all_articles)
|
| 173 |
+
text_to_summarize = " ".join([d['Title'] + " " + d['summary'] for d in article[:5]])
|
| 174 |
+
summary_final = summarize_text(text_to_summarize)
|
| 175 |
+
|
| 176 |
+
audio_path = generate_hindi_tts(summary_final)
|
| 177 |
+
if audio_path:# and os.path.exists(audio_path):
|
| 178 |
+
# Convert audio file to base64
|
| 179 |
+
with open(audio_path, "rb") as f:
|
| 180 |
+
audio_base64 = base64.b64encode(f.read()).decode('utf-8')
|
| 181 |
+
|
| 182 |
+
output["Audio"] = audio_base64
|
| 183 |
+
|
| 184 |
+
return output
|
| 185 |
+
|
| 186 |
+
else:
|
| 187 |
+
return jsonify({"error": "Failed to generate audio"}), 500
|
| 188 |
+
# df = pd.DataFrame(all_articles)
|
| 189 |
+
# text_to_summarize = " ".join([d['Title'] + " " + d['summary'] for d in article[:5]])
|
| 190 |
+
# summary_final = summarize_text(text_to_summarize)
|
| 191 |
+
|
| 192 |
+
# audio_path = generate_hindi_tts(summary_final)
|
| 193 |
+
# if audio_path:# and os.path.exists(audio_path):
|
| 194 |
+
# # Convert audio file to base64
|
| 195 |
+
# with open(audio_path, "rb") as f:
|
| 196 |
+
# audio_base64 = base64.b64encode(f.read()).decode('utf-8')
|
| 197 |
+
|
| 198 |
+
# output["Audio"] = audio_base64
|
| 199 |
+
|
| 200 |
+
# return output
|
| 201 |
+
|
| 202 |
+
# else:
|
| 203 |
+
# return jsonify({"error": "Failed to generate audio"}), 500
|
| 204 |
+
else:
|
| 205 |
+
return jsonify({"error": "Invalid source provided"}), 400
|
| 206 |
+
|
| 207 |
+
if __name__ == '__main__':
|
| 208 |
+
app.run(debug=True, port=8000)
|