ahmedumeraziz commited on
Commit
8b1b20c
Β·
verified Β·
1 Parent(s): 43f8957

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from utils.auth import authenticate, generate_token, verify_token
4
+ from utils.database import initialize_vector_db, add_to_collection
5
+ from utils.rag_utils import process_pdf, get_rag_response
6
+ from huggingface_hub import hf_hub_download
7
+ import time
8
+
9
+ # Configuration
10
+ st.set_page_config(page_title="RAG Chatbot", layout="wide")
11
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
12
+
13
+ # Initialize FAISS vector DB
14
+ if 'vector_db' not in st.session_state:
15
+ with st.spinner("Loading AI model..."):
16
+ st.session_state.vector_db = initialize_vector_db()
17
+
18
+ # Authentication
19
+ def login_section():
20
+ st.sidebar.title("πŸ”‘ Login")
21
+ username = st.sidebar.text_input("Username")
22
+ password = st.sidebar.text_input("Password", type="password")
23
+
24
+ if st.sidebar.button("Login"):
25
+ if authenticate(username, password):
26
+ st.session_state.token = generate_token(username)
27
+ st.session_state.logged_in = True
28
+ st.sidebar.success("Logged in successfully!")
29
+ time.sleep(1)
30
+ st.rerun()
31
+ else:
32
+ st.sidebar.error("Invalid credentials")
33
+
34
+ # Main App
35
+ def main_app():
36
+ st.title("πŸ“š RAG Chatbot with PDF Processing")
37
+
38
+ col1, col2 = st.columns([1, 2])
39
+
40
+ with col1:
41
+ st.subheader("Upload PDF")
42
+ uploaded_file = st.file_uploader("Choose file", type="pdf", label_visibility="collapsed")
43
+ if uploaded_file:
44
+ with st.spinner("Processing PDF..."):
45
+ chunks = process_pdf(uploaded_file)
46
+ if add_to_collection(chunks):
47
+ st.success(f"Processed {len(chunks)} text chunks!")
48
+
49
+ with col2:
50
+ st.subheader("Chat Interface")
51
+ if prompt := st.chat_input("Ask about your documents"):
52
+ with st.spinner("Generating answer..."):
53
+ response = get_rag_response(prompt, st.session_state.vector_db)
54
+ st.chat_message("user").write(prompt)
55
+ st.chat_message("assistant").write(response)
56
+
57
+ # Authentication flow
58
+ if "logged_in" not in st.session_state:
59
+ st.session_state.logged_in = False
60
+
61
+ if st.session_state.logged_in:
62
+ if verify_token(st.session_state.get("token", "")):
63
+ main_app()
64
+ if st.sidebar.button("Logout"):
65
+ st.session_state.logged_in = False
66
+ st.rerun()
67
+ else:
68
+ st.error("Session expired")
69
+ st.session_state.logged_in = False
70
+ login_section()
71
+ else:
72
+ login_section()