Spaces:
Configuration error
Configuration error
File size: 5,577 Bytes
192f98f |
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
from flask import Flask, request, jsonify, send_file
from bs4 import BeautifulSoup
from newspaper import Article
from textblob import TextBlob
from newsapi import NewsApiClient
from transformers import pipeline
import requests
from utils import *
import pandas as pd
import base64
app = Flask(__name__)
newsapi = NewsApiClient(api_key='YOUR_NEWS_API_KEY') # Replace with your API key
@app.route('/analyze_news', methods=['GET'])
def analyze_news():
company = request.args.get('company')
source = request.args.get('company')
if not company or not source:
return jsonify({"error": "Please provide a company name as a query parameter"}), 400
all_articles = []
output = {"Company": f"{company}", "Articles": all_articles}
if source == "NewsOrg":
# Fetch articles from News API
response = newsapi.get_everything(q=company, page_size=5, sort_by='publishedAt', language='en')
results = []
sentiment_count = {"Positive": 0, "Negative": 0, "Neutral": 0}
for idx, article in enumerate(response['articles']):
url = article.get("url")
news_article = Article(url)
try:
news_article.download()
news_article.parse()
except:
continue
blob = TextBlob(news_article.text)
polarity = blob.sentiment.polarity
if polarity > 0.3:
sentiment = "Positive"
elif polarity < -0.3:
sentiment = "Negative"
else:
sentiment = "Neutral"
sentiment_count[sentiment] += 1
results.append({
"title": article.get("title"),
"author": article.get("author"),
"summary": article.get("description"),
"sentiment": sentiment,
"url": article.get("url")
})
return jsonify({
"company": company,
"sentiment_distribution": sentiment_count,
"articles": results
})
elif source == "Yahoo News":
url = f"https://finance.yahoo.com/quote/{company}/news/"
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'}
response = requests.get(url, headers=headers)
if response.status_code != 200:
print("Failed to fetch news articles")
return {}
paragraphs = []
titles = []
summaries = []
soup = BeautifulSoup(response.content, 'html.parser')
overall_sentiment_count = 0
positive_sentiment_count = 0
negative_sentiment_count = 0
neutral_sentiment_count = 0
for news in soup.find_all("div", class_="holder yf-1napat3"):
title_all = news.find_all('h3', class_="clamp yf-82qtw3")
summary_all = news.find_all('p', class_="clamp yf-82qtw3")
for title, summary in zip(title_all, summary_all):
title_text = title.get_text()
summary_text = summary.get_text()
paragraph = title_text + ' ' + summary_text
titles.append(title_text)
summaries.append(summary_text)
paragraphs.append(paragraph)
# Analyze sentiment and prepare the output
for i, paragraph in enumerate(paragraphs):
sentiment = analyze_sentiment(paragraph)
if sentiment == "POSITIVE":
positive_sentiment_count += 1
overall_sentiment_count += 1
elif sentiment == "NEGATIVE":
negative_sentiment_count += 1
overall_sentiment_count -= 1
else:
neutral_sentiment_count += 1
# top_words =
article = {
"Title": titles[i],
"Summary": summaries[i],
"Sentiment": sentiment
}
all_articles.append(article)
output["Comparitive Sentiment Score"]["Sentiment Distribution"] = {
"Positive": positive_sentiment_count,
"Negative": negative_sentiment_count,
"Neutral": neutral_sentiment_count,
}
if overall_sentiment_count>0:
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly positive. Potential stock growth expected."
elif overall_sentiment_count<0:
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly negative. Potential stock decline expected."
else:
output["Final Sentiment Analysis"] = f"{company.capitalize()}'s lastest news is mostly neutral. Stocks going to stay stagnant for some time."
df = pd.DataFrame(article)
text_to_summarize = " ".join([d['Title'] + " " + d['summary'] for d in article[:5]])
summary_final = summarize_text(text_to_summarize)
audio_path = generate_hindi_tts(summary_final)
if audio_path:# and os.path.exists(audio_path):
# Convert audio file to base64
with open(audio_path, "rb") as f:
audio_base64 = base64.b64encode(f.read()).decode('utf-8')
output["Audio"] = audio_base64
return output
else:
return jsonify({"error": "Failed to generate audio"}), 500
if __name__ == '__main__':
app.run(debug=True, port=8000)
|