Spaces:
Sleeping
Sleeping
Sasmita Harini commited on
Commit ·
440ee04
1
Parent(s): 634db4b
Run FastAPI as subprocess in app.py
Browse files
app.py
CHANGED
|
@@ -1,115 +1,87 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import requests
|
| 3 |
-
import json
|
| 4 |
-
import base64
|
| 5 |
-
import io
|
| 6 |
-
from deep_translator import GoogleTranslator
|
| 7 |
-
from gtts import gTTS
|
| 8 |
import utils
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Initialize translator
|
| 11 |
-
translator = GoogleTranslator(source='en', target='hi')
|
| 12 |
-
|
| 13 |
-
# Streamlit app
|
| 14 |
st.title("News Summarization and Text-to-Speech Application")
|
| 15 |
|
| 16 |
-
# User input for company name
|
| 17 |
company_name = st.text_input("Enter the company name:", "").strip().lower()
|
|
|
|
| 18 |
if st.button("Fetch News"):
|
| 19 |
if company_name:
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Display news data
|
| 34 |
-
st.subheader(f"News Analysis for {news_data['Company']}")
|
| 35 |
-
|
| 36 |
-
# Articles section
|
| 37 |
-
st.subheader("Articles")
|
| 38 |
-
with st.expander("View Articles", expanded=False):
|
| 39 |
-
for i, article in enumerate(news_data['Articles']):
|
| 40 |
-
st.markdown(f"#### Article {i+1}: {article['Title']}")
|
| 41 |
-
st.markdown(f"**Summary:** {article['Summary']}")
|
| 42 |
-
st.markdown(f"**Sentiment:** {article['Sentiment']}")
|
| 43 |
-
st.markdown(f"**Topics:** {', '.join(article['Topics'])}")
|
| 44 |
-
st.divider()
|
| 45 |
-
|
| 46 |
-
# Sentiment Distribution
|
| 47 |
-
st.subheader("Sentiment Distribution")
|
| 48 |
-
sentiment_data = news_data['Comparative Sentiment Score']['Sentiment Distribution']
|
| 49 |
-
col1, col2, col3 = st.columns(3)
|
| 50 |
-
col1.metric("Positive", sentiment_data['Positive'])
|
| 51 |
-
col2.metric("Neutral", sentiment_data['Neutral'])
|
| 52 |
-
col3.metric("Negative", sentiment_data['Negative'])
|
| 53 |
-
|
| 54 |
-
# Topic Analysis
|
| 55 |
-
st.subheader("Topic Analysis")
|
| 56 |
-
with st.expander("View Topic Analysis", expanded=False):
|
| 57 |
-
st.markdown("**Common Topics:**")
|
| 58 |
-
st.write(", ".join(news_data['Topic Overlap']['Common Topics']))
|
| 59 |
-
for key, value in news_data['Topic Overlap'].items():
|
| 60 |
-
if key != "Common Topics":
|
| 61 |
-
st.markdown(f"**{key}:**")
|
| 62 |
-
st.write(", ".join(value))
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
coverage_diff = news_data['Coverage Differences']
|
| 68 |
-
if isinstance(coverage_diff, str):
|
| 69 |
-
st.write(coverage_diff) # Fallback for error cases
|
| 70 |
-
else:
|
| 71 |
-
formatted_text = '"Coverage Differences": [\n'
|
| 72 |
-
for i, item in enumerate(coverage_diff.get("Coverage Differences", [])):
|
| 73 |
-
formatted_text += "{\n"
|
| 74 |
-
formatted_text += f' "Comparison": "{item["Comparison"]}",\n'
|
| 75 |
-
formatted_text += f' "Impact": "{item["Impact"]}"\n'
|
| 76 |
-
formatted_text += "}" + (",\n" if i < len(coverage_diff["Coverage Differences"]) - 1 else "\n")
|
| 77 |
-
formatted_text += "]"
|
| 78 |
-
st.code(formatted_text, language="json")
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
st.info(news_data['Final Sentiment Analysis'])
|
| 83 |
-
|
| 84 |
-
# Download JSON
|
| 85 |
-
st.subheader("Download Data")
|
| 86 |
-
st.download_button(
|
| 87 |
-
label="Download JSON File",
|
| 88 |
-
data=json.dumps(news_data, indent=4),
|
| 89 |
-
file_name=f"{company_name}_news.json",
|
| 90 |
-
mime="application/json"
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
# Hindi Audio
|
| 94 |
-
st.subheader("Hindi Audio for Final Sentiment Analysis")
|
| 95 |
-
try:
|
| 96 |
-
hindi_text = translator.translate(news_data['Final Sentiment Analysis'])
|
| 97 |
-
tts = gTTS(text=hindi_text, lang='hi')
|
| 98 |
-
audio_io = io.BytesIO()
|
| 99 |
-
tts.write_to_fp(audio_io)
|
| 100 |
-
audio_io.seek(0)
|
| 101 |
-
audio_bytes = audio_io.read()
|
| 102 |
st.download_button(
|
| 103 |
label="Download Hindi Audio",
|
| 104 |
-
data=
|
| 105 |
file_name=f"{company_name}_sentiment_hindi.mp3",
|
| 106 |
mime="audio/mp3"
|
| 107 |
)
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
else:
|
| 115 |
st.warning("Please enter a company name.")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import utils
|
| 3 |
+
import os
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import tempfile
|
| 6 |
+
import re
|
| 7 |
+
from deep_translator import GoogleTranslator
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
st.title("News Summarization and Text-to-Speech Application")
|
| 10 |
|
|
|
|
| 11 |
company_name = st.text_input("Enter the company name:", "").strip().lower()
|
| 12 |
+
|
| 13 |
if st.button("Fetch News"):
|
| 14 |
if company_name:
|
| 15 |
+
# Status container for progress updates
|
| 16 |
+
status = st.status("Fetching news...", expanded=True)
|
| 17 |
+
status.write(f"Fetching news for **{company_name}**...")
|
| 18 |
+
|
| 19 |
+
# Fetch and process news
|
| 20 |
+
file_name = utils.fetch_and_save_news(company_name)
|
| 21 |
+
|
| 22 |
+
if os.path.exists(file_name):
|
| 23 |
+
status.update(label="News fetched successfully!", state="complete", expanded=False)
|
| 24 |
+
|
| 25 |
+
st.subheader(f"News Analysis for {company_name.capitalize()}")
|
| 26 |
+
|
| 27 |
+
# News Content Section
|
| 28 |
+
with open(file_name, "r", encoding="utf-8") as file:
|
| 29 |
+
text_content = file.read()
|
| 30 |
+
|
| 31 |
+
with st.expander("View News Analysis", expanded=False):
|
| 32 |
+
st.text_area("Full Analysis", text_content, height=300)
|
| 33 |
+
|
| 34 |
+
# Download Section
|
| 35 |
+
st.subheader("Download Data")
|
| 36 |
+
with open(file_name, "rb") as file:
|
| 37 |
+
st.download_button(
|
| 38 |
+
label="Download Text File",
|
| 39 |
+
data=file,
|
| 40 |
+
file_name=file_name,
|
| 41 |
+
mime="text/plain"
|
| 42 |
+
)
|
| 43 |
|
| 44 |
+
# Final Sentiment Analysis
|
| 45 |
+
final_sentiment_line = ""
|
| 46 |
+
with open(file_name, "r", encoding="utf-8") as file:
|
| 47 |
+
content = file.read()
|
| 48 |
+
match = re.search(r'"Final Sentiment Analysis": "([^"]+)"', content)
|
| 49 |
+
if match:
|
| 50 |
+
final_sentiment_line = match.group(1)
|
| 51 |
+
|
| 52 |
+
if final_sentiment_line:
|
| 53 |
+
st.subheader("Final Sentiment Analysis")
|
| 54 |
+
st.info(final_sentiment_line)
|
| 55 |
|
| 56 |
+
# Hindi Audio Section
|
| 57 |
+
st.subheader("Hindi Audio for Final Sentiment Analysis")
|
| 58 |
+
try:
|
| 59 |
+
translator = GoogleTranslator(source='en', target='hi')
|
| 60 |
+
hindi_text = translator.translate(final_sentiment_line)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
tts = gTTS(text=hindi_text, lang='hi', slow=False)
|
| 63 |
+
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 64 |
+
tts.save(temp_audio_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
with open(temp_audio_file.name, "rb") as audio_file:
|
| 67 |
+
audio_data = audio_file.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
st.download_button(
|
| 69 |
label="Download Hindi Audio",
|
| 70 |
+
data=audio_data,
|
| 71 |
file_name=f"{company_name}_sentiment_hindi.mp3",
|
| 72 |
mime="audio/mp3"
|
| 73 |
)
|
| 74 |
+
|
| 75 |
+
# Clean up temporary file
|
| 76 |
+
os.unlink(temp_audio_file.name)
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
status.update(label="Audio processing error", state="error")
|
| 80 |
+
st.error(f"Error generating Hindi audio: {str(e)}")
|
| 81 |
+
else:
|
| 82 |
+
st.warning("Could not find Final Sentiment Analysis in the text.")
|
| 83 |
+
else:
|
| 84 |
+
status.update(label="No news found", state="error")
|
| 85 |
+
st.warning(f"No relevant news articles found for {company_name}")
|
| 86 |
else:
|
| 87 |
st.warning("Please enter a company name.")
|