Divyansh Kushwaha commited on
Commit
aea053c
·
1 Parent(s): 0021684
Files changed (2) hide show
  1. app.py +75 -37
  2. utils.py +1 -1
app.py CHANGED
@@ -2,45 +2,79 @@ 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:
@@ -49,31 +83,35 @@ if st.button("Generate Summary"):
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:
 
2
  import requests
3
 
4
  BASE_URL = "https://jatin7237-news-app.hf.space" # Replace with your API's public URL
 
 
5
  st.title("Company Sentiment Analysis")
 
6
 
7
+ company_name = st.text_input(
8
+ "Enter the company name:",
9
+ placeholder="Example: Microsoft, Apple, Tesla"
10
+ )
11
+
12
+ def display_articles(articles):
13
+ for i, article in enumerate(articles, start=1):
14
+ st.markdown(f"#### **Article {i}**")
15
+ st.write(f"- **Title:** {article['Title']}")
16
+ st.write(f"- **Summary:** {article['Summary']}")
17
+ st.write(f"- **Sentiment:** {article['Sentiment']}")
18
+ st.write(f"- **Score:** {article['Score']:.2f}")
19
+ st.write(f"- **Topics:** {', '.join(article['Topics'])}")
20
+ st.markdown("---")
21
+
22
+ def display_sentiment_distribution(sentiment_distribution):
23
+ st.subheader("Sentiment Distribution")
24
+ sentiment_data = {
25
+ "Sentiment": list(sentiment_distribution.keys()),
26
+ "Count": list(sentiment_distribution.values())
27
+ }
28
+ st.table(sentiment_data)
29
+
30
+ def display_coverage_differences(coverage_differences):
31
+ st.subheader("Coverage Differences")
32
+ for diff in coverage_differences:
33
+ st.markdown(f"- **Comparison:** {diff['Comparison']}")
34
+ st.markdown(f" - **Impact:** {diff['Impact']}")
35
+ st.markdown("---")
36
+
37
+ def display_topic_overlap(topic_overlap):
38
+ st.subheader("Topic Overlap")
39
+ st.write(f"- **Common Topics:** {', '.join(topic_overlap['Common Topics'])}")
40
+ st.write("- **Unique Topics by Article:**")
41
+ for article, topics in topic_overlap["Unique Topics"].items():
42
+ st.write(f" - **{article}:** {', '.join(topics)}")
43
+ st.markdown("---")
44
+
45
  if st.button("Generate Summary"):
46
  if company_name:
47
  try:
48
+ summary_url = f"{BASE_URL}/generateSummary?company_name={company_name}"
49
+ response = requests.post(summary_url)
 
50
 
51
  if response.status_code == 200:
52
  data = response.json()
53
 
54
+ # Company Name
55
+ st.markdown(f"### **Company: {data.get('Company', 'Unknown')}**")
56
+
57
+ # Articles
58
+ st.markdown("### **Articles:**")
59
+ display_articles(data.get("Articles", []))
60
+
61
+ # Comparative Sentiment Score
62
+ st.markdown("### **Comparative Sentiment Score**")
63
+ sentiment_distribution = data.get("Comparative Sentiment Score", {}).get("Sentiment Distribution", {})
64
+ display_sentiment_distribution(sentiment_distribution)
65
+
66
+ coverage_differences = data.get("Comparative Sentiment Score", {}).get("Coverage Differences", [])
67
+ display_coverage_differences(coverage_differences)
68
+
69
+ topic_overlap = data.get("Comparative Sentiment Score", {}).get("Topic Overlap", {})
70
+ display_topic_overlap(topic_overlap)
71
+
72
+ # Final Sentiment Analysis
73
+ st.markdown("### **Final Sentiment Analysis**")
74
  st.write(data.get("Final Sentiment Analysis", "No sentiment analysis available."))
75
 
76
+ # Hindi Summary
77
+ st.markdown("### **Hindi Summary**")
78
  st.write(data.get("Hindi Summary", "No Hindi summary available."))
79
 
80
  else:
 
83
  except Exception as e:
84
  st.error(f"An error occurred: {e}")
85
  else:
86
+ st.warning("⚠️ Please enter a company name.")
87
 
 
88
  if st.button("Download JSON File"):
89
  json_url = f"{BASE_URL}/downloadJson"
90
  try:
91
  response = requests.get(json_url)
92
  if response.status_code == 200:
93
+ st.download_button(
94
+ label="Download JSON File",
95
+ data=response.content,
96
+ file_name="final_summary.json",
97
+ mime="application/json",
98
+ )
99
  else:
100
  st.error(f"Error: {response.status_code}, {response.text}")
101
  except Exception as e:
102
  st.error(f"An error occurred: {e}")
103
 
 
104
  if st.button("Download Hindi Audio"):
105
  audio_url = f"{BASE_URL}/downloadHindiAudio"
106
  try:
107
  response = requests.get(audio_url)
108
  if response.status_code == 200:
109
+ st.download_button(
110
+ label="Download Hindi Audio",
111
+ data=response.content,
112
+ file_name="hindi_summary.mp3",
113
+ mime="audio/mp3",
114
+ )
115
  else:
116
  st.error(f"Error: {response.status_code}, {response.text}")
117
  except Exception as e:
utils.py CHANGED
@@ -157,7 +157,7 @@ def compare_articles(news_data, sentiment_counts):
157
  """
158
  response = llm.invoke([HumanMessage(content=comparison_prompt)]).content
159
  coverage_differences = extract_json(response).get("Coverage Differences", [])
160
- final_sentiment = generate_final_sentiment(news_data, sentiment_counts,llm)
161
  return {
162
  "Company": news_data["Company"],
163
  "Articles": articles,
 
157
  """
158
  response = llm.invoke([HumanMessage(content=comparison_prompt)]).content
159
  coverage_differences = extract_json(response).get("Coverage Differences", [])
160
+ final_sentiment = generate_final_sentiment(news_data, sentiment_counts)
161
  return {
162
  "Company": news_data["Company"],
163
  "Articles": articles,