Spaces:
Sleeping
Sleeping
File size: 3,436 Bytes
7d03ecd 573b9d7 440ee04 37b27d1 99c7b56 7d03ecd 5b0e184 7d03ecd 440ee04 5b0e184 7d03ecd 573b9d7 440ee04 573b9d7 440ee04 573b9d7 440ee04 573b9d7 440ee04 573b9d7 440ee04 573b9d7 440ee04 1da5140 573b9d7 440ee04 573b9d7 440ee04 573b9d7 440ee04 573b9d7 440ee04 1da5140 573b9d7 440ee04 573b9d7 440ee04 1da5140 573b9d7 5b0e184 573b9d7 440ee04 1da5140 440ee04 1da5140 440ee04 573b9d7 7d03ecd 5b0e184 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import streamlit as st
import utils # Import functions from utils.py
import os
from gtts import gTTS
import tempfile
import re
from deep_translator import GoogleTranslator
st.title("News Summarization and Text-to-Speech Application")
# User input for company name
company_name = st.text_input("Enter the company name:", "").strip().lower()
if st.button("Fetch News"):
if company_name:
# Run news extraction and analysis
st.write(f"Fetching news for **{company_name}**...")
# Call the function from utils.py
file_name = utils.fetch_and_save_news(company_name)
if os.path.exists(file_name):
st.success(f"Data saved in **{file_name}**")
# Read the file to display content
with open(file_name, "r", encoding="utf-8") as file:
text_content = file.read()
st.text_area("News Analysis", text_content, height=400)
# Provide a download button for text file
with open(file_name, "rb") as file:
st.download_button(
label="Download Text File",
data=file,
file_name=file_name,
mime="text/plain"
)
# Extract only the Final Sentiment Analysis line
final_sentiment_line = ""
with open(file_name, "r", encoding="utf-8") as file:
content = file.read()
# Use regular expression to find the Final Sentiment Analysis line
match = re.search(r'"Final Sentiment Analysis": "([^"]+)"', content)
if match:
final_sentiment_line = match.group(1)
if final_sentiment_line:
st.subheader("Hindi Audio for Final Sentiment Analysis")
try:
# First translate the English text to Hindi using deep_translator
translator = GoogleTranslator(source='en', target='hi')
hindi_text = translator.translate(final_sentiment_line)
# Create Hindi audio from the translated text
tts = gTTS(text=hindi_text, lang='hi', slow=False)
# Save the audio in a temporary file
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
tts.save(temp_audio_file.name)
# Provide download button for the audio
with open(temp_audio_file.name, "rb") as audio_file:
audio_data = audio_file.read()
st.download_button(
label="Download Hindi Audio",
data=audio_data,
file_name=f"{company_name}_sentiment_hindi.mp3",
mime="audio/mp3"
)
except Exception as e:
st.error(f"Error generating Hindi audio: {str(e)}")
else:
st.warning("Could not find Final Sentiment Analysis in the text.")
else:
st.error("No relevant news articles found.")
else:
st.warning("Please enter a company name.")
requirements.txt
|