KeenWoo commited on
Commit
559b7d5
·
verified ·
1 Parent(s): 099163b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -170,7 +170,9 @@ def collect_settings(*args):
170
 
171
  # In app.py, replace the existing parse_and_tag_entries function with this one.
172
 
173
- def parse_and_tag_entries(text_content: str, source: str) -> List[Document]:
 
 
174
  separator_pattern = r'\n(?:---|--|-|-\*-|-\.-)\n'
175
  entries = re.split(separator_pattern, text_content)
176
 
@@ -196,7 +198,9 @@ def parse_and_tag_entries(text_content: str, source: str) -> List[Document]:
196
 
197
  full_content = f"Title: {title}\n\nContent: {content}"
198
 
199
- print(f" - Parsing entry: '{title}'")
 
 
200
  behavior_options = CONFIG.get("behavior_tags", [])
201
  emotion_options = CONFIG.get("emotion_tags", [])
202
  topic_options = CONFIG.get("topic_tags", [])
@@ -234,30 +238,32 @@ def parse_and_tag_entries(text_content: str, source: str) -> List[Document]:
234
 
235
 
236
 
237
- def handle_add_knowledge(title, text_input, file_input, image_input, yt_url):
 
 
238
  global personal_vectorstore
239
  docs_to_add = []
240
 
241
  # Corrected prioritization of inputs
242
  if text_input and text_input.strip():
243
  # Handle manual text input first
244
- docs_to_add = parse_and_tag_entries(f"Title: {title}\n\nContent: {text_input}", "Text Input")
245
  elif file_input:
246
  content_source = os.path.basename(file_input)
247
  if file_input.lower().endswith('.txt'):
248
  with open(file_input, 'r', encoding='utf-8') as f:
249
  file_content = f.read()
250
- docs_to_add = parse_and_tag_entries(file_content, content_source)
251
  else: # Handle audio/video
252
  final_title = title.strip() if title and title.strip() else "Audio/Video Note"
253
  content_text = transcribe_audio(file_input)
254
  full_content = f"Title: {final_title}\n\nContent: {content_text}"
255
- docs_to_add = parse_and_tag_entries(full_content, content_source)
256
  elif image_input:
257
  final_title = title.strip() if title and title.strip() else "Image Note"
258
  content_text = describe_image(image_input)
259
  full_content = f"Title: {final_title}\n\nContent: {content_text}"
260
- docs_to_add = parse_and_tag_entries(full_content, "Image Input")
261
  elif yt_url and ("youtube.com" in yt_url or "youtu.be" in yt_url):
262
  try:
263
  yt = YouTube(yt_url)
@@ -271,7 +277,7 @@ def handle_add_knowledge(title, text_input, file_input, image_input, yt_url):
271
  content_source = f"YouTube: {video_title}"
272
  os.remove(temp_audio_path)
273
  full_content = f"Title: {final_title}\n\nContent: {content_text}"
274
- docs_to_add = parse_and_tag_entries(full_content, content_source)
275
  except Exception as e:
276
  return f"Error processing YouTube link: {e}"
277
  else:
@@ -732,10 +738,11 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CSS) as demo:
732
  submit_btn.click(fn=chat_fn, inputs=[user_text, audio_in, settings_state, chatbot], outputs=[user_text, audio_out, chatbot])
733
  save_btn.click(fn=save_chat_to_memory, inputs=[chatbot], outputs=[chat_status])
734
  clear_btn.click(lambda: (None, None, [], None, "", ""), outputs=[user_text, audio_out, chatbot, audio_in, user_text, chat_status])
735
-
 
736
  personal_add_btn.click(
737
  fn=handle_add_knowledge,
738
- inputs=[personal_title, personal_text, personal_file, personal_image, personal_yt_url],
739
  outputs=[personal_status]
740
  ).then(
741
  lambda: (None, None, None, None, None),
 
170
 
171
  # In app.py, replace the existing parse_and_tag_entries function with this one.
172
 
173
+ # orignal without debug mode -> def parse_and_tag_entries(text_content: str, source: str) -> List[Document]:
174
+ def parse_and_tag_entries(text_content: str, source: str, settings: dict = None) -> List[Document]:
175
+
176
  separator_pattern = r'\n(?:---|--|-|-\*-|-\.-)\n'
177
  entries = re.split(separator_pattern, text_content)
178
 
 
198
 
199
  full_content = f"Title: {title}\n\nContent: {content}"
200
 
201
+ # add setting for debug mode
202
+ if settings and settings.get("debug_mode"):
203
+ print(f" - Parsing entry: '{title}'")
204
  behavior_options = CONFIG.get("behavior_tags", [])
205
  emotion_options = CONFIG.get("emotion_tags", [])
206
  topic_options = CONFIG.get("topic_tags", [])
 
238
 
239
 
240
 
241
+ # def handle_add_knowledge(title, text_input, file_input, image_input, yt_url):
242
+ def handle_add_knowledge(title, text_input, file_input, image_input, yt_url, settings):
243
+
244
  global personal_vectorstore
245
  docs_to_add = []
246
 
247
  # Corrected prioritization of inputs
248
  if text_input and text_input.strip():
249
  # Handle manual text input first
250
+ docs_to_add = parse_and_tag_entries(f"Title: {title}\n\nContent: {text_input}", "Text Input", settings=settings)
251
  elif file_input:
252
  content_source = os.path.basename(file_input)
253
  if file_input.lower().endswith('.txt'):
254
  with open(file_input, 'r', encoding='utf-8') as f:
255
  file_content = f.read()
256
+ docs_to_add = parse_and_tag_entries(file_content, content_source, settings=settings)
257
  else: # Handle audio/video
258
  final_title = title.strip() if title and title.strip() else "Audio/Video Note"
259
  content_text = transcribe_audio(file_input)
260
  full_content = f"Title: {final_title}\n\nContent: {content_text}"
261
+ docs_to_add = parse_and_tag_entries(full_content, content_source, settings=settings)
262
  elif image_input:
263
  final_title = title.strip() if title and title.strip() else "Image Note"
264
  content_text = describe_image(image_input)
265
  full_content = f"Title: {final_title}\n\nContent: {content_text}"
266
+ docs_to_add = parse_and_tag_entries(full_content, "Image Input", settings=settings)
267
  elif yt_url and ("youtube.com" in yt_url or "youtu.be" in yt_url):
268
  try:
269
  yt = YouTube(yt_url)
 
277
  content_source = f"YouTube: {video_title}"
278
  os.remove(temp_audio_path)
279
  full_content = f"Title: {final_title}\n\nContent: {content_text}"
280
+ docs_to_add = parse_and_tag_entries(full_content, content_source, settings=settings)
281
  except Exception as e:
282
  return f"Error processing YouTube link: {e}"
283
  else:
 
738
  submit_btn.click(fn=chat_fn, inputs=[user_text, audio_in, settings_state, chatbot], outputs=[user_text, audio_out, chatbot])
739
  save_btn.click(fn=save_chat_to_memory, inputs=[chatbot], outputs=[chat_status])
740
  clear_btn.click(lambda: (None, None, [], None, "", ""), outputs=[user_text, audio_out, chatbot, audio_in, user_text, chat_status])
741
+
742
+ # add settings for debug mode
743
  personal_add_btn.click(
744
  fn=handle_add_knowledge,
745
+ inputs=[personal_title, personal_text, personal_file, personal_image, personal_yt_url, settings_state],
746
  outputs=[personal_status]
747
  ).then(
748
  lambda: (None, None, None, None, None),