News-App / app.py
Divyansh Kushwaha
UPdated
220d7b5
raw
history blame
4.93 kB
import streamlit as st
import requests
BASE_URL = "http://localhost:8000"
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}: {article['Title']}**")
st.write(f"- **Summary:** {article['Summary']}")
st.write(f"- **Sentiment:** {article['Sentiment']} | **Score:** {article['Score']:.2f}")
st.write(f"- **Topics:** {', '.join(article['Topics'])}")
def display_sentiment_distribution(sentiment_distribution):
st.markdown("#### **Sentiment Distribution:**")
sentiment_data = {
"Sentiment": list(sentiment_distribution.keys()),
"Count": list(sentiment_distribution.values())
}
st.table(sentiment_data)
def display_coverage_differences(coverage_differences):
if coverage_differences:
st.markdown("#### **Coverage Differences:**")
for diff in coverage_differences:
st.write(f"- **{diff['Comparison']}:** {diff['Impact']}")
def display_topic_overlap(topic_overlap):
st.markdown("#### **Topic Overlap:**")
st.write(f"- **Common Topics:** {', '.join(topic_overlap['Common Topics'])}")
st.markdown("- **Unique Topics by Article:**")
for article, topics in topic_overlap["Unique Topics"].items():
st.write(f" - **{article}:** {', '.join(topics)}")
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()
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
if st.button("Play Hindi Audio"):
audio_url = f"{BASE_URL}/downloadHindiAudio"
try:
# Fetch audio data using the API
response = requests.get(audio_url, stream=True) # Stream the audio data
if response.status_code == 200:
st.audio(response.content, format="audio/mp3") # Pass the audio content directly as bytes
else:
st.error(f"Error: {response.status_code}, {response.text}")
except Exception as e:
st.error(f"An error occurred while fetching the audio: {e}")
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}")