File size: 3,683 Bytes
5492b0a
 
 
 
 
 
 
 
52b8c6b
5492b0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2c88f5
3aa9772
5492b0a
52b8c6b
5492b0a
 
 
 
 
 
 
 
 
 
 
 
3c68f70
5492b0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a4fdbe
5492b0a
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import streamlit as st
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.groq import Groq
from crewai import Agent, Task, Crew
from crewai_tools import LlamaIndexTool
from langchain_openai import ChatOpenAI
from langchain_groq import ChatGroq
import tempfile

st.set_page_config(page_title="Financial Analyst App", layout="wide")

# Environment API Keys
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")

# Streamlit Input for API Keys
st.title("Financial Analysis and Content Generation App")

if not GROQ_API_KEY or not TAVILY_API_KEY:
    st.warning("Please enter valid API keys to proceed.")
    st.stop()

# File Upload
uploaded_file = st.file_uploader("Upload a PDF for Analysis", type="pdf")

if uploaded_file:
    with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
        tmp_file.write(uploaded_file.read())
        pdf_path = tmp_file.name

    st.success("PDF uploaded successfully!")

    # Load and Embed the Document
    st.subheader("Processing PDF...")
    reader = SimpleDirectoryReader(input_files=[pdf_path])
    docs = reader.load_data()
    st.write("Loaded document content: ", docs[0].text[:500])

    embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
    index = VectorStoreIndex.from_documents(docs, embed_model=embed_model)
    query_engine = index.as_query_engine(similarity_top_k=5)

    st.subheader("Setting Up Query Tool")
    #llm = ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"), model="groq/llama-3.2-90b-text-preview")


    
    query_tool = LlamaIndexTool.from_query_engine(
        query_engine,
        name="Financial Query Tool",
        description="Use this tool to lookup insights from the uploaded document.",
    )

    st.success("Query Engine is ready!")

    # Agent Definitions
    chat_llm = ChatOpenAI(
        openai_api_base="https://api.groq.com/openai/v1",
        openai_api_key=GROQ_API_KEY,
        model="groq/llama-3.2-90b-text-preview",
        temperature=0,
        max_tokens=1000,
    )

    researcher = Agent(
        role="Senior Financial Analyst",
        goal="Uncover insights about the document",
        backstory="You are an experienced analyst focused on extracting key financial insights.",
        verbose=True,
        allow_delegation=False,
        tools=[query_tool],
        llm=chat_llm,
    )

    writer = Agent(
        role="Tech Content Strategist",
        goal="Write an engaging blog post based on financial insights",
        backstory="You transform complex financial information into accessible and engaging narratives.",
        llm=chat_llm,
        verbose=True,
        allow_delegation=False,
    )

    # Tasks
    task1 = Task(
        description="Conduct a comprehensive analysis of the uploaded document.",
        expected_output="Full analysis report in bullet points",
        agent=researcher,
    )

    task2 = Task(
        description="""Using the analysis insights, create an engaging blog post that highlights key findings 
        in a simple and accessible manner.""",
        expected_output="A well-structured blog post with at least 4 paragraphs.",
        agent=writer,
    )

    # Crew Execution
    crew = Crew(
        agents=[researcher, writer],
        tasks=[task1, task2],
        verbose=True,
    )

    if st.button("Kickoff Analysis"):
        st.subheader("Running Analysis and Content Generation...")
        result = crew.kickoff()
        st.subheader("Generated Output:")
        st.write(result)
else:
    st.info("Please upload a PDF file to proceed.")