sidmanale643 commited on
Commit
1a42de3
·
verified ·
1 Parent(s): 57a25d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -21
app.py CHANGED
@@ -47,14 +47,12 @@ def fetch_from_web(query):
47
  )
48
  return {"sources": response['results']}
49
 
50
-
51
  class Sentiment(BaseModel):
52
  summary: str
53
  reasoning: str
54
  topics: List[str]
55
  sentiment: Literal['positive', 'negative', 'neutral']
56
 
57
-
58
  def analyze_sentiment(article):
59
  sentiment_prompt = f"""
60
  Analyze the following news article about a company:
@@ -110,7 +108,6 @@ def analyze_sentiment(article):
110
  logger.error(f"Error parsing sentiment output: {e}")
111
  return None
112
 
113
-
114
  def generate_comparative_sentiment(articles):
115
  sentiment_counts = {"Positive": 0, "Negative": 0, "Neutral": 0}
116
 
@@ -165,7 +162,6 @@ def generate_comparative_sentiment(articles):
165
 
166
  return comparative_sentiment
167
 
168
-
169
  def get_summaries_by_sentiment(articles):
170
  pos_sum = []
171
  neg_sum = []
@@ -191,7 +187,6 @@ def get_summaries_by_sentiment(articles):
191
 
192
  return pos_sum, neg_sum, neutral_sum
193
 
194
-
195
  def comparative_analysis(pos_sum, neg_sum, neutral_sum):
196
  prompt = f"""
197
  Perform a detailed comparative analysis of the sentiment across three categories of articles (Positive, Negative, and Neutral) about a specific company. Address the following aspects:
@@ -223,7 +218,7 @@ def comparative_analysis(pos_sum, neg_sum, neutral_sum):
223
 
224
  def generate_final_report(pos_sum, neg_sum, neutral_sum, comparative_sentiment):
225
  final_report_prompt = f"""
226
- Corporate News Sentiment Analysis Report:
227
 
228
  ### 1. Executive Summary
229
  - Overview of sentiment distribution: {comparative_sentiment["Sentiment Distribution"]['Positive']} positive, {comparative_sentiment["Sentiment Distribution"]['Negative']} negative, {comparative_sentiment["Sentiment Distribution"]['Neutral']} neutral.
@@ -272,7 +267,6 @@ def generate_final_report(pos_sum, neg_sum, neutral_sum, comparative_sentiment):
272
 
273
  ### 9. Appendix
274
  - Full article details (title, publication, date, author, URL).
275
- - Sentiment scoring methodology.
276
  - Media monitoring metrics (reach, engagement, etc.).
277
  """
278
 
@@ -280,10 +274,10 @@ def generate_final_report(pos_sum, neg_sum, neutral_sum, comparative_sentiment):
280
 
281
  return response
282
 
 
283
 
284
- def translate(report):
285
  translation_prompt = f"""
286
- Translate the following corporate sentiment analysis report into Hindi:
287
 
288
  {report}
289
 
@@ -292,7 +286,6 @@ def translate(report):
292
  translation = call_llm(translation_prompt)
293
  return translation
294
 
295
-
296
  def text_to_speech(text):
297
  url = "https://api.elevenlabs.io/v1/text-to-speech/JBFqnCBsd6RMkjVDRZzb?output_format=mp3_44100_128"
298
 
@@ -325,6 +318,7 @@ def text_to_speech(text):
325
  st.title("Company Sentiment Analyzer")
326
 
327
  company_name = st.text_input("Enter Company Name")
 
328
 
329
  # Save your file with the correct path
330
 
@@ -338,7 +332,7 @@ if st.button("Fetch Sentiment Data"):
338
  else:
339
  sentiment_output = [
340
  analyze_sentiment(article)
341
- for article in web_results["sources"][:5]
342
  ]
343
  sentiment_output = [s for s in sentiment_output if s is not None]
344
  logger.info(f"Generating comparative sentiment")
@@ -358,9 +352,9 @@ if st.button("Fetch Sentiment Data"):
358
  )
359
 
360
  logger.info(f"Translating Report")
361
- hindi_translation = translate(final_report)
362
 
363
- #logger.info(f"Generating Speech from Text")
364
  #audio_data = text_to_speech(hindi_translation)
365
 
366
  output_dict = {
@@ -369,7 +363,7 @@ if st.button("Fetch Sentiment Data"):
369
  "comparative_sentiment": comparative_sentiment,
370
  "final_report": final_report,
371
  "hindi_translation": hindi_translation,
372
- #"audio_text": audio_data,
373
  }
374
 
375
  st.subheader("Company Name")
@@ -378,13 +372,11 @@ if st.button("Fetch Sentiment Data"):
378
  st.subheader("Final Report")
379
  st.write(output_dict.get("final_report"))
380
 
381
- # if audio_data:
382
- # st.write(audio_data)
383
- # with st.spinner("Generating audio..."):
384
- # st.audio(audio_data, format="audio/mp3")
385
- # else:
386
- # st.error("Failed to generate audio.")
387
-
388
 
389
  except requests.exceptions.RequestException as e:
390
  st.error(f"Error fetching data: {e}")
 
47
  )
48
  return {"sources": response['results']}
49
 
 
50
  class Sentiment(BaseModel):
51
  summary: str
52
  reasoning: str
53
  topics: List[str]
54
  sentiment: Literal['positive', 'negative', 'neutral']
55
 
 
56
  def analyze_sentiment(article):
57
  sentiment_prompt = f"""
58
  Analyze the following news article about a company:
 
108
  logger.error(f"Error parsing sentiment output: {e}")
109
  return None
110
 
 
111
  def generate_comparative_sentiment(articles):
112
  sentiment_counts = {"Positive": 0, "Negative": 0, "Neutral": 0}
113
 
 
162
 
163
  return comparative_sentiment
164
 
 
165
  def get_summaries_by_sentiment(articles):
166
  pos_sum = []
167
  neg_sum = []
 
187
 
188
  return pos_sum, neg_sum, neutral_sum
189
 
 
190
  def comparative_analysis(pos_sum, neg_sum, neutral_sum):
191
  prompt = f"""
192
  Perform a detailed comparative analysis of the sentiment across three categories of articles (Positive, Negative, and Neutral) about a specific company. Address the following aspects:
 
218
 
219
  def generate_final_report(pos_sum, neg_sum, neutral_sum, comparative_sentiment):
220
  final_report_prompt = f"""
221
+ Corporate News Sentiment Analysis Report:
222
 
223
  ### 1. Executive Summary
224
  - Overview of sentiment distribution: {comparative_sentiment["Sentiment Distribution"]['Positive']} positive, {comparative_sentiment["Sentiment Distribution"]['Negative']} negative, {comparative_sentiment["Sentiment Distribution"]['Neutral']} neutral.
 
267
 
268
  ### 9. Appendix
269
  - Full article details (title, publication, date, author, URL).
 
270
  - Media monitoring metrics (reach, engagement, etc.).
271
  """
272
 
 
274
 
275
  return response
276
 
277
+ def translate(report, target_language):
278
 
 
279
  translation_prompt = f"""
280
+ Translate the following corporate sentiment analysis report into {target_language}:
281
 
282
  {report}
283
 
 
286
  translation = call_llm(translation_prompt)
287
  return translation
288
 
 
289
  def text_to_speech(text):
290
  url = "https://api.elevenlabs.io/v1/text-to-speech/JBFqnCBsd6RMkjVDRZzb?output_format=mp3_44100_128"
291
 
 
318
  st.title("Company Sentiment Analyzer")
319
 
320
  company_name = st.text_input("Enter Company Name")
321
+ target_language = st.text_input("Enter Target Language for Translation")
322
 
323
  # Save your file with the correct path
324
 
 
332
  else:
333
  sentiment_output = [
334
  analyze_sentiment(article)
335
+ for article in web_results["sources"]
336
  ]
337
  sentiment_output = [s for s in sentiment_output if s is not None]
338
  logger.info(f"Generating comparative sentiment")
 
352
  )
353
 
354
  logger.info(f"Translating Report")
355
+ hindi_translation = translate(final_report, target_language= target_language)
356
 
357
+ logger.info(f"Generating Speech from Text")
358
  #audio_data = text_to_speech(hindi_translation)
359
 
360
  output_dict = {
 
363
  "comparative_sentiment": comparative_sentiment,
364
  "final_report": final_report,
365
  "hindi_translation": hindi_translation,
366
+ "audio_text": "",
367
  }
368
 
369
  st.subheader("Company Name")
 
372
  st.subheader("Final Report")
373
  st.write(output_dict.get("final_report"))
374
 
375
+ st.subheader("Translated Report")
376
+ st.write(output_dict.get("hindi_translation", "Please Check Your Internet Connection"))
377
+
378
+ st.subheader("Speech To Text")
379
+ st.write("Request Timed Out Please Check Your Internet Connection")
 
 
380
 
381
  except requests.exceptions.RequestException as e:
382
  st.error(f"Error fetching data: {e}")