News-App / app.py
Divyansh Kushwaha
m
aea053c
raw
history blame
4.56 kB
import streamlit as st
import requests
BASE_URL = "https://jatin7237-news-app.hf.space" # Replace with your API's public URL
st.title("Company Sentiment Analysis")
company_name = st.text_input(
"Enter the company name:",
placeholder="Example: Microsoft, Apple, Tesla"
)
def display_articles(articles):
for i, article in enumerate(articles, start=1):
st.markdown(f"#### **Article {i}**")
st.write(f"- **Title:** {article['Title']}")
st.write(f"- **Summary:** {article['Summary']}")
st.write(f"- **Sentiment:** {article['Sentiment']}")
st.write(f"- **Score:** {article['Score']:.2f}")
st.write(f"- **Topics:** {', '.join(article['Topics'])}")
st.markdown("---")
def display_sentiment_distribution(sentiment_distribution):
st.subheader("Sentiment Distribution")
sentiment_data = {
"Sentiment": list(sentiment_distribution.keys()),
"Count": list(sentiment_distribution.values())
}
st.table(sentiment_data)
def display_coverage_differences(coverage_differences):
st.subheader("Coverage Differences")
for diff in coverage_differences:
st.markdown(f"- **Comparison:** {diff['Comparison']}")
st.markdown(f" - **Impact:** {diff['Impact']}")
st.markdown("---")
def display_topic_overlap(topic_overlap):
st.subheader("Topic Overlap")
st.write(f"- **Common Topics:** {', '.join(topic_overlap['Common Topics'])}")
st.write("- **Unique Topics by Article:**")
for article, topics in topic_overlap["Unique Topics"].items():
st.write(f" - **{article}:** {', '.join(topics)}")
st.markdown("---")
if st.button("Generate Summary"):
if company_name:
try:
summary_url = f"{BASE_URL}/generateSummary?company_name={company_name}"
response = requests.post(summary_url)
if response.status_code == 200:
data = response.json()
# Company Name
st.markdown(f"### **Company: {data.get('Company', 'Unknown')}**")
# Articles
st.markdown("### **Articles:**")
display_articles(data.get("Articles", []))
# Comparative Sentiment Score
st.markdown("### **Comparative Sentiment Score**")
sentiment_distribution = data.get("Comparative Sentiment Score", {}).get("Sentiment Distribution", {})
display_sentiment_distribution(sentiment_distribution)
coverage_differences = data.get("Comparative Sentiment Score", {}).get("Coverage Differences", [])
display_coverage_differences(coverage_differences)
topic_overlap = data.get("Comparative Sentiment Score", {}).get("Topic Overlap", {})
display_topic_overlap(topic_overlap)
# Final Sentiment Analysis
st.markdown("### **Final Sentiment Analysis**")
st.write(data.get("Final Sentiment Analysis", "No sentiment analysis available."))
# Hindi Summary
st.markdown("### **Hindi Summary**")
st.write(data.get("Hindi Summary", "No Hindi summary available."))
else:
st.error(f"Error: {response.status_code}, {response.text}")
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.warning("⚠️ Please enter a company name.")
if st.button("Download JSON File"):
json_url = f"{BASE_URL}/downloadJson"
try:
response = requests.get(json_url)
if response.status_code == 200:
st.download_button(
label="Download JSON File",
data=response.content,
file_name="final_summary.json",
mime="application/json",
)
else:
st.error(f"Error: {response.status_code}, {response.text}")
except Exception as e:
st.error(f"An error occurred: {e}")
if st.button("Download Hindi Audio"):
audio_url = f"{BASE_URL}/downloadHindiAudio"
try:
response = requests.get(audio_url)
if response.status_code == 200:
st.download_button(
label="Download Hindi Audio",
data=response.content,
file_name="hindi_summary.mp3",
mime="audio/mp3",
)
else:
st.error(f"Error: {response.status_code}, {response.text}")
except Exception as e:
st.error(f"An error occurred: {e}")