Entreprenerdly commited on
Commit
4987f13
·
verified ·
1 Parent(s): 7004422

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import chainlit as cl
3
+ from llama_index.core import VectorStoreIndex, Document
4
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
5
+ from llama_index.llms.groq import Groq
6
+ from llama_index.core import ServiceContext
7
+ from llama_index.core.node_parser import SentenceSplitter
8
+ from PyPDF2 import PdfReader
9
+ import tempfile
10
+
11
+ GROQ_API_KEY = "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"
12
+
13
+ # Initialize models
14
+ embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
15
+ llm = Groq(model="llama3-70b-8192", api_key=GROQ_API_KEY)
16
+
17
+ # Create service context
18
+ service_context = ServiceContext.from_defaults(
19
+ llm=llm,
20
+ embed_model=embed_model,
21
+ node_parser=SentenceSplitter(chunk_size=1000, chunk_overlap=200)
22
+ )
23
+
24
+ summary_prompt = (
25
+ "You are a world-class financial analyst with extensive experience analyzing quarterly reports. "
26
+ "Give me a comprehensive summary of the earnings report. Focus on the Strategic Insights and Key Financial Figures. "
27
+ "Answer in extensive bullet points please."
28
+ )
29
+
30
+ question_prompt = (
31
+ "You are a financial analyst with extensive experience analyzing quarterly reports. "
32
+ "Read the earnings call transcript and earnings presentation report and generate 10 questions focusing on the strategic insights and financial figures. "
33
+ "Ask questions that require precise answers and provide strategic insight into the company's financial and strategic performance, such as revenue growth, market trends, profit margins, and more. "
34
+ "Only ask questions that can be answered using the provided document, without making any assumptions or inferences beyond the text. "
35
+ "Please format the questions as a list with a simple '1. Question 1', '2. Question 2', etc. structure. "
36
+ "Unless retrievable from the documents, don't ask questions which cannot be compared to previous periods."
37
+ )
38
+
39
+ @cl.on_chat_start
40
+ async def on_chat_start():
41
+ files = await cl.AskFileMessage(
42
+ content="Please upload a PDF file to begin!",
43
+ accept=["application/pdf"],
44
+ max_size_mb=20,
45
+ ).send()
46
+
47
+ if not files:
48
+ await cl.Message(content="No file was uploaded. Please try again.").send()
49
+ return
50
+
51
+ file = files[0]
52
+ msg = cl.Message(content=f"Processing `{file.name}`...")
53
+ await msg.send()
54
+
55
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
56
+ temp_file.write(file.content)
57
+ temp_file_path = temp_file.name
58
+
59
+ try:
60
+ # Extract text from PDF
61
+ pdf_reader = PdfReader(temp_file_path)
62
+ text = ""
63
+ for page in pdf_reader.pages:
64
+ text += page.extract_text()
65
+
66
+ # Create document
67
+ document = Document(text=text)
68
+
69
+ # Create index
70
+ index = VectorStoreIndex.from_documents(
71
+ [document], service_context=service_context
72
+ )
73
+
74
+ # Store the index in the user session
75
+ cl.user_session.set("index", index)
76
+
77
+ # Generate summary
78
+ query_engine = index.as_query_engine()
79
+ summary_response = await cl.make_async(query_engine.query)(summary_prompt)
80
+ await cl.Message(content=f"**Summary:**\n{summary_response}").send()
81
+
82
+ # Generate questions
83
+ questions_response = await cl.make_async(query_engine.query)(question_prompt)
84
+ questions_format = str(questions_response).split('\n')
85
+ relevant_questions = [question.strip() for question in questions_format if question.strip() and question.strip()[0].isdigit()]
86
+
87
+ # Answer generated questions
88
+ await cl.Message(content="Generated questions and answers:").send()
89
+ for question in relevant_questions:
90
+ response = await cl.make_async(query_engine.query)(question)
91
+ await cl.Message(content=f"**{question}**\n{response}").send()
92
+
93
+ msg.content = f"Processing `{file.name}` done. You can now ask more questions!"
94
+ await msg.update()
95
+
96
+ except Exception as e:
97
+ await cl.Message(content=f"An error occurred during processing: {str(e)}").send()
98
+ finally:
99
+ os.unlink(temp_file_path)
100
+
101
+ @cl.on_message
102
+ async def main(message: cl.Message):
103
+ index = cl.user_session.get("index")
104
+
105
+ if index is None:
106
+ await cl.Message(content="Please upload a file first before asking questions.").send()
107
+ return
108
+
109
+ query_engine = index.as_query_engine()
110
+
111
+ response = await cl.make_async(query_engine.query)(message.content)
112
+
113
+ response_message = cl.Message(content="")
114
+ for token in str(response):
115
+ await response_message.stream_token(token=token)
116
+
117
+ await response_message.send()