v1shal commited on
Commit
afc8e70
·
verified ·
1 Parent(s): 4fa7d03

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing Libraries and functions from utils.py in approach_api
2
+ import streamlit as st
3
+ from approach_api.utils.news_extraction_api import extract_news
4
+ from approach_api.utils.news_summarisation import summarize_text
5
+ from approach_api.utils.news_sentiment import analyze_sentiment
6
+ from approach_api.utils.topic_extraction import preprocess_text, train_lda, extract_topic_words
7
+ from approach_api.utils.comparative_analysis import comparative_sentiment_analysis
8
+ from approach_api.utils.text_to_speech import text_to_speech
9
+ import os
10
+
11
+ # Function
12
+ def analyze_company_news(company):
13
+ st.write(f"Analyzing company: {company}")
14
+
15
+ with st.spinner("Fetching news articles..."):
16
+ articles = extract_news(company)
17
+ if not articles:
18
+ st.error("No news articles found. Try a different company.")
19
+ return None
20
+ st.write(f"Found {len(articles)} articles")
21
+
22
+ articles_data = []
23
+ texts = [article["content"] for article in articles]
24
+
25
+ with st.spinner("Performing sentiment analysis..."):
26
+ sentiment_results = analyze_sentiment(texts)
27
+ st.write(f"Sentiment analysis completed for {len(sentiment_results['Predicted Sentiment'])} articles")
28
+
29
+ for article, sentiment in zip(articles, sentiment_results["Predicted Sentiment"]):
30
+ summary = summarize_text(article["content"])
31
+ preprocessed_text = preprocess_text([article["content"]])
32
+ lda_model, dictionary = train_lda(preprocessed_text)
33
+ topic_words = extract_topic_words(lda_model)
34
+
35
+ articles_data.append({
36
+ "Title": article["title"],
37
+ "Summary": summary,
38
+ "Sentiment": sentiment,
39
+ "Topics": topic_words
40
+ })
41
+
42
+ with st.spinner("Performing comparative analysis..."):
43
+ analysis_result = comparative_sentiment_analysis(company, articles_data)
44
+ st.write("Comparative analysis completed")
45
+ st.write("Analysis result:", analysis_result)
46
+
47
+ final_summary = f"{company}’s latest news coverage is mostly {analysis_result['Final Sentiment Analysis']}."
48
+
49
+ with st.spinner("Generating Hindi TTS summary..."):
50
+ try:
51
+ audio_file = text_to_speech(final_summary)
52
+ if os.path.exists(audio_file):
53
+ st.write(f"TTS summary generated: {audio_file}")
54
+ else:
55
+ st.error("Failed to generate TTS summary")
56
+ audio_file = None
57
+ except Exception as e:
58
+ st.error(f"TTS generation failed: {str(e)}")
59
+ audio_file = None
60
+
61
+ return {
62
+ "Company": company,
63
+ "Articles": articles_data,
64
+ "Comparative Sentiment Score": analysis_result,
65
+ "Audio": audio_file
66
+ }
67
+
68
+ st.title("Company News Analysis")
69
+ company = st.text_input("Enter the company name for analysis:")
70
+ if st.button("Analyze") and company:
71
+ st.write(f"Starting analysis for: {company}")
72
+ result = analyze_company_news(company)
73
+ if result:
74
+ st.subheader(f"Analysis for {result['Company']}")
75
+
76
+ for article in result["Articles"]:
77
+ st.write(f"**Title:** {article['Title']}")
78
+ st.write(f"**Summary:** {article['Summary']}")
79
+ st.write(f"**Sentiment:** {article['Sentiment']}")
80
+ st.write(f"**Topics:** {', '.join(article['Topics'])}")
81
+ st.markdown("---")
82
+
83
+ st.subheader("Comparative Sentiment Score")
84
+ st.json(result["Comparative Sentiment Score"])
85
+
86
+ st.subheader("Hindi TTS Summary")
87
+ if result["Audio"]:
88
+ st.audio(result["Audio"], format="audio/mp3")
89
+ else:
90
+ st.warning("TTS summary not available")