Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pymupdf
|
3 |
+
import re
|
4 |
+
import traceback
|
5 |
+
import faiss
|
6 |
+
import numpy as np
|
7 |
+
import requests
|
8 |
+
from rank_bm25 import BM25Okapi
|
9 |
+
from sentence_transformers import SentenceTransformer
|
10 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
11 |
+
from langchain_groq import ChatGroq
|
12 |
+
import torch
|
13 |
+
import os
|
14 |
+
from langgraph.graph import StateGraph
|
15 |
+
from typing import TypedDict, List
|
16 |
+
|
17 |
+
# Set up Streamlit page
|
18 |
+
st.set_page_config(page_title="Financial Insights Chatbot", page_icon="π", layout="wide")
|
19 |
+
|
20 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
21 |
+
|
22 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
23 |
+
ALPHA_VANTAGE_API_KEY = os.getenv("ALPHA_VANTAGE_API_KEY")
|
24 |
+
|
25 |
+
temp_llm = ChatGroq(temperature=0, model="llama3-70b-8192", api_key=GROQ_API_KEY)
|
26 |
+
st.success("β
LLM initialized successfully. Using llama3-70b-8192")
|
27 |
+
|
28 |
+
embedding_model = SentenceTransformer("baconnier/Finance2_embedding_small_en-V1.5", device=device)
|
29 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
30 |
+
|
31 |
+
class AgentState(TypedDict):
|
32 |
+
extracted_data: str
|
33 |
+
compliance_issues: str
|
34 |
+
risk_analysis: str
|
35 |
+
report: str
|
36 |
+
audit_log: List[str]
|
37 |
+
approval_status: str
|
38 |
+
|
39 |
+
def extract_data(state: AgentState) -> AgentState:
|
40 |
+
"""Extracts financial data from PDFs or APIs."""
|
41 |
+
if state.get("extracted_data"):
|
42 |
+
return state
|
43 |
+
return {**state, "extracted_data": "Extracted financial data here."}
|
44 |
+
|
45 |
+
def validate_compliance(state: AgentState) -> AgentState:
|
46 |
+
"""Validates financial data against compliance rules."""
|
47 |
+
return {**state, "compliance_issues": "No issues found."}
|
48 |
+
|
49 |
+
def assess_risk(state: AgentState) -> AgentState:
|
50 |
+
"""Analyzes potential financial risks."""
|
51 |
+
return {**state, "risk_analysis": "Low risk identified."}
|
52 |
+
|
53 |
+
def generate_report(state: AgentState) -> AgentState:
|
54 |
+
"""Generates a financial compliance report."""
|
55 |
+
report = f"Report:\nData: {state['extracted_data']}\nCompliance: {state['compliance_issues']}\nRisk: {state['risk_analysis']}"
|
56 |
+
return {**state, "report": report}
|
57 |
+
|
58 |
+
def log_audit(state: AgentState) -> AgentState:
|
59 |
+
"""Maintains an audit trail of changes."""
|
60 |
+
return {**state, "audit_log": state.get("audit_log", []) + ["Report generated"]}
|
61 |
+
|
62 |
+
def approve_report(state: AgentState) -> AgentState:
|
63 |
+
"""Sends the report for approval."""
|
64 |
+
return {**state, "approval_status": "Approved"}
|
65 |
+
|
66 |
+
def display_dashboard(state: AgentState):
|
67 |
+
"""Shows the final insights on Streamlit UI."""
|
68 |
+
st.markdown("### π Final Dashboard")
|
69 |
+
st.write(state)
|
70 |
+
|
71 |
+
workflow = StateGraph(AgentState)
|
72 |
+
workflow.add_node("extract_data", extract_data)
|
73 |
+
workflow.add_node("validate_compliance", validate_compliance)
|
74 |
+
workflow.add_node("assess_risk", assess_risk)
|
75 |
+
workflow.add_node("generate_report", generate_report)
|
76 |
+
workflow.add_node("log_audit", log_audit)
|
77 |
+
workflow.add_node("approve_report", approve_report)
|
78 |
+
workflow.add_edge("extract_data", "validate_compliance")
|
79 |
+
workflow.add_edge("validate_compliance", "assess_risk")
|
80 |
+
workflow.add_edge("assess_risk", "generate_report")
|
81 |
+
workflow.add_edge("generate_report", "log_audit")
|
82 |
+
workflow.add_edge("log_audit", "approve_report")
|
83 |
+
workflow.set_entry_point("extract_data")
|
84 |
+
workflow.set_termination_nodes(["approve_report"])
|
85 |
+
|
86 |
+
if st.button("Run Multi-Agent Workflow"):
|
87 |
+
output = workflow.run({})
|
88 |
+
display_dashboard(output)
|