Muthusamy6993 commited on
Commit
eea8ead
·
verified ·
1 Parent(s): ed256d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -20
app.py CHANGED
@@ -5,8 +5,8 @@ import pdfplumber
5
  from langchain.chains.mapreduce import MapReduceChain
6
  from langchain.text_splitter import CharacterTextSplitter
7
  from langchain.chains.summarize import load_summarize_chain
8
- from langchain.chat_models import ChatOpenAI
9
- from langchain.document_loaders import UnstructuredFileLoader
10
  from langchain.prompts import PromptTemplate
11
  import logging
12
  import json
@@ -21,17 +21,24 @@ import pandas as pd
21
  import requests
22
  import gradio as gr
23
  import re
24
- from langchain.document_loaders import WebBaseLoader
25
  from langchain.chains.combine_documents.stuff import StuffDocumentsChain
26
  from transformers import pipeline
27
  import plotly.express as px
28
- from langchain.document_loaders.csv_loader import CSVLoader
 
29
  from langchain.chains.llm import LLMChain
30
  import yfinance as yf
31
  import pandas as pd
32
  import nltk
33
  from nltk.tokenize import sent_tokenize
34
  from openai import AzureOpenAI
 
 
 
 
 
 
35
 
36
 
37
  class KeyValueExtractor:
@@ -183,8 +190,8 @@ class KeyValueExtractor:
183
  def analyze_sentiment_for_graph(self, text):
184
 
185
  pipe = pipeline("zero-shot-classification", model=self.model)
186
- label=["Positive", "Negative", "Neutral"]
187
- result = pipe(text, label)
188
  sentiment_scores = {
189
  result['labels'][0]: result['scores'][0],
190
  result['labels'][1]: result['scores'][1],
@@ -288,37 +295,52 @@ class KeyValueExtractor:
288
 
289
  return bullet_string
290
 
291
- def one_year_summary(self,keyword):
292
-
 
293
  csv_path = self.get_finance_data(keyword)
 
294
  df = self.csv_to_dataframe(csv_path)
 
 
 
 
295
  output_file_path = self.save_dataframe_in_text_file(df)
 
 
 
296
  docs = self.csv_loader(output_file_path)
 
 
 
297
  split_docs = self.document_text_spilliter(docs)
 
 
298
 
299
- prompt_template = """Analyze the Financial Details and Write a abractive quick short summary how the company perform up and down,Bullish/Bearish of the following:
 
300
  {text}
301
  CONCISE SUMMARY:"""
302
  prompt = PromptTemplate.from_template(prompt_template)
303
 
304
- # Prepare the template for refining the summary with additional context
305
  refine_template = (
306
  "Your job is to produce a final summary\n"
307
  "We have provided an existing summary up to a certain point: {existing_answer}\n"
308
- "We have the opportunity to refine the existing summary"
309
  "(only if needed) with some more context below.\n"
310
  "------------\n"
311
  "{text}\n"
312
  "------------\n"
313
- "Given the new context, refine the original summary"
314
  "If the context isn't useful, return the original summary."
315
- "10 line summary is enough"
316
  )
317
  refine_prompt = PromptTemplate.from_template(refine_template)
318
 
319
- # Load the summarization chain using the ChatOpenAI language model
320
  chain = load_summarize_chain(
321
- llm = AzureChatOpenAI(azure_deployment = "GPT-3"),
322
  chain_type="refine",
323
  question_prompt=prompt,
324
  refine_prompt=refine_prompt,
@@ -327,11 +349,19 @@ class KeyValueExtractor:
327
  output_key="output_text",
328
  )
329
 
330
- # Generate the refined summary using the loaded summarization chain
331
  result = chain({"input_documents": split_docs}, return_only_outputs=True)
 
 
332
  one_year_perfomance_summary = self.change_bullet_points(result["output_text"])
333
- # Return the refined summary
 
 
 
334
  return one_year_perfomance_summary
 
 
 
335
 
336
  def main(self,keyword):
337
 
@@ -381,19 +411,24 @@ class KeyValueExtractor:
381
  key_value_pair_result = gr.Textbox(label="Discussed Topics", lines = 12)
382
  with gr.Row(elem_id="col-container"):
383
  with gr.Column(scale=1.0, min_width=0):
384
- plot_for_day =gr.Plot(label="Sentiment for Last Day", size=(500, 600))
 
 
 
385
  with gr.Row(elem_id="col-container"):
386
  with gr.Column(scale=1.0, min_width=150):
387
  analyse_sentiment = gr.Button("Analyse Sentiment For Last Day")
388
  with gr.Row(elem_id="col-container"):
389
  with gr.Column(scale=1.0, min_width=150, ):
390
- one_year_summary = gr.Textbox(label="Summary For One Year Perfomance",lines = 12)
391
  with gr.Row(elem_id="col-container"):
392
  with gr.Column(scale=1.0, min_width=150):
393
  one_year = gr.Button("Analyse One Year Summary")
394
  with gr.Row(elem_id="col-container"):
395
  with gr.Column(scale=1.0, min_width=0):
396
- plot_for_year =gr.Plot(label="Sentiment for One Year", size=(500, 600))
 
 
397
  with gr.Row(elem_id="col-container"):
398
  with gr.Column(scale=1.0, min_width=150):
399
  analyse_sentiment_for_year = gr.Button("Analyse Sentiment For One Year")
 
5
  from langchain.chains.mapreduce import MapReduceChain
6
  from langchain.text_splitter import CharacterTextSplitter
7
  from langchain.chains.summarize import load_summarize_chain
8
+
9
+ from langchain_community.document_loaders import UnstructuredFileLoader
10
  from langchain.prompts import PromptTemplate
11
  import logging
12
  import json
 
21
  import requests
22
  import gradio as gr
23
  import re
24
+ from langchain_community.document_loaders import WebBaseLoader
25
  from langchain.chains.combine_documents.stuff import StuffDocumentsChain
26
  from transformers import pipeline
27
  import plotly.express as px
28
+ from langchain_community.document_loaders import CSVLoader
29
+ from langchain_community.chat_models import ChatOpenAI
30
  from langchain.chains.llm import LLMChain
31
  import yfinance as yf
32
  import pandas as pd
33
  import nltk
34
  from nltk.tokenize import sent_tokenize
35
  from openai import AzureOpenAI
36
+ from langchain.prompts import PromptTemplate
37
+ from langchain.chains import load_summarize_chain
38
+ from langchain.chat_models import AzureChatOpenAI
39
+
40
+
41
+
42
 
43
 
44
  class KeyValueExtractor:
 
190
  def analyze_sentiment_for_graph(self, text):
191
 
192
  pipe = pipeline("zero-shot-classification", model=self.model)
193
+ labels=["Positive", "Negative", "Neutral"]
194
+ result = pipe(text, labels)
195
  sentiment_scores = {
196
  result['labels'][0]: result['scores'][0],
197
  result['labels'][1]: result['scores'][1],
 
295
 
296
  return bullet_string
297
 
298
+ def one_year_summary(self, keyword):
299
+ try:
300
+ # Step 1: Get the finance data and convert to DataFrame
301
  csv_path = self.get_finance_data(keyword)
302
+ print(f"CSV path: {csv_path}") # For debugging, ensure it's correct.
303
  df = self.csv_to_dataframe(csv_path)
304
+ if df is None or df.empty:
305
+ raise ValueError("The DataFrame is empty. Please check the CSV content.")
306
+
307
+ # Step 2: Save the DataFrame to a text file
308
  output_file_path = self.save_dataframe_in_text_file(df)
309
+ print(f"Output file saved at: {output_file_path}")
310
+
311
+ # Step 3: Load and split the document data
312
  docs = self.csv_loader(output_file_path)
313
+ if not docs:
314
+ raise ValueError("No content was loaded from the CSV file.")
315
+
316
  split_docs = self.document_text_spilliter(docs)
317
+ if not split_docs:
318
+ raise ValueError("Document splitting failed. No valid chunks were created.")
319
 
320
+ # Step 4: Prepare the summarization prompt
321
+ prompt_template = """Analyze the Financial Details and Write a brief and concise summary of how the company performed:
322
  {text}
323
  CONCISE SUMMARY:"""
324
  prompt = PromptTemplate.from_template(prompt_template)
325
 
326
+ # Step 5: Prepare the refine prompt for summarization chain
327
  refine_template = (
328
  "Your job is to produce a final summary\n"
329
  "We have provided an existing summary up to a certain point: {existing_answer}\n"
330
+ "We have the opportunity to refine the existing summary "
331
  "(only if needed) with some more context below.\n"
332
  "------------\n"
333
  "{text}\n"
334
  "------------\n"
335
+ "Given the new context, refine the original summary. "
336
  "If the context isn't useful, return the original summary."
337
+ "10 lines of summary are enough."
338
  )
339
  refine_prompt = PromptTemplate.from_template(refine_template)
340
 
341
+ # Step 6: Load the summarization chain with Azure ChatGPT
342
  chain = load_summarize_chain(
343
+ llm=AzureChatOpenAI(azure_deployment="GPT-3"),
344
  chain_type="refine",
345
  question_prompt=prompt,
346
  refine_prompt=refine_prompt,
 
349
  output_key="output_text",
350
  )
351
 
352
+ # Step 7: Generate the summary
353
  result = chain({"input_documents": split_docs}, return_only_outputs=True)
354
+
355
+ # Step 8: Process and return the summary
356
  one_year_perfomance_summary = self.change_bullet_points(result["output_text"])
357
+
358
+ # Log final summary
359
+ print(f"Generated Summary: {one_year_perfomance_summary}")
360
+
361
  return one_year_perfomance_summary
362
+ except Exception as e:
363
+ print(f"Error during one_year_summary processing: {str(e)}")
364
+ return None
365
 
366
  def main(self,keyword):
367
 
 
411
  key_value_pair_result = gr.Textbox(label="Discussed Topics", lines = 12)
412
  with gr.Row(elem_id="col-container"):
413
  with gr.Column(scale=1.0, min_width=0):
414
+ plot_for_day =gr.Plot(label="Sentiment for Last Day")
415
+
416
+ plot_for_day.width = 500
417
+ plot_for_day.height = 600
418
  with gr.Row(elem_id="col-container"):
419
  with gr.Column(scale=1.0, min_width=150):
420
  analyse_sentiment = gr.Button("Analyse Sentiment For Last Day")
421
  with gr.Row(elem_id="col-container"):
422
  with gr.Column(scale=1.0, min_width=150, ):
423
+ one_year_summary = gr.Textbox(label="Summary For One Year Performance",lines = 12)
424
  with gr.Row(elem_id="col-container"):
425
  with gr.Column(scale=1.0, min_width=150):
426
  one_year = gr.Button("Analyse One Year Summary")
427
  with gr.Row(elem_id="col-container"):
428
  with gr.Column(scale=1.0, min_width=0):
429
+ plot_for_year =gr.Plot(label="Sentiment for One Year")
430
+ plot_for_day.width = 500
431
+ plot_for_day.height = 600
432
  with gr.Row(elem_id="col-container"):
433
  with gr.Column(scale=1.0, min_width=150):
434
  analyse_sentiment_for_year = gr.Button("Analyse Sentiment For One Year")