DrishtiSharma commited on
Commit
5492b0a
·
verified ·
1 Parent(s): 6850324

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
4
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
5
+ from llama_index.llms.groq import Groq
6
+ from crewai import Agent, Task, Crew
7
+ from crewai_tools import LlamaIndexTool
8
+ from langchain_openai import ChatOpenAI
9
+ import tempfile
10
+
11
+ st.set_page_config(page_title="Financial Analyst App", layout="wide")
12
+
13
+ # Environment API Keys
14
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
15
+ TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
16
+
17
+ # Streamlit Input for API Keys
18
+ st.title("Financial Analysis and Content Generation App")
19
+
20
+ if not GROQ_API_KEY or not TAVILY_API_KEY:
21
+ st.warning("Please enter valid API keys to proceed.")
22
+ st.stop()
23
+
24
+ # File Upload
25
+ uploaded_file = st.file_uploader("Upload a PDF for Analysis", type="pdf")
26
+
27
+ if uploaded_file:
28
+ with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
29
+ tmp_file.write(uploaded_file.read())
30
+ pdf_path = tmp_file.name
31
+
32
+ st.success("PDF uploaded successfully!")
33
+
34
+ # Load and Embed the Document
35
+ st.subheader("Processing PDF...")
36
+ reader = SimpleDirectoryReader(input_files=[pdf_path])
37
+ docs = reader.load_data()
38
+ st.write("Loaded document content: ", docs[0].text[:500])
39
+
40
+ embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
41
+ index = VectorStoreIndex.from_documents(docs, embed_model=embed_model)
42
+ query_engine = index.as_query_engine(similarity_top_k=5)
43
+
44
+ st.subheader("Setting Up Query Tool")
45
+ llm = Groq(model="llama3-70b-8192", api_key=GROQ_API_KEY)
46
+
47
+ query_tool = LlamaIndexTool.from_query_engine(
48
+ query_engine,
49
+ name="Financial Query Tool",
50
+ description="Use this tool to lookup insights from the uploaded document.",
51
+ )
52
+
53
+ st.success("Query Engine is ready!")
54
+
55
+ # Agent Definitions
56
+ chat_llm = ChatOpenAI(
57
+ openai_api_base="https://api.groq.com/openai/v1",
58
+ openai_api_key=GROQ_API_KEY,
59
+ model="llama3-70b-8192",
60
+ temperature=0,
61
+ max_tokens=1000,
62
+ )
63
+
64
+ researcher = Agent(
65
+ role="Senior Financial Analyst",
66
+ goal="Uncover insights about the document",
67
+ backstory="You are an experienced analyst focused on extracting key financial insights.",
68
+ verbose=True,
69
+ allow_delegation=False,
70
+ tools=[query_tool],
71
+ llm=chat_llm,
72
+ )
73
+
74
+ writer = Agent(
75
+ role="Tech Content Strategist",
76
+ goal="Write an engaging blog post based on financial insights",
77
+ backstory="You transform complex financial information into accessible and engaging narratives.",
78
+ llm=chat_llm,
79
+ verbose=True,
80
+ allow_delegation=False,
81
+ )
82
+
83
+ # Tasks
84
+ task1 = Task(
85
+ description="Conduct a comprehensive analysis of the uploaded document.",
86
+ expected_output="Full analysis report in bullet points",
87
+ agent=researcher,
88
+ )
89
+
90
+ task2 = Task(
91
+ description="""Using the analysis insights, create an engaging blog post that highlights key findings
92
+ in a simple and accessible manner.""",
93
+ expected_output="A well-structured blog post with at least 4 paragraphs.",
94
+ agent=writer,
95
+ )
96
+
97
+ # Crew Execution
98
+ crew = Crew(
99
+ agents=[researcher, writer],
100
+ tasks=[task1, task2],
101
+ verbose=2,
102
+ )
103
+
104
+ if st.button("Kickoff Analysis"):
105
+ st.subheader("Running Analysis and Content Generation...")
106
+ result = crew.kickoff()
107
+ st.subheader("Generated Output:")
108
+ st.write(result)
109
+ else:
110
+ st.info("Please upload a PDF file to proceed.")