Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
api_token = os.getenv("HF_TOKEN")
|
| 4 |
|
| 5 |
from langchain_community.vectorstores import FAISS
|
|
@@ -13,9 +16,14 @@ from langchain_community.llms import HuggingFaceEndpoint
|
|
| 13 |
list_llm = ["meta-llama/Meta-Llama-3-8B-Instruct", "mistralai/Mistral-7B-Instruct-v0.2"]
|
| 14 |
list_llm_simple = [os.path.basename(llm) for llm in list_llm]
|
| 15 |
|
| 16 |
-
# Simulated user
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Load and split PDF document
|
| 21 |
def load_doc(list_file_path):
|
|
@@ -121,31 +129,50 @@ def conversation(qa_chain, message, history, language):
|
|
| 121 |
return qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
|
| 122 |
|
| 123 |
# Login function
|
| 124 |
-
def
|
| 125 |
-
if username
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
else:
|
| 128 |
-
return False, "Invalid username or password. Please try again."
|
| 129 |
|
| 130 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
def demo():
|
| 132 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
# State variables
|
| 134 |
vector_db = gr.State()
|
| 135 |
qa_chain = gr.State()
|
| 136 |
logged_in = gr.State(value=False)
|
|
|
|
| 137 |
|
| 138 |
# Login interface
|
| 139 |
-
with gr.Column(visible=True) as login_col:
|
| 140 |
-
gr.HTML("<
|
| 141 |
-
username = gr.Textbox(label="Username", placeholder="Enter username")
|
| 142 |
-
password = gr.Textbox(label="Password", type="password", placeholder="Enter password")
|
| 143 |
-
login_btn = gr.Button("Login")
|
| 144 |
-
login_message = gr.Textbox(value="Please log in to access the chatbot.", show_label=False)
|
| 145 |
|
| 146 |
# Chatbot interface (hidden until login)
|
| 147 |
with gr.Column(visible=False) as chatbot_col:
|
| 148 |
-
gr.
|
|
|
|
|
|
|
| 149 |
gr.Markdown("""<b>Query your PDF documents!</b> This AI agent is designed to perform retrieval augmented generation (RAG) on PDF documents. \
|
| 150 |
<b>Please do not upload confidential documents.</b>""")
|
| 151 |
|
|
@@ -153,7 +180,7 @@ def demo():
|
|
| 153 |
with gr.Column(scale=86):
|
| 154 |
gr.Markdown("<b>Step 1 - Upload PDF documents and Initialize RAG pipeline</b>")
|
| 155 |
document = gr.Files(height=300, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload PDF documents")
|
| 156 |
-
db_btn = gr.Button("Create vector database")
|
| 157 |
db_progress = gr.Textbox(value="Not initialized", show_label=False)
|
| 158 |
gr.Markdown("<b>Select Large Language Model (LLM) and input parameters</b>")
|
| 159 |
llm_btn = gr.Radio(list_llm_simple, label="Available LLMs", value=list_llm_simple[0], type="index")
|
|
@@ -161,7 +188,7 @@ def demo():
|
|
| 161 |
slider_temperature = gr.Slider(minimum=0.01, maximum=1.0, value=0.5, step=0.1, label="Temperature", interactive=True)
|
| 162 |
slider_maxtokens = gr.Slider(minimum=128, maximum=9192, value=4096, step=128, label="Max New Tokens", interactive=True)
|
| 163 |
slider_topk = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="top-k", interactive=True)
|
| 164 |
-
qachain_btn = gr.Button("Initialize Question Answering Chatbot")
|
| 165 |
llm_progress = gr.Textbox(value="Not initialized", show_label=False)
|
| 166 |
|
| 167 |
with gr.Column(scale=200):
|
|
@@ -176,14 +203,14 @@ def demo():
|
|
| 176 |
doc_source3 = gr.Textbox(label="Reference 3", lines=2, container=True, scale=20)
|
| 177 |
source3_page = gr.Number(label="Page", scale=1)
|
| 178 |
msg = gr.Textbox(placeholder="Ask a question", container=True)
|
| 179 |
-
submit_btn = gr.Button("Submit")
|
| 180 |
clear_btn = gr.ClearButton([msg, chatbot], value="Clear")
|
| 181 |
|
| 182 |
# Login event
|
| 183 |
login_btn.click(
|
| 184 |
-
fn=
|
| 185 |
inputs=[username, password],
|
| 186 |
-
outputs=[logged_in, login_message]
|
| 187 |
).then(
|
| 188 |
fn=lambda logged: (gr.update(visible=not logged), gr.update(visible=logged)),
|
| 189 |
inputs=[logged_in],
|
|
@@ -191,7 +218,24 @@ def demo():
|
|
| 191 |
queue=False
|
| 192 |
)
|
| 193 |
|
| 194 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
db_btn.click(initialize_database, inputs=[document], outputs=[vector_db, db_progress])
|
| 196 |
qachain_btn.click(initialize_LLM, inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], outputs=[qa_chain, llm_progress]).then(
|
| 197 |
lambda: [None, "", 0, "", 0, "", 0], inputs=None, outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], queue=False
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import secrets
|
| 4 |
+
from functools import partial
|
| 5 |
+
|
| 6 |
api_token = os.getenv("HF_TOKEN")
|
| 7 |
|
| 8 |
from langchain_community.vectorstores import FAISS
|
|
|
|
| 16 |
list_llm = ["meta-llama/Meta-Llama-3-8B-Instruct", "mistralai/Mistral-7B-Instruct-v0.2"]
|
| 17 |
list_llm_simple = [os.path.basename(llm) for llm in list_llm]
|
| 18 |
|
| 19 |
+
# Simulated user database (replace with a real database in production)
|
| 20 |
+
USER_DB = {
|
| 21 |
+
"admin": {"password": "securepass123", "email": "admin@example.com"},
|
| 22 |
+
"user1": {"password": "userpass456", "email": "user1@example.com"}
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# Session storage (in-memory for simplicity)
|
| 26 |
+
SESSIONS = {}
|
| 27 |
|
| 28 |
# Load and split PDF document
|
| 29 |
def load_doc(list_file_path):
|
|
|
|
| 129 |
return qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
|
| 130 |
|
| 131 |
# Login function
|
| 132 |
+
def login(username, password):
|
| 133 |
+
if username in USER_DB and USER_DB[username]["password"] == password:
|
| 134 |
+
session_token = secrets.token_hex(16)
|
| 135 |
+
SESSIONS[session_token] = username
|
| 136 |
+
return True, session_token, f"Welcome, {username}! You are now logged in."
|
| 137 |
else:
|
| 138 |
+
return False, None, "Invalid username or password. Please try again."
|
| 139 |
|
| 140 |
+
# Logout function
|
| 141 |
+
def logout(session_token):
|
| 142 |
+
if session_token in SESSIONS:
|
| 143 |
+
del SESSIONS[session_token]
|
| 144 |
+
return False, None, "You have been logged out."
|
| 145 |
+
|
| 146 |
+
# Main demo with modern login
|
| 147 |
def demo():
|
| 148 |
+
with gr.Blocks(
|
| 149 |
+
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray", neutral_hue="slate"),
|
| 150 |
+
css="""
|
| 151 |
+
.login-box { max-width: 400px; margin: 50px auto; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); }
|
| 152 |
+
.title { text-align: center; font-size: 2em; margin-bottom: 20px; }
|
| 153 |
+
.button { background-color: #007bff; color: white; border-radius: 5px; }
|
| 154 |
+
.button:hover { background-color: #0056b3; }
|
| 155 |
+
"""
|
| 156 |
+
) as demo:
|
| 157 |
# State variables
|
| 158 |
vector_db = gr.State()
|
| 159 |
qa_chain = gr.State()
|
| 160 |
logged_in = gr.State(value=False)
|
| 161 |
+
session_token = gr.State(value=None)
|
| 162 |
|
| 163 |
# Login interface
|
| 164 |
+
with gr.Column(elem_classes="login-box", visible=True) as login_col:
|
| 165 |
+
gr.HTML("<h1 class='title'>RAG PDF Chatbot Login</h1>")
|
| 166 |
+
username = gr.Textbox(label="Username", placeholder="Enter your username", lines=1)
|
| 167 |
+
password = gr.Textbox(label="Password", type="password", placeholder="Enter your password", lines=1)
|
| 168 |
+
login_btn = gr.Button("Login", elem_classes="button")
|
| 169 |
+
login_message = gr.Textbox(value="Please log in to access the chatbot.", show_label=False, interactive=False)
|
| 170 |
|
| 171 |
# Chatbot interface (hidden until login)
|
| 172 |
with gr.Column(visible=False) as chatbot_col:
|
| 173 |
+
with gr.Row():
|
| 174 |
+
gr.HTML("<h1 class='title'>RAG PDF Chatbot</h1>")
|
| 175 |
+
logout_btn = gr.Button("Logout", elem_classes="button", scale=0)
|
| 176 |
gr.Markdown("""<b>Query your PDF documents!</b> This AI agent is designed to perform retrieval augmented generation (RAG) on PDF documents. \
|
| 177 |
<b>Please do not upload confidential documents.</b>""")
|
| 178 |
|
|
|
|
| 180 |
with gr.Column(scale=86):
|
| 181 |
gr.Markdown("<b>Step 1 - Upload PDF documents and Initialize RAG pipeline</b>")
|
| 182 |
document = gr.Files(height=300, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload PDF documents")
|
| 183 |
+
db_btn = gr.Button("Create vector database", elem_classes="button")
|
| 184 |
db_progress = gr.Textbox(value="Not initialized", show_label=False)
|
| 185 |
gr.Markdown("<b>Select Large Language Model (LLM) and input parameters</b>")
|
| 186 |
llm_btn = gr.Radio(list_llm_simple, label="Available LLMs", value=list_llm_simple[0], type="index")
|
|
|
|
| 188 |
slider_temperature = gr.Slider(minimum=0.01, maximum=1.0, value=0.5, step=0.1, label="Temperature", interactive=True)
|
| 189 |
slider_maxtokens = gr.Slider(minimum=128, maximum=9192, value=4096, step=128, label="Max New Tokens", interactive=True)
|
| 190 |
slider_topk = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="top-k", interactive=True)
|
| 191 |
+
qachain_btn = gr.Button("Initialize Question Answering Chatbot", elem_classes="button")
|
| 192 |
llm_progress = gr.Textbox(value="Not initialized", show_label=False)
|
| 193 |
|
| 194 |
with gr.Column(scale=200):
|
|
|
|
| 203 |
doc_source3 = gr.Textbox(label="Reference 3", lines=2, container=True, scale=20)
|
| 204 |
source3_page = gr.Number(label="Page", scale=1)
|
| 205 |
msg = gr.Textbox(placeholder="Ask a question", container=True)
|
| 206 |
+
submit_btn = gr.Button("Submit", elem_classes="button")
|
| 207 |
clear_btn = gr.ClearButton([msg, chatbot], value="Clear")
|
| 208 |
|
| 209 |
# Login event
|
| 210 |
login_btn.click(
|
| 211 |
+
fn=login,
|
| 212 |
inputs=[username, password],
|
| 213 |
+
outputs=[logged_in, session_token, login_message]
|
| 214 |
).then(
|
| 215 |
fn=lambda logged: (gr.update(visible=not logged), gr.update(visible=logged)),
|
| 216 |
inputs=[logged_in],
|
|
|
|
| 218 |
queue=False
|
| 219 |
)
|
| 220 |
|
| 221 |
+
# Logout event
|
| 222 |
+
logout_btn.click(
|
| 223 |
+
fn=logout,
|
| 224 |
+
inputs=[session_token],
|
| 225 |
+
outputs=[logged_in, session_token, login_message]
|
| 226 |
+
).then(
|
| 227 |
+
fn=lambda logged: (gr.update(visible=not logged), gr.update(visible=logged)),
|
| 228 |
+
inputs=[logged_in],
|
| 229 |
+
outputs=[login_col, chatbot_col],
|
| 230 |
+
queue=False
|
| 231 |
+
).then(
|
| 232 |
+
fn=lambda: gr.update(value="Please log in to access the chatbot."),
|
| 233 |
+
inputs=None,
|
| 234 |
+
outputs=[login_message],
|
| 235 |
+
queue=False
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
# Preprocessing events
|
| 239 |
db_btn.click(initialize_database, inputs=[document], outputs=[vector_db, db_progress])
|
| 240 |
qachain_btn.click(initialize_LLM, inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], outputs=[qa_chain, llm_progress]).then(
|
| 241 |
lambda: [None, "", 0, "", 0, "", 0], inputs=None, outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], queue=False
|