DrishtiSharma commited on
Commit
c149248
·
verified ·
1 Parent(s): 3c68f70

Create interim.py

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