rajeshthangaraj1 commited on
Commit
a440dc6
·
verified ·
1 Parent(s): 4849c1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -130
app.py CHANGED
@@ -1,130 +1,130 @@
1
- import streamlit as st
2
- import os
3
- from dotenv import load_dotenv
4
- from file_handler import FileHandler
5
- from chat_handler import ChatHandler
6
-
7
- # Load environment variables
8
- load_dotenv()
9
-
10
- # Static credentials
11
- USERNAME = st.secrets["USERNAME"]
12
- PASSWORD = st.secrets["PASSWORD"]
13
-
14
- # Initialize Handlers
15
- VECTOR_DB_PATH = st.secrets["VECTOR_DB_PATH_DB"]
16
- OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
17
- HUGGINGFACE_API_TOKEN = st.secrets["HUGGINGFACE_API_TOKEN"]
18
- GROQ_API_KEY_TOKEN = st.secrets["GROQ_API_KEY"]
19
-
20
- os.makedirs(VECTOR_DB_PATH, exist_ok=True)
21
-
22
- file_handler = FileHandler(VECTOR_DB_PATH, HUGGINGFACE_API_TOKEN)
23
- chat_handler = ChatHandler(VECTOR_DB_PATH, HUGGINGFACE_API_TOKEN, OPENAI_API_KEY,GROQ_API_KEY_TOKEN)
24
-
25
- # Streamlit UI
26
- st.set_page_config(layout="wide", page_title="AI Connect - Smarter Network Planning for the Future")
27
-
28
- # Session state to track login status
29
- if "logged_in" not in st.session_state:
30
- st.session_state["logged_in"] = False
31
-
32
- # Login page
33
- # Refined Login Page
34
- if not st.session_state["logged_in"]:
35
- # Customize page title
36
- st.markdown(
37
- """
38
- <style>
39
- .title {
40
- font-size: 2.5rem;
41
- color: #1f77b4;
42
- font-weight: bold;
43
- text-align: center;
44
- margin-bottom: 10px;
45
- }
46
- .subtitle {
47
- font-size: 1.2rem;
48
- color: #555;
49
- text-align: center;
50
- margin-bottom: 20px;
51
- }
52
- .login-box {
53
- margin: auto;
54
- width: 50%;
55
- padding: 20px;
56
- background: #f9f9f9;
57
- border: 1px solid #ddd;
58
- border-radius: 10px;
59
- }
60
- .login-box input {
61
- margin-bottom: 10px;
62
- }
63
- </style>
64
- <div>
65
- <div class="title">Welcome to AI Connect</div>
66
- <div class="subtitle">Smarter Network Planning for the Future</div>
67
- </div>
68
- """,
69
- unsafe_allow_html=True,
70
- )
71
-
72
- # Centered Login Box
73
- # st.markdown('<div class="login-box">', unsafe_allow_html=True)
74
- st.subheader("Login to Continue")
75
- username = st.text_input("Username")
76
- password = st.text_input("Password", type="password")
77
- if st.button("Login"):
78
- if username == USERNAME and password == PASSWORD:
79
- st.session_state["logged_in"] = True
80
- st.success("Login successful!")
81
- st.rerun()
82
- else:
83
- st.error("Invalid username or password.")
84
- st.markdown("</div>", unsafe_allow_html=True)
85
- else:
86
- # Main app (Chat Interface)
87
- st.title("Chatbot - Smarter Network Planning for the Future")
88
- st.sidebar.header("Upload Documents")
89
- uploaded_file = st.sidebar.file_uploader("Upload PDF, Excel, Docx, or Txt", type=["pdf", "xlsx", "docx", "txt", "csv"])
90
- document_name = st.sidebar.text_input("Document Name", "")
91
- document_description = st.sidebar.text_area("Document Description", "")
92
-
93
- if st.sidebar.button("Process File"):
94
- if uploaded_file:
95
- with st.spinner("Processing your file..."):
96
- response = file_handler.handle_file_upload(
97
- file=uploaded_file,
98
- document_name=document_name,
99
- document_description=document_description,
100
- )
101
- st.sidebar.success(f"File processed: {response['message']}")
102
- else:
103
- st.sidebar.warning("Please upload a file before processing.")
104
-
105
- # Chat Interface
106
- if "messages" not in st.session_state:
107
- st.session_state["messages"] = []
108
-
109
- # Display chat messages from history
110
- for message in st.session_state["messages"]:
111
- with st.chat_message(message["role"]):
112
- st.markdown(message["content"])
113
-
114
- # Accept user input
115
- if prompt := st.chat_input("Type your question here..."):
116
- with st.chat_message("user"):
117
- st.markdown(prompt)
118
- st.session_state["messages"].append({"role": "user", "content": prompt})
119
-
120
- with st.spinner("Processing your question..."):
121
- response = chat_handler.answer_question(prompt)
122
- with st.chat_message("assistant"):
123
- st.markdown(response)
124
- st.session_state["messages"].append({"role": "assistant", "content": response})
125
-
126
- # Logout button
127
- if st.session_state["logged_in"]:
128
- if st.sidebar.button("Logout"):
129
- st.session_state["logged_in"] = False
130
- st.rerun()
 
1
+ import streamlit as st
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from file_handler import FileHandler
5
+ from chat_handler import ChatHandler
6
+
7
+ # Load environment variables
8
+ load_dotenv()
9
+
10
+ # Static credentials
11
+ USERNAME = os.environ.get("USERNAME")
12
+ PASSWORD = os.environ.get("PASSWORD")
13
+
14
+ # Initialize Handlers
15
+ VECTOR_DB_PATH = os.environ.get("VECTOR_DB_PATH_DB")
16
+ OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
17
+ HUGGINGFACE_API_TOKEN = os.environ.get("HUGGINGFACE_API_TOKEN")
18
+ GROQ_API_KEY_TOKEN = os.environ.get("GROQ_API_KEY")
19
+
20
+ os.makedirs(VECTOR_DB_PATH, exist_ok=True)
21
+
22
+ file_handler = FileHandler(VECTOR_DB_PATH, HUGGINGFACE_API_TOKEN)
23
+ chat_handler = ChatHandler(VECTOR_DB_PATH, HUGGINGFACE_API_TOKEN, OPENAI_API_KEY,GROQ_API_KEY_TOKEN)
24
+
25
+ # Streamlit UI
26
+ st.set_page_config(layout="wide", page_title="AI Connect - Smarter Network Planning for the Future")
27
+
28
+ # Session state to track login status
29
+ if "logged_in" not in st.session_state:
30
+ st.session_state["logged_in"] = False
31
+
32
+ # Login page
33
+ # Refined Login Page
34
+ if not st.session_state["logged_in"]:
35
+ # Customize page title
36
+ st.markdown(
37
+ """
38
+ <style>
39
+ .title {
40
+ font-size: 2.5rem;
41
+ color: #1f77b4;
42
+ font-weight: bold;
43
+ text-align: center;
44
+ margin-bottom: 10px;
45
+ }
46
+ .subtitle {
47
+ font-size: 1.2rem;
48
+ color: #555;
49
+ text-align: center;
50
+ margin-bottom: 20px;
51
+ }
52
+ .login-box {
53
+ margin: auto;
54
+ width: 50%;
55
+ padding: 20px;
56
+ background: #f9f9f9;
57
+ border: 1px solid #ddd;
58
+ border-radius: 10px;
59
+ }
60
+ .login-box input {
61
+ margin-bottom: 10px;
62
+ }
63
+ </style>
64
+ <div>
65
+ <div class="title">Welcome to AI Connect</div>
66
+ <div class="subtitle">Smarter Network Planning for the Future</div>
67
+ </div>
68
+ """,
69
+ unsafe_allow_html=True,
70
+ )
71
+
72
+ # Centered Login Box
73
+ # st.markdown('<div class="login-box">', unsafe_allow_html=True)
74
+ st.subheader("Login to Continue")
75
+ username = st.text_input("Username")
76
+ password = st.text_input("Password", type="password")
77
+ if st.button("Login"):
78
+ if username == USERNAME and password == PASSWORD:
79
+ st.session_state["logged_in"] = True
80
+ st.success("Login successful!")
81
+ st.rerun()
82
+ else:
83
+ st.error("Invalid username or password.")
84
+ st.markdown("</div>", unsafe_allow_html=True)
85
+ else:
86
+ # Main app (Chat Interface)
87
+ st.title("Chatbot - Smarter Network Planning for the Future")
88
+ st.sidebar.header("Upload Documents")
89
+ uploaded_file = st.sidebar.file_uploader("Upload PDF, Excel, Docx, or Txt", type=["pdf", "xlsx", "docx", "txt", "csv"])
90
+ document_name = st.sidebar.text_input("Document Name", "")
91
+ document_description = st.sidebar.text_area("Document Description", "")
92
+
93
+ if st.sidebar.button("Process File"):
94
+ if uploaded_file:
95
+ with st.spinner("Processing your file..."):
96
+ response = file_handler.handle_file_upload(
97
+ file=uploaded_file,
98
+ document_name=document_name,
99
+ document_description=document_description,
100
+ )
101
+ st.sidebar.success(f"File processed: {response['message']}")
102
+ else:
103
+ st.sidebar.warning("Please upload a file before processing.")
104
+
105
+ # Chat Interface
106
+ if "messages" not in st.session_state:
107
+ st.session_state["messages"] = []
108
+
109
+ # Display chat messages from history
110
+ for message in st.session_state["messages"]:
111
+ with st.chat_message(message["role"]):
112
+ st.markdown(message["content"])
113
+
114
+ # Accept user input
115
+ if prompt := st.chat_input("Type your question here..."):
116
+ with st.chat_message("user"):
117
+ st.markdown(prompt)
118
+ st.session_state["messages"].append({"role": "user", "content": prompt})
119
+
120
+ with st.spinner("Processing your question..."):
121
+ response = chat_handler.answer_question(prompt)
122
+ with st.chat_message("assistant"):
123
+ st.markdown(response)
124
+ st.session_state["messages"].append({"role": "assistant", "content": response})
125
+
126
+ # Logout button
127
+ if st.session_state["logged_in"]:
128
+ if st.sidebar.button("Logout"):
129
+ st.session_state["logged_in"] = False
130
+ st.rerun()