File size: 1,881 Bytes
3efe7a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from Embeddings import GetEmbeddings
import json


# Load Agent once and cache it
@st.cache_resource
def load_agent():
    agent = GetEmbeddings(config_path="config.json")
    agent.run()                # Build/load FAISS
    agent.load_summarizer()    # Load summarizer model
    encoder = agent.load_encoder()
    return agent, encoder


def main():
    st.set_page_config(page_title="πŸ“Š Financial QA Agent", layout="wide")

    st.title("πŸ“Š Financial QA Agent")
    st.markdown(
        """
        Ask questions about financial reports.  
        The system retrieves relevant sections from company reports and summarizes them into concise answers.
        """
    )

    # Sidebar
    st.sidebar.header("βš™οΈ Settings")
    show_debug = st.sidebar.checkbox("Show retrieved chunks", value=False)

    # Load Agent
    agent, encoder = load_agent()

    # User Input
    query = st.text_area("Enter your financial question:", height=100)

    if st.button("Get Answer"):
        if query.strip() == "":
            st.warning("⚠️ Please enter a query.")
        else:
            with st.spinner("πŸ”Ž Searching and generating answer..."):
                answer = agent.answer_query(query, top_k=3)

            st.subheader("βœ… Answer")
            st.write(answer)

            if show_debug:
                st.subheader("πŸ“‚ Retrieved Chunks (Debug)")
                # Show top chunks used
                q_emb = encoder.encode(query, convert_to_numpy=True).reshape(1, -1)
                import faiss
                faiss.normalize_L2(q_emb)
                scores, idxs = agent.index.search(q_emb, k=3)
                for score, idx in zip(scores[0], idxs[0]):
                    st.markdown(f"**Score:** {score:.4f}")
                    st.write(agent.metadata[idx]["text"][:500] + "...")


if __name__ == "__main__":
    main()