Sasmita Harini
Run FastAPI as subprocess in app.py
99c7b56
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