Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,6 @@ from gtts import gTTS
|
|
| 5 |
import tempfile
|
| 6 |
import re
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
-
import json
|
| 9 |
-
import io
|
| 10 |
|
| 11 |
st.title("News Summarization and Text-to-Speech Application")
|
| 12 |
|
|
@@ -15,99 +13,69 @@ company_name = st.text_input("Enter the company name:", "").strip().lower()
|
|
| 15 |
|
| 16 |
if st.button("Fetch News"):
|
| 17 |
if company_name:
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# Read the saved JSON file and parse it
|
| 30 |
-
with open(file_name, "r", encoding="utf-8") as file:
|
| 31 |
-
news_data = json.load(file)
|
| 32 |
-
|
| 33 |
-
status.update(label="News fetched successfully!", state="complete", expanded=False)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
with st.expander(f"Article {i+1}: {article['Title']}", expanded=False):
|
| 42 |
-
st.markdown(f"**Summary:** {article['Summary']}")
|
| 43 |
-
st.markdown(f"**Sentiment:** {article['Sentiment']}")
|
| 44 |
-
st.markdown(f"**Topics:** {', '.join(article['Topics'])}")
|
| 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 |
-
st.markdown("**Common Topics:**")
|
| 57 |
-
st.write(", ".join(news_data['Topic Overlap']['Common Topics']))
|
| 58 |
-
for key, value in news_data['Topic Overlap'].items():
|
| 59 |
-
if key != "Common Topics":
|
| 60 |
-
st.markdown(f"**{key}:**")
|
| 61 |
-
st.write(", ".join(value))
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
if isinstance(coverage_diff, str):
|
| 67 |
-
st.write(coverage_diff) # Fallback for error cases
|
| 68 |
-
else:
|
| 69 |
-
formatted_text = '"Coverage Differences": [\n'
|
| 70 |
-
for i, item in enumerate(coverage_diff.get("Coverage Differences", [])):
|
| 71 |
-
formatted_text += "{\n"
|
| 72 |
-
formatted_text += f' "Comparison": "{item["Comparison"]}",\n'
|
| 73 |
-
formatted_text += f' "Impact": "{item["Impact"]}"\n'
|
| 74 |
-
formatted_text += "}" + (",\n" if i < len(coverage_diff["Coverage Differences"]) - 1 else "\n")
|
| 75 |
-
formatted_text += "]"
|
| 76 |
-
st.code(formatted_text, language="json")
|
| 77 |
-
|
| 78 |
-
# Final Sentiment Analysis
|
| 79 |
-
st.subheader("Final Sentiment Analysis")
|
| 80 |
-
st.info(news_data['Final Sentiment Analysis'])
|
| 81 |
-
|
| 82 |
-
# Download JSON
|
| 83 |
-
st.subheader("Download Data")
|
| 84 |
-
st.download_button(
|
| 85 |
-
label="Download JSON File",
|
| 86 |
-
data=json.dumps(news_data, indent=4),
|
| 87 |
-
file_name=f"{company_name}_news.json",
|
| 88 |
-
mime="application/json"
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
# Hindi Audio
|
| 92 |
-
st.subheader("Hindi Audio for Final Sentiment Analysis")
|
| 93 |
-
try:
|
| 94 |
-
hindi_text = translator.translate(news_data['Final Sentiment Analysis'])
|
| 95 |
-
tts = gTTS(text=hindi_text, lang='hi')
|
| 96 |
-
audio_io = io.BytesIO()
|
| 97 |
-
tts.write_to_fp(audio_io)
|
| 98 |
-
audio_io.seek(0)
|
| 99 |
-
audio_bytes = audio_io.read()
|
| 100 |
st.download_button(
|
| 101 |
label="Download Hindi Audio",
|
| 102 |
-
data=
|
| 103 |
file_name=f"{company_name}_sentiment_hindi.mp3",
|
| 104 |
mime="audio/mp3"
|
| 105 |
)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
else:
|
| 113 |
-
st.warning("Please enter a company name.")
|
|
|
|
| 5 |
import tempfile
|
| 6 |
import re
|
| 7 |
from deep_translator import GoogleTranslator
|
|
|
|
|
|
|
| 8 |
|
| 9 |
st.title("News Summarization and Text-to-Speech Application")
|
| 10 |
|
|
|
|
| 13 |
|
| 14 |
if st.button("Fetch News"):
|
| 15 |
if company_name:
|
| 16 |
+
# Run news extraction and analysis
|
| 17 |
+
st.write(f"Fetching news for **{company_name}**...")
|
| 18 |
+
|
| 19 |
+
# Call the function from utils.py
|
| 20 |
+
file_name = utils.fetch_and_save_news(company_name)
|
| 21 |
+
|
| 22 |
+
if os.path.exists(file_name):
|
| 23 |
+
st.success(f"Data saved in **{file_name}**")
|
| 24 |
|
| 25 |
+
# Read the file to display content
|
| 26 |
+
with open(file_name, "r", encoding="utf-8") as file:
|
| 27 |
+
text_content = file.read()
|
| 28 |
+
st.text_area("News Analysis", text_content, height=400)
|
| 29 |
+
|
| 30 |
+
# Provide a download button for text file
|
| 31 |
+
with open(file_name, "rb") as file:
|
| 32 |
+
st.download_button(
|
| 33 |
+
label="Download Text File",
|
| 34 |
+
data=file,
|
| 35 |
+
file_name=file_name,
|
| 36 |
+
mime="text/plain"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Extract only the Final Sentiment Analysis line
|
| 40 |
+
final_sentiment_line = ""
|
| 41 |
+
with open(file_name, "r", encoding="utf-8") as file:
|
| 42 |
+
content = file.read()
|
| 43 |
+
# Use regular expression to find the Final Sentiment Analysis line
|
| 44 |
+
match = re.search(r'"Final Sentiment Analysis": "([^"]+)"', content)
|
| 45 |
+
if match:
|
| 46 |
+
final_sentiment_line = match.group(1)
|
| 47 |
+
|
| 48 |
+
if final_sentiment_line:
|
| 49 |
+
st.subheader("Hindi Audio for Final Sentiment Analysis")
|
| 50 |
|
| 51 |
+
try:
|
| 52 |
+
# First translate the English text to Hindi using deep_translator
|
| 53 |
+
translator = GoogleTranslator(source='en', target='hi')
|
| 54 |
+
hindi_text = translator.translate(final_sentiment_line)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Create Hindi audio from the translated text
|
| 57 |
+
tts = gTTS(text=hindi_text, lang='hi', slow=False)
|
| 58 |
|
| 59 |
+
# Save the audio in a temporary file
|
| 60 |
+
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 61 |
+
tts.save(temp_audio_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Provide download button for the audio
|
| 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 |
+
except Exception as e:
|
| 75 |
+
st.error(f"Error generating Hindi audio: {str(e)}")
|
| 76 |
+
else:
|
| 77 |
+
st.warning("Could not find Final Sentiment Analysis in the text.")
|
| 78 |
+
else:
|
| 79 |
+
st.error("No relevant news articles found.")
|
| 80 |
else:
|
| 81 |
+
st.warning("Please enter a company name.")
|