Divyansh Kushwaha
commited on
Commit
·
53dd0ab
1
Parent(s):
fd6a623
updated
Browse files
main.py
CHANGED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
|
4 |
+
BASE_URL = "https://jatin7237-news-app.hf.space" # Replace with your API's public URL
|
5 |
+
|
6 |
+
# App title and description
|
7 |
+
st.title("Company Sentiment Analysis")
|
8 |
+
st.write("Analyze company news articles for sentiment, topics, and summaries using the FastAPI app.")
|
9 |
+
|
10 |
+
# Input company name
|
11 |
+
company_name = st.text_input("Enter the company name:", placeholder="Example: Microsoft, Apple, Tesla")
|
12 |
+
# Generate summary button
|
13 |
+
if st.button("Generate Summary"):
|
14 |
+
if company_name:
|
15 |
+
try:
|
16 |
+
# Call the /generateSummary endpoint
|
17 |
+
summary_url = f"{BASE_URL}/generateSummary"
|
18 |
+
response = requests.post(summary_url, json={"company_name": company_name})
|
19 |
+
|
20 |
+
if response.status_code == 200:
|
21 |
+
data = response.json()
|
22 |
+
|
23 |
+
# Display the output
|
24 |
+
st.subheader("Company:")
|
25 |
+
st.write(data.get("Company", "Unknown"))
|
26 |
+
|
27 |
+
st.subheader("Articles:")
|
28 |
+
articles = data.get("Articles", [])
|
29 |
+
for i, article in enumerate(articles, start=1):
|
30 |
+
st.write(f"**Article {i}:**")
|
31 |
+
st.write(f"Title: {article['Title']}")
|
32 |
+
st.write(f"Summary: {article['Summary']}")
|
33 |
+
st.write(f"Sentiment: {article['Sentiment']}")
|
34 |
+
st.write(f"Score: {article['Score']}")
|
35 |
+
st.write(f"Topics: {', '.join(article['Topics'])}")
|
36 |
+
|
37 |
+
st.subheader("Comparative Sentiment Score:")
|
38 |
+
st.write(data.get("Comparative Sentiment Score", {}))
|
39 |
+
|
40 |
+
st.subheader("Final Sentiment Analysis:")
|
41 |
+
st.write(data.get("Final Sentiment Analysis", "No sentiment analysis available."))
|
42 |
+
|
43 |
+
st.subheader("Hindi Summary:")
|
44 |
+
st.write(data.get("Hindi Summary", "No Hindi summary available."))
|
45 |
+
|
46 |
+
else:
|
47 |
+
st.error(f"Error: {response.status_code}, {response.text}")
|
48 |
+
|
49 |
+
except Exception as e:
|
50 |
+
st.error(f"An error occurred: {e}")
|
51 |
+
else:
|
52 |
+
st.warning("Please enter a company name.")
|
53 |
+
|
54 |
+
# Download JSON file
|
55 |
+
if st.button("Download JSON File"):
|
56 |
+
json_url = f"{BASE_URL}/downloadJson"
|
57 |
+
try:
|
58 |
+
response = requests.get(json_url)
|
59 |
+
if response.status_code == 200:
|
60 |
+
with open("final_summary.json", "wb") as f:
|
61 |
+
f.write(response.content)
|
62 |
+
st.success("JSON file downloaded successfully!")
|
63 |
+
else:
|
64 |
+
st.error(f"Error: {response.status_code}, {response.text}")
|
65 |
+
except Exception as e:
|
66 |
+
st.error(f"An error occurred: {e}")
|
67 |
+
|
68 |
+
# Download Hindi audio file
|
69 |
+
if st.button("Download Hindi Audio"):
|
70 |
+
audio_url = f"{BASE_URL}/downloadHindiAudio"
|
71 |
+
try:
|
72 |
+
response = requests.get(audio_url)
|
73 |
+
if response.status_code == 200:
|
74 |
+
with open("hindi_summary.mp3", "wb") as f:
|
75 |
+
f.write(response.content)
|
76 |
+
st.success("Hindi audio file downloaded successfully!")
|
77 |
+
else:
|
78 |
+
st.error(f"Error: {response.status_code}, {response.text}")
|
79 |
+
except Exception as e:
|
80 |
+
st.error(f"An error occurred: {e}")
|
utils.py
CHANGED
@@ -76,25 +76,25 @@ def perform_sentiment_analysis(news_data):
|
|
76 |
return news_data, sentiment_counts
|
77 |
|
78 |
# def extract_topics_with_hf(news_data):
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
|
99 |
def extract_topics_with_hf(news_data):
|
100 |
structured_data = {
|
|
|
76 |
return news_data, sentiment_counts
|
77 |
|
78 |
# def extract_topics_with_hf(news_data):
|
79 |
+
# structured_data = {
|
80 |
+
# "Company": news_data.get("Company", "Unknown"),
|
81 |
+
# "Articles": []
|
82 |
+
# }
|
83 |
+
# topic_pipe = pipeline("text-classification", model="valurank/distilroberta-topic-classification",device=1)
|
84 |
+
# articles = news_data.get("Articles", [])
|
85 |
+
# for article in articles:
|
86 |
+
# content = f"{article['Title']} {article['Summary']}"
|
87 |
+
# topics_result = topic_pipe(content, top_k=3)
|
88 |
+
# topics = [topic["label"] for topic in topics_result] if topics_result else ["Unknown"]
|
89 |
+
|
90 |
+
# structured_data["Articles"].append({
|
91 |
+
# "Title": article["Title"],
|
92 |
+
# "Summary": article["Summary"],
|
93 |
+
# "Sentiment": article.get("Sentiment", "Unknown"),
|
94 |
+
# "Score": article.get("Score", 0.0),
|
95 |
+
# "Topics": topics
|
96 |
+
# })
|
97 |
+
# return structured_data
|
98 |
|
99 |
def extract_topics_with_hf(news_data):
|
100 |
structured_data = {
|