File size: 4,425 Bytes
e2b8671
 
 
 
 
 
 
 
 
 
 
 
 
 
8427e43
e2b8671
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import streamlit as st
import torch
from rag import FinancialChatbot
import warnings
from transformers import logging

# Suppress warnings
warnings.filterwarnings("ignore")
logging.set_verbosity_error()

torch.classes.__path__ = []

# Use session state to persist the chatbot instance
if "chatbot" not in st.session_state:
    st.session_state.chatbot = FinancialChatbot()

def fetch_answer_from_backend(query):
    """Calls the backend function to get an answer."""
    return st.session_state.chatbot.get_answer(query)  # Use session state chatbot instance



# Initialize Session State for Chat History
if "chat_history" not in st.session_state:
    st.session_state.chat_history = []

if "loading" not in st.session_state:
    st.session_state.loading = False

# Layout and Title
st.title("Financial RAG Chat Assistant")


# st.markdown(
#     """
#     <style>
#     body, .stApp {
#         background: url('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQlSQ-kkQFs_BAOhel_cd_CkFoHN1V-5bUO7vv8NwRp0cjftW6Wm2utQSE&s=10');
#         background-size: cover;
#         # background-attachment: fixed;
#         # background-position: center;
#         # background-repeat: no-repeat;
#     }
#     # .stApp {
#     #     background: transparent;
#     # }
#     # .stTextInput, .stTextArea, .stButton {
#     #     background-color: rgba(0, 0, 0, 0.5);
#     #     border-radius: 10px;
#     #     color: #ffffff;
#     # }
#     # .stTextInput > div > input {
#     #     color: #ffffff;
#     # }
#     # .stButton button {
#     #     color: #ffffff;
#     #     background-color: #4CAF50;
#     #     border: none;
#     #     border-radius: 10px;
#     # }
#     </style>
#     """,
#     unsafe_allow_html=True
# )





# Display Chat History
for chat in st.session_state.chat_history:
    # User's query on the right
    st.markdown(
        f"""
        <div style='text-align: right; max-width: 75%; float: right; clear: both;'>
            <div style='font-size: 13px; color: #4a4a4a; margin: 0 10px;'>You</div>
            <div style='background-color: #d3d3d3; color: black; padding: 10px; border-radius: 10px; 
                        box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.1);'>
                {chat['question']}
            </div>
        </div>
        """,
        unsafe_allow_html=True
    )
    
    # Assistant's label and response on the left
    if chat["answer"] is not None:
        st.markdown(
            f"""
            <div style='text-align: left; max-width: 75%; float: left; clear: both;'>
                <div style='font-size: 13px; color: #4a4a4a; margin: 0 10px;'>Assistant</div>
                <div style='background-color: #add8e6; color: black; padding: 10px; border-radius: 10px; 
                        box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.1);'>
                    {chat['answer']}
                </div>
            </div>
            """,
        unsafe_allow_html=True
    )
    
    # Confidence Score (Below the answer)
    if chat["confidence"] is not None:
        st.markdown(
            f"<div style='clear: both; color: #4a4a4a; font-size: 13px;'>Confidence: {chat['confidence'] * 100}%</div>",
            unsafe_allow_html=True
        )
        st.divider()  # Adds a visual divider between Q&A pairs

# User Input (Always at the Bottom)
user_input = st.chat_input("Ask a financial question...")

# If user inputs a question
if user_input:
    # Add question to chat history and show loading animation
    st.session_state.chat_history.append({
        "question": user_input,
        "answer": None,  # Placeholder for the answer
        "confidence": None  # Placeholder for the confidence score
    })
    st.session_state.loading = True
    st.rerun()  # Refresh to display the question immediately

# If loading, simulate the API call and update the last question's answer
if st.session_state.loading:
    with st.spinner("Fetching answer..."):
        # Get the last question
        last_question = st.session_state.chat_history[-1]["question"]

        # API Call
        answer, confidence = fetch_answer_from_backend(last_question)

        # Update the last chat history item with the answer and confidence
        st.session_state.chat_history[-1]["answer"] = answer
        st.session_state.chat_history[-1]["confidence"] = confidence

    # Stop loading and refresh to show the answer
    st.session_state.loading = False
    st.rerun()