DHEIVER commited on
Commit
afd06a9
·
verified ·
1 Parent(s): 26a30cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -39
app.py CHANGED
@@ -13,6 +13,10 @@ 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
  # Load and split PDF document
17
  def load_doc(list_file_path):
18
  loaders = [PyPDFLoader(x) for x in list_file_path]
@@ -45,8 +49,8 @@ def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, pr
45
  temperature=temperature,
46
  max_new_tokens=max_tokens,
47
  top_k=top_k,
48
- timeout=120, # Aumentado para 120 segundos
49
- max_retries=3 # Tenta até 3 vezes
50
  )
51
  else:
52
  llm = HuggingFaceEndpoint(
@@ -85,8 +89,6 @@ def format_chat_history(message, chat_history):
85
 
86
  def conversation(qa_chain, message, history, language):
87
  formatted_chat_history = format_chat_history(message, history)
88
-
89
- # Ajustar o prompt com instrução de idioma
90
  if language == "Português":
91
  prompt = f"Responda em português: {message}"
92
  else:
@@ -118,45 +120,78 @@ def conversation(qa_chain, message, history, language):
118
  new_history = history + [(message, response_answer)]
119
  return qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
120
 
 
 
 
 
 
 
 
 
121
  def demo():
122
  with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue="sky")) as demo:
 
123
  vector_db = gr.State()
124
  qa_chain = gr.State()
125
- gr.HTML("<center><h1>RAG PDF Chatbot</h1></center>")
126
- gr.Markdown("""<b>Query your PDF documents!</b> This AI agent is designed to perform retrieval augmented generation (RAG) on PDF documents. \
127
- <b>Please do not upload confidential documents.</b>""")
128
-
129
- with gr.Row():
130
- with gr.Column(scale=86):
131
- gr.Markdown("<b>Step 1 - Upload PDF documents and Initialize RAG pipeline</b>")
132
- document = gr.Files(height=300, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload PDF documents")
133
- db_btn = gr.Button("Create vector database")
134
- db_progress = gr.Textbox(value="Not initialized", show_label=False)
135
- gr.Markdown("<b>Select Large Language Model (LLM) and input parameters</b>")
136
- llm_btn = gr.Radio(list_llm_simple, label="Available LLMs", value=list_llm_simple[0], type="index")
137
- with gr.Accordion("LLM input parameters", open=False):
138
- slider_temperature = gr.Slider(minimum=0.01, maximum=1.0, value=0.5, step=0.1, label="Temperature", interactive=True)
139
- slider_maxtokens = gr.Slider(minimum=128, maximum=9192, value=4096, step=128, label="Max New Tokens", interactive=True)
140
- slider_topk = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="top-k", interactive=True)
141
- qachain_btn = gr.Button("Initialize Question Answering Chatbot")
142
- llm_progress = gr.Textbox(value="Not initialized", show_label=False)
143
-
144
- with gr.Column(scale=200):
145
- gr.Markdown("<b>Step 2 - Chat with your Document</b>")
146
- language_selector = gr.Radio(["English", "Português"], label="Select Language", value="English")
147
- chatbot = gr.Chatbot(height=505)
148
- with gr.Accordion("Relevant context from the source document", open=False):
149
- doc_source1 = gr.Textbox(label="Reference 1", lines=2, container=True, scale=20)
150
- source1_page = gr.Number(label="Page", scale=1)
151
- doc_source2 = gr.Textbox(label="Reference 2", lines=2, container=True, scale=20)
152
- source2_page = gr.Number(label="Page", scale=1)
153
- doc_source3 = gr.Textbox(label="Reference 3", lines=2, container=True, scale=20)
154
- source3_page = gr.Number(label="Page", scale=1)
155
- msg = gr.Textbox(placeholder="Ask a question", container=True)
156
- submit_btn = gr.Button("Submit")
157
- clear_btn = gr.ClearButton([msg, chatbot], value="Clear")
158
-
159
- # Preprocessing events
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  db_btn.click(initialize_database, inputs=[document], outputs=[vector_db, db_progress])
161
  qachain_btn.click(initialize_LLM, inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], outputs=[qa_chain, llm_progress]).then(
162
  lambda: [None, "", 0, "", 0, "", 0], inputs=None, outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], queue=False
 
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 credentials (replace with a secure method in production)
17
+ VALID_USERNAME = "admin"
18
+ VALID_PASSWORD = "password123"
19
+
20
  # Load and split PDF document
21
  def load_doc(list_file_path):
22
  loaders = [PyPDFLoader(x) for x in list_file_path]
 
49
  temperature=temperature,
50
  max_new_tokens=max_tokens,
51
  top_k=top_k,
52
+ timeout=120,
53
+ max_retries=3
54
  )
55
  else:
56
  llm = HuggingFaceEndpoint(
 
89
 
90
  def conversation(qa_chain, message, history, language):
91
  formatted_chat_history = format_chat_history(message, history)
 
 
92
  if language == "Português":
93
  prompt = f"Responda em português: {message}"
94
  else:
 
120
  new_history = history + [(message, response_answer)]
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 check_login(username, password):
125
+ if username == VALID_USERNAME and password == VALID_PASSWORD:
126
+ return True, "Login successful! Access the chatbot below."
127
+ else:
128
+ return False, "Invalid username or password. Please try again."
129
+
130
+ # Main demo with login
131
  def demo():
132
  with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue="sky")) as demo:
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("<center><h1>RAG PDF Chatbot - Login</h1></center>")
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.HTML("<center><h1>RAG PDF Chatbot</h1></center>")
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
+
152
+ with gr.Row():
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")
160
+ with gr.Accordion("LLM input parameters", open=False):
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):
168
+ gr.Markdown("<b>Step 2 - Chat with your Document</b>")
169
+ language_selector = gr.Radio(["English", "Português"], label="Select Language", value="English")
170
+ chatbot = gr.Chatbot(height=505)
171
+ with gr.Accordion("Relevant context from the source document", open=False):
172
+ doc_source1 = gr.Textbox(label="Reference 1", lines=2, container=True, scale=20)
173
+ source1_page = gr.Number(label="Page", scale=1)
174
+ doc_source2 = gr.Textbox(label="Reference 2", lines=2, container=True, scale=20)
175
+ source2_page = gr.Number(label="Page", scale=1)
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=check_login,
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],
190
+ outputs=[login_col, chatbot_col],
191
+ queue=False
192
+ )
193
+
194
+ # Preprocessing events (only accessible after login)
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