localsavageai commited on
Commit
439d179
·
verified ·
1 Parent(s): d59a8ff

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -8
app.py CHANGED
@@ -148,6 +148,9 @@ def create_new_database(file_content: str, db_name: str, password: str, progress
148
  def generate_response(user_input: str, db_name: str) -> Optional[str]:
149
  """Generate response using Qwen2.5 MAX"""
150
  try:
 
 
 
151
  db_path = os.path.join(DATABASE_DIR, db_name)
152
  if not os.path.exists(db_path):
153
  return f"Database '{db_name}' does not exist."
@@ -212,6 +215,15 @@ embeddings = LocalEmbeddings(model)
212
  with gr.Blocks() as app:
213
  gr.Markdown("# Local Tech Knowledge Assistant")
214
 
 
 
 
 
 
 
 
 
 
215
  with gr.Tab("Create Database"):
216
  gr.Markdown("## Create a New FAISS Database")
217
  file_input = gr.File(label="Upload .txt File")
@@ -234,12 +246,16 @@ with gr.Blocks() as app:
234
  else:
235
  return "Invalid file format. Please upload a .txt file."
236
 
237
- return create_new_database(file_content, db_name, password, progress)
 
 
 
 
238
 
239
  create_button.click(
240
  handle_create,
241
  inputs=[file_input, db_name_input, password_input],
242
- outputs=create_output
243
  )
244
 
245
  with gr.Tab("Chat with Database"):
@@ -249,12 +265,9 @@ with gr.Blocks() as app:
249
  msg = gr.Textbox(label="Votre question")
250
  clear = gr.ClearButton([msg, chatbot])
251
 
252
- def update_db_list():
253
- if not os.path.exists(DATABASE_DIR):
254
- return []
255
- return [name for name in os.listdir(DATABASE_DIR) if os.path.isdir(os.path.join(DATABASE_DIR, name))]
256
-
257
  def chat_response(message: str, db_name: str, history: List[Tuple[str, str]]):
 
 
258
  response = generate_response(message, db_name)
259
  return "", history + [(message, response or "Erreur de génération - Veuillez réessayer.")]
260
 
@@ -265,8 +278,15 @@ with gr.Blocks() as app:
265
  queue=True
266
  )
267
 
268
- # Update database list on page load
269
  db_select.choices = update_db_list()
270
 
 
 
 
 
 
 
 
271
  if __name__ == "__main__":
272
  app.launch(server_name="0.0.0.0", server_port=7860)
 
148
  def generate_response(user_input: str, db_name: str) -> Optional[str]:
149
  """Generate response using Qwen2.5 MAX"""
150
  try:
151
+ if not db_name:
152
+ return "Please select a database to chat with."
153
+
154
  db_path = os.path.join(DATABASE_DIR, db_name)
155
  if not os.path.exists(db_path):
156
  return f"Database '{db_name}' does not exist."
 
215
  with gr.Blocks() as app:
216
  gr.Markdown("# Local Tech Knowledge Assistant")
217
 
218
+ # Shared state for database list
219
+ db_list_state = gr.State([])
220
+
221
+ def update_db_list():
222
+ """Update the list of available databases"""
223
+ if not os.path.exists(DATABASE_DIR):
224
+ return []
225
+ return [name for name in os.listdir(DATABASE_DIR) if os.path.isdir(os.path.join(DATABASE_DIR, name))]
226
+
227
  with gr.Tab("Create Database"):
228
  gr.Markdown("## Create a New FAISS Database")
229
  file_input = gr.File(label="Upload .txt File")
 
246
  else:
247
  return "Invalid file format. Please upload a .txt file."
248
 
249
+ result = create_new_database(file_content, db_name, password, progress)
250
+ if "created successfully" in result:
251
+ # Update the database list
252
+ return result, update_db_list()
253
+ return result, None
254
 
255
  create_button.click(
256
  handle_create,
257
  inputs=[file_input, db_name_input, password_input],
258
+ outputs=[create_output, db_list_state]
259
  )
260
 
261
  with gr.Tab("Chat with Database"):
 
265
  msg = gr.Textbox(label="Votre question")
266
  clear = gr.ClearButton([msg, chatbot])
267
 
 
 
 
 
 
268
  def chat_response(message: str, db_name: str, history: List[Tuple[str, str]]):
269
+ if not db_name:
270
+ return "", history + [("System", "Please select a database to chat with.")]
271
  response = generate_response(message, db_name)
272
  return "", history + [(message, response or "Erreur de génération - Veuillez réessayer.")]
273
 
 
278
  queue=True
279
  )
280
 
281
+ # Update dropdown on page load
282
  db_select.choices = update_db_list()
283
 
284
+ # Update dropdown when db_list_state changes
285
+ db_list_state.change(
286
+ lambda dbs: gr.Dropdown.update(choices=dbs),
287
+ inputs=db_list_state,
288
+ outputs=db_select
289
+ )
290
+
291
  if __name__ == "__main__":
292
  app.launch(server_name="0.0.0.0", server_port=7860)