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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -217
app.py CHANGED
@@ -6,7 +6,6 @@ from tavily import TavilyClient
6
  from pydantic import BaseModel
7
  from ollama import chat
8
  from dotenv import load_dotenv
9
- from groq import Groq
10
  import instructor
11
  import logging
12
  from together import Together
@@ -16,12 +15,26 @@ import json
16
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
17
  logger = logging.getLogger(__name__)
18
 
19
- GROQ_API_KEY = "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"
20
  ELEVEN_LABS_API_KEY = "sk_cc3fea7dcfd81744dcc51673fcd011e7315d4732bab408a7"
21
  TAVILY_API_KEY = "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"
22
 
23
  load_dotenv()
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  def fetch_from_web(query):
27
  tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
@@ -42,7 +55,7 @@ class Sentiment(BaseModel):
42
  sentiment: Literal['positive', 'negative', 'neutral']
43
 
44
 
45
- def analyze_sentiment(article, model_provider = "Groq"):
46
  sentiment_prompt = f"""
47
  Analyze the following news article about a company:
48
 
@@ -65,78 +78,30 @@ def analyze_sentiment(article, model_provider = "Groq"):
65
  """
66
 
67
  try:
68
- if model_provider == "Ollama":
69
- response = chat(
70
- messages=[
71
- {
72
- 'role': 'user',
73
- 'content': sentiment_prompt
74
- }
75
- ],
76
- model='llama3.2:3b',
77
- format=Sentiment.model_json_schema(),
78
- )
79
 
80
- sentiment_output = Sentiment.model_validate_json("")
81
-
82
- final_dict = {
83
- "title": article["title"],
84
- "summary": sentiment_output.summary,
85
- "reasoning": sentiment_output.reasoning,
86
- "topics": sentiment_output.topics,
87
- "sentiment": sentiment_output.sentiment
88
- }
89
- elif model_provider == "GROQ":
90
- llm = Groq(api_key=GROQ_API_KEY)
91
- llm = instructor.from_groq(llm, mode=instructor.Mode.TOOLS)
92
-
93
- resp = llm.chat.completions.create(
94
- model="llama-3.3-70b-versatile",
95
- messages=[
96
- {
97
- "role": "user",
98
- "content": sentiment_prompt,
99
- }
100
- ],
101
- response_model=Sentiment,
102
- )
103
-
104
- sentiment_output = resp.model_dump()
105
-
106
- final_dict = {
107
- "title": article["title"],
108
- "summary": sentiment_output.get("summary"),
109
- "reasoning": sentiment_output.get("reasoning"),
110
- "topics": sentiment_output.get("topics"),
111
- "sentiment": sentiment_output.get("sentiment")
112
- }
113
-
114
- elif model_provider == "Together":
115
-
116
- client = Together(api_key = "aa77adf5b5adaefe8fb3e4a5a1e9bb4937ba9d5d362e03de2521631ab9dab07f")
117
-
118
- extract = client.chat.completions.create(
119
- messages=[
120
  {
121
  "role": "user",
122
  "content": sentiment_prompt,
123
  },
124
  ],
125
- model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
126
  response_format={
127
  "type": "json_object",
128
  "schema": Sentiment.model_json_schema(),
129
  },
130
  )
131
 
132
- output = json.loads(extract.choices[0].message.content)
133
 
134
- final_dict = {
135
  "title": article["title"],
136
- "summary": output .get("summary"),
137
- "reasoning": output .get("reasoning"),
138
- "topics": output .get("topics"),
139
- "sentiment": output .get("sentiment")
140
  }
141
 
142
  return final_dict
@@ -227,146 +192,96 @@ def get_summaries_by_sentiment(articles):
227
  return pos_sum, neg_sum, neutral_sum
228
 
229
 
230
- def comparative_analysis(pos_sum, neg_sum, neutral_sum, model_provider = "Groq"):
231
  prompt = f"""
232
- 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:
233
-
234
- 1. **Sentiment Breakdown**: Identify how each category (positive, negative, and neutral) portrays the company. Highlight the language, tone, and emotional cues that shape the sentiment.
235
 
236
- 2. **Key Themes and Topics**: Compare the primary themes and narratives within each sentiment group. What aspects of the company's operations, performance, or reputation does each category focus on?
237
 
238
- 3. **Perceived Company Image**: Analyze how each sentiment type influences public perception of the company. What impression is created by positive vs. negative vs. neutral coverage?
239
 
240
- 4. **Bias and Framing**: Evaluate whether any of the articles reflect explicit biases or specific agendas regarding the company. Are there patterns in how the company is framed across different sentiments?
241
 
242
- 5. **Market or Stakeholder Impact**: Discuss potential effects on stakeholders (e.g., investors, customers, regulators) based on the sentiment of each article type.
243
 
244
- 6. **Comparative Insights**: Provide a concise summary of the major differences and commonalities between the three sentiment groups. What overall narrative emerges about the company?
245
 
246
- ### Positive Articles:
247
- {pos_sum}
248
 
249
- ### Negative Articles:
250
- {neg_sum}
251
 
252
- ### Neutral Articles:
253
- {neutral_sum}
254
- """
255
 
256
- if model_provider == "Ollama":
257
- response = chat(
258
- messages=[
259
- {
260
- 'role': 'user',
261
- 'content': prompt
262
- }
263
- ],
264
- model='llama3.2:3b'
265
- )
266
- response = response.message.content
267
-
268
- else:
269
- llm = Groq(api_key=GROQ_API_KEY)
270
-
271
- chat_completion = llm.chat.completions.create(
272
- messages=[
273
- {
274
- "role": "user",
275
- "content": prompt[:5000],
276
- }
277
- ],
278
- model="llama-3.3-70b-versatile",
279
- )
280
- response = chat_completion.choices[0].message.content
281
-
282
- return response
283
 
 
 
284
 
285
- def generate_final_report(pos_sum, neg_sum, neutral_sum, comparative_sentiment, model_provider = "Groq"):
286
  final_report_prompt = f"""
287
- Corporate News Sentiment Analysis Report:
288
-
289
- ### 1. Executive Summary
290
- - Overview of sentiment distribution: {comparative_sentiment["Sentiment Distribution"]['Positive']} positive, {comparative_sentiment["Sentiment Distribution"]['Negative']} negative, {comparative_sentiment["Sentiment Distribution"]['Neutral']} neutral.
291
- - Highlight the dominant narrative shaping the company's perception.
292
- - Summarize key drivers behind positive and negative sentiments.
293
-
294
- ### 2. Media Coverage Analysis
295
- - Identify major news sources covering the company.
296
- - Highlight patterns in coverage across platforms (e.g., frequency, timing).
297
- - Identify whether media sentiment shifts over time.
298
-
299
- ### 3. Sentiment Breakdown
300
- - **Positive Sentiment:**
301
- * Titles and sources: {pos_sum}
302
- * Key themes, notable quotes, and focal areas (e.g., product, leadership).
303
- - **Negative Sentiment:**
304
- * Titles and sources: {neg_sum}
305
- * Key themes, notable quotes, and areas of concern.
306
- - **Neutral Sentiment:**
307
- * Titles and sources: {neutral_sum}
308
- * Key themes and neutral narratives.
309
-
310
- ### 4. Narrative Analysis
311
- - Identify primary storylines about the company.
312
- - Analyze how the company is positioned (positive, neutral, negative).
313
- - Detect shifts or emerging narratives over time.
314
-
315
- ### 5. Key Drivers of Sentiment
316
- - Identify specific events, announcements, or actions driving media sentiment.
317
- - Evaluate sentiment linked to industry trends vs. company-specific factors.
318
- - Highlight company strengths and weaknesses based on media portrayal.
319
-
320
- ### 6. Competitive Context
321
- - Identify competitor comparisons.
322
- - Analyze how media sentiment about the company compares to industry standards.
323
- - Highlight competitive advantages or concerns raised by the media.
324
-
325
- ### 7. Stakeholder Perspective
326
- - Identify how key stakeholders (e.g., investors, customers, regulators) are represented.
327
- - Analyze stakeholder concerns and reputation risks/opportunities.
328
-
329
- ### 8. Recommendations
330
- - Suggest strategies to mitigate negative sentiment.
331
- - Recommend approaches to amplify positive narratives.
332
- - Provide messaging suggestions for future announcements.
333
-
334
- ### 9. Appendix
335
- - Full article details (title, publication, date, author, URL).
336
- - Sentiment scoring methodology.
337
- - Media monitoring metrics (reach, engagement, etc.).
338
- """
339
-
340
- if model_provider == "Ollama":
341
- final_report = chat(
342
- messages=[
343
- {
344
- 'role': 'user',
345
- 'content': final_report_prompt
346
- }
347
- ],
348
- model='llama3.2:3b'
349
- )
350
- response = final_report.message.content
351
-
352
- else:
353
- llm = Groq(api_key=GROQ_API_KEY)
354
-
355
- chat_completion = llm.chat.completions.create(
356
- messages=[
357
- {
358
- "role": "user",
359
- "content": final_report_prompt[:5000],
360
- }
361
- ],
362
- model="llama-3.3-70b-versatile",
363
- )
364
- response = chat_completion.choices[0].message.content
365
 
366
  return response
367
 
368
 
369
- def translate(report, model_provider = "Groq"):
370
  translation_prompt = f"""
371
  Translate the following corporate sentiment analysis report into Hindi:
372
 
@@ -374,33 +289,8 @@ def translate(report, model_provider = "Groq"):
374
 
375
  Ensure the translation maintains professional tone and structure while accurately conveying key insights and details.
376
  """
377
- if model_provider == "Ollama":
378
- translation = chat(
379
- messages=[
380
- {
381
- 'role': 'user',
382
- 'content': translation_prompt
383
- }
384
- ],
385
- model='llama3.2:3b'
386
- )
387
- response = translation.message.content
388
-
389
- else:
390
- translation_llm = Groq(api_key=GROQ_API_KEY)
391
-
392
- chat_completion = translation_llm.chat.completions.create(
393
- messages=[
394
- {
395
- "role": "user",
396
- "content": translation_prompt[:5000],
397
- }
398
- ],
399
- model="llama-3.3-70b-versatile",
400
- )
401
- response = chat_completion.choices[0].message.content
402
-
403
- return response
404
 
405
 
406
  def text_to_speech(text):
@@ -447,9 +337,10 @@ if st.button("Fetch Sentiment Data"):
447
  st.error("No sources found.")
448
  else:
449
  sentiment_output = [
450
- analyze_sentiment(article , model_provider = "Together")
451
  for article in web_results["sources"][:5]
452
  ]
 
453
  logger.info(f"Generating comparative sentiment")
454
  comparative_sentiment = generate_comparative_sentiment(sentiment_output)
455
 
@@ -463,15 +354,13 @@ if st.button("Fetch Sentiment Data"):
463
  positive_summary,
464
  negative_summary,
465
  neutral_summary,
466
- comparative_sentiment,
467
- model_provider = "Groq"
468
-
469
  )
470
 
471
  logger.info(f"Translating Report")
472
- hindi_translation = translate(final_report , model_provider = "Groq")
473
 
474
- logger.info(f"Generating Speech from Text")
475
  #audio_data = text_to_speech(hindi_translation)
476
 
477
  output_dict = {
 
6
  from pydantic import BaseModel
7
  from ollama import chat
8
  from dotenv import load_dotenv
 
9
  import instructor
10
  import logging
11
  from together import Together
 
15
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
16
  logger = logging.getLogger(__name__)
17
 
18
+
19
  ELEVEN_LABS_API_KEY = "sk_cc3fea7dcfd81744dcc51673fcd011e7315d4732bab408a7"
20
  TAVILY_API_KEY = "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"
21
 
22
  load_dotenv()
23
 
24
+ def call_llm(prompt):
25
+ client = Together(api_key = "aa77adf5b5adaefe8fb3e4a5a1e9bb4937ba9d5d362e03de2521631ab9dab07f")
26
+ response = client.chat.completions.create(
27
+ model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
28
+ messages=[
29
+ {
30
+ "role": "user",
31
+ "content": prompt
32
+ }
33
+ ]
34
+ )
35
+ response = response.choices[0].message.content
36
+
37
+ return response
38
 
39
  def fetch_from_web(query):
40
  tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
 
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:
61
 
 
78
  """
79
 
80
  try:
81
+ client = Together(api_key = "aa77adf5b5adaefe8fb3e4a5a1e9bb4937ba9d5d362e03de2521631ab9dab07f")
 
 
 
 
 
 
 
 
 
 
82
 
83
+ extract = client.chat.completions.create(
84
+ messages=[
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  {
86
  "role": "user",
87
  "content": sentiment_prompt,
88
  },
89
  ],
90
+ model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
91
  response_format={
92
  "type": "json_object",
93
  "schema": Sentiment.model_json_schema(),
94
  },
95
  )
96
 
97
+ output = json.loads(extract.choices[0].message.content)
98
 
99
+ final_dict = {
100
  "title": article["title"],
101
+ "summary": output.get("summary"),
102
+ "reasoning": output.get("reasoning"),
103
+ "topics": output.get("topics"),
104
+ "sentiment": output.get("sentiment")
105
  }
106
 
107
  return final_dict
 
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:
 
 
198
 
199
+ 1. **Sentiment Breakdown**: Identify how each category (positive, negative, and neutral) portrays the company. Highlight the language, tone, and emotional cues that shape the sentiment.
200
 
201
+ 2. **Key Themes and Topics**: Compare the primary themes and narratives within each sentiment group. What aspects of the company's operations, performance, or reputation does each category focus on?
202
 
203
+ 3. **Perceived Company Image**: Analyze how each sentiment type influences public perception of the company. What impression is created by positive vs. negative vs. neutral coverage?
204
 
205
+ 4. **Bias and Framing**: Evaluate whether any of the articles reflect explicit biases or specific agendas regarding the company. Are there patterns in how the company is framed across different sentiments?
206
 
207
+ 5. **Market or Stakeholder Impact**: Discuss potential effects on stakeholders (e.g., investors, customers, regulators) based on the sentiment of each article type.
208
 
209
+ 6. **Comparative Insights**: Provide a concise summary of the major differences and commonalities between the three sentiment groups. What overall narrative emerges about the company?
 
210
 
211
+ ### Positive Articles:
212
+ {pos_sum}
213
 
214
+ ### Negative Articles:
215
+ {neg_sum}
 
216
 
217
+ ### Neutral Articles:
218
+ {neutral_sum}
219
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
 
221
+ output = call_llm(prompt)
222
+ return output
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.
230
+ - Highlight the dominant narrative shaping the company's perception.
231
+ - Summarize key drivers behind positive and negative sentiments.
232
+
233
+ ### 2. Media Coverage Analysis
234
+ - Identify major news sources covering the company.
235
+ - Highlight patterns in coverage across platforms (e.g., frequency, timing).
236
+ - Identify whether media sentiment shifts over time.
237
+
238
+ ### 3. Sentiment Breakdown
239
+ - **Positive Sentiment:**
240
+ * Titles and sources: {pos_sum}
241
+ * Key themes, notable quotes, and focal areas (e.g., product, leadership).
242
+ - **Negative Sentiment:**
243
+ * Titles and sources: {neg_sum}
244
+ * Key themes, notable quotes, and areas of concern.
245
+ - **Neutral Sentiment:**
246
+ * Titles and sources: {neutral_sum}
247
+ * Key themes and neutral narratives.
248
+
249
+ ### 4. Narrative Analysis
250
+ - Identify primary storylines about the company.
251
+ - Analyze how the company is positioned (positive, neutral, negative).
252
+ - Detect shifts or emerging narratives over time.
253
+
254
+ ### 5. Key Drivers of Sentiment
255
+ - Identify specific events, announcements, or actions driving media sentiment.
256
+ - Evaluate sentiment linked to industry trends vs. company-specific factors.
257
+ - Highlight company strengths and weaknesses based on media portrayal.
258
+
259
+ ### 6. Competitive Context
260
+ - Identify competitor comparisons.
261
+ - Analyze how media sentiment about the company compares to industry standards.
262
+ - Highlight competitive advantages or concerns raised by the media.
263
+
264
+ ### 7. Stakeholder Perspective
265
+ - Identify how key stakeholders (e.g., investors, customers, regulators) are represented.
266
+ - Analyze stakeholder concerns and reputation risks/opportunities.
267
+
268
+ ### 8. Recommendations
269
+ - Suggest strategies to mitigate negative sentiment.
270
+ - Recommend approaches to amplify positive narratives.
271
+ - Provide messaging suggestions for future announcements.
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
+
279
+ response = call_llm(final_report_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
 
289
 
290
  Ensure the translation maintains professional tone and structure while accurately conveying key insights and details.
291
  """
292
+ translation = call_llm(translation_prompt)
293
+ return translation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
 
295
 
296
  def text_to_speech(text):
 
337
  st.error("No sources found.")
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")
345
  comparative_sentiment = generate_comparative_sentiment(sentiment_output)
346
 
 
354
  positive_summary,
355
  negative_summary,
356
  neutral_summary,
357
+ comparative_sentiment
 
 
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 = {