Update back.branched.PDFAddedRAG.03282025.app.py
Browse files
back.branched.PDFAddedRAG.03282025.app.py
CHANGED
@@ -99,11 +99,12 @@ def SpeechSynthesis(result):
|
|
99 |
def generate_filename(prompt, file_type, original_name=None):
|
100 |
central = pytz.timezone('US/Central')
|
101 |
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
102 |
-
if original_name:
|
103 |
base_name = os.path.splitext(original_name)[0]
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
107 |
|
108 |
def create_and_save_file(content, file_type="md", prompt=None, original_name=None, should_save=True):
|
109 |
if not should_save:
|
@@ -157,9 +158,9 @@ def process_image(image_input, user_prompt):
|
|
157 |
# Audio Processing
|
158 |
def process_audio(audio_input, text_input=''):
|
159 |
if audio_input:
|
160 |
-
audio_bytes = audio_input.read()
|
161 |
supported_formats = ['flac', 'm4a', 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm']
|
162 |
-
file_ext = os.path.splitext(audio_input.name)[1][1:].lower()
|
163 |
if file_ext not in supported_formats:
|
164 |
st.error(f"Unsupported format: {file_ext}. Supported formats: {supported_formats}")
|
165 |
return
|
@@ -218,8 +219,8 @@ def process_video(video_path, seconds_per_frame=2):
|
|
218 |
clip.audio.write_audiofile(audio_path, bitrate="32k")
|
219 |
clip.audio.close()
|
220 |
clip.close()
|
221 |
-
except:
|
222 |
-
st.warning("No audio track found
|
223 |
audio_path = None
|
224 |
return base64Frames, audio_path
|
225 |
|
@@ -324,8 +325,8 @@ def process_rag_query(query, vector_store_id):
|
|
324 |
)
|
325 |
tool_calls = response.choices[0].message.tool_calls if response.choices[0].message.tool_calls else []
|
326 |
return response.choices[0].message.content, tool_calls
|
327 |
-
except openai.
|
328 |
-
st.error(f"RAG error: {str(e)}
|
329 |
return None, []
|
330 |
|
331 |
def evaluate_rag(vector_store_id, questions_dict):
|
@@ -340,7 +341,7 @@ def evaluate_rag(vector_store_id, questions_dict):
|
|
340 |
response, tool_calls = process_rag_query(query, vector_store_id)
|
341 |
if not tool_calls:
|
342 |
continue
|
343 |
-
retrieved_files = [call.
|
344 |
if expected_file in retrieved_files:
|
345 |
rank = retrieved_files.index(expected_file) + 1
|
346 |
correct_retrievals_at_k += 1
|
@@ -378,8 +379,8 @@ def rag_pdf_gallery():
|
|
378 |
st.markdown(response)
|
379 |
st.write("Retrieved chunks:")
|
380 |
for call in tool_calls:
|
381 |
-
if "file_search" in call.
|
382 |
-
st.json(call.
|
383 |
|
384 |
if st.button("Evaluate RAG Performance"):
|
385 |
with st.spinner("Evaluating..."):
|
@@ -464,13 +465,29 @@ def main():
|
|
464 |
option = st.selectbox("Select Input Type", ("Text", "Image", "Audio", "Video", "ArXiv Search", "RAG PDF Gallery"))
|
465 |
|
466 |
if option == "Text":
|
467 |
-
|
468 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
469 |
with st.spinner("Processing..."):
|
470 |
process_text(text_input)
|
471 |
|
472 |
elif option == "Image":
|
473 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
474 |
image_input = st.file_uploader("Upload an image (max 200MB)", type=["png", "jpg", "jpeg"], accept_multiple_files=False)
|
475 |
if image_input and text_input:
|
476 |
if image_input.size > 200 * 1024 * 1024:
|
@@ -480,6 +497,7 @@ def main():
|
|
480 |
image_response = process_image(image_input, text_input)
|
481 |
with st.chat_message("ai", avatar="🦖"):
|
482 |
st.markdown(image_response)
|
|
|
483 |
|
484 |
elif option == "Audio":
|
485 |
text_input = st.text_input("Audio Prompt:", value="Summarize this audio transcription in Markdown.")
|
@@ -488,10 +506,13 @@ def main():
|
|
488 |
if audio_bytes:
|
489 |
with open("recorded_audio.wav", "wb") as f:
|
490 |
f.write(audio_bytes)
|
491 |
-
|
492 |
-
|
|
|
|
|
493 |
with st.spinner("Processing..."):
|
494 |
process_audio(audio_input, text_input)
|
|
|
495 |
|
496 |
elif option == "Video":
|
497 |
text_input = st.text_input("Video Prompt:", value="Summarize this video and its transcription in Markdown.")
|
@@ -502,6 +523,7 @@ def main():
|
|
502 |
else:
|
503 |
with st.spinner("Processing..."):
|
504 |
process_audio_and_video(video_input)
|
|
|
505 |
|
506 |
elif option == "ArXiv Search":
|
507 |
query = st.text_input("AI Search ArXiv Scholarly Articles:")
|
@@ -509,6 +531,7 @@ def main():
|
|
509 |
with st.spinner("Searching ArXiv..."):
|
510 |
result = search_arxiv(query)
|
511 |
st.markdown(result)
|
|
|
512 |
|
513 |
elif option == "RAG PDF Gallery":
|
514 |
rag_pdf_gallery()
|
@@ -519,7 +542,9 @@ for message in st.session_state.messages:
|
|
519 |
st.markdown(message["content"])
|
520 |
|
521 |
if prompt := st.chat_input("GPT-4o Multimodal ChatBot - What can I help you with?"):
|
522 |
-
|
|
|
|
|
523 |
|
524 |
FileSidebar()
|
525 |
main()
|
|
|
99 |
def generate_filename(prompt, file_type, original_name=None):
|
100 |
central = pytz.timezone('US/Central')
|
101 |
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
102 |
+
if original_name and file_type == "md": # For images
|
103 |
base_name = os.path.splitext(original_name)[0]
|
104 |
+
safe_prompt = re.sub(r'[<>:"/\\|?*\n]', ' ', prompt).strip()[:100]
|
105 |
+
return f"{safe_date_time}_{safe_prompt}_{base_name}.{file_type}"
|
106 |
+
safe_prompt = re.sub(r'[<>:"/\\|?*\n]', ' ', prompt).strip()[:240]
|
107 |
+
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
108 |
|
109 |
def create_and_save_file(content, file_type="md", prompt=None, original_name=None, should_save=True):
|
110 |
if not should_save:
|
|
|
158 |
# Audio Processing
|
159 |
def process_audio(audio_input, text_input=''):
|
160 |
if audio_input:
|
161 |
+
audio_bytes = audio_input if isinstance(audio_input, bytes) else audio_input.read()
|
162 |
supported_formats = ['flac', 'm4a', 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm']
|
163 |
+
file_ext = "wav" if isinstance(audio_input, bytes) else os.path.splitext(audio_input.name)[1][1:].lower()
|
164 |
if file_ext not in supported_formats:
|
165 |
st.error(f"Unsupported format: {file_ext}. Supported formats: {supported_formats}")
|
166 |
return
|
|
|
219 |
clip.audio.write_audiofile(audio_path, bitrate="32k")
|
220 |
clip.audio.close()
|
221 |
clip.close()
|
222 |
+
except Exception as e:
|
223 |
+
st.warning(f"No audio track found or error: {str(e)}")
|
224 |
audio_path = None
|
225 |
return base64Frames, audio_path
|
226 |
|
|
|
325 |
)
|
326 |
tool_calls = response.choices[0].message.tool_calls if response.choices[0].message.tool_calls else []
|
327 |
return response.choices[0].message.content, tool_calls
|
328 |
+
except openai.BadRequestError as e:
|
329 |
+
st.error(f"RAG query error: {str(e)}")
|
330 |
return None, []
|
331 |
|
332 |
def evaluate_rag(vector_store_id, questions_dict):
|
|
|
341 |
response, tool_calls = process_rag_query(query, vector_store_id)
|
342 |
if not tool_calls:
|
343 |
continue
|
344 |
+
retrieved_files = [call.arguments.get("file_id", "") for call in tool_calls if "file_search" in call.type][:k]
|
345 |
if expected_file in retrieved_files:
|
346 |
rank = retrieved_files.index(expected_file) + 1
|
347 |
correct_retrievals_at_k += 1
|
|
|
379 |
st.markdown(response)
|
380 |
st.write("Retrieved chunks:")
|
381 |
for call in tool_calls:
|
382 |
+
if "file_search" in call.type:
|
383 |
+
st.json(call.arguments)
|
384 |
|
385 |
if st.button("Evaluate RAG Performance"):
|
386 |
with st.spinner("Evaluating..."):
|
|
|
465 |
option = st.selectbox("Select Input Type", ("Text", "Image", "Audio", "Video", "ArXiv Search", "RAG PDF Gallery"))
|
466 |
|
467 |
if option == "Text":
|
468 |
+
default_text = "emojis in markdown. Maybe a buckeyball feature rating comparing them against each other in markdown emoji outline or tables."
|
469 |
+
col1, col2 = st.columns([1, 5])
|
470 |
+
with col1:
|
471 |
+
if st.button("📝 MD", key="md_button"):
|
472 |
+
st.session_state["text_input"] = default_text
|
473 |
+
with st.spinner("Processing..."):
|
474 |
+
process_text(default_text)
|
475 |
+
st.rerun()
|
476 |
+
with col2:
|
477 |
+
text_input = st.text_input("Enter your text:", value=st.session_state.get("text_input", ""), key="text_input_field")
|
478 |
+
if text_input and text_input != st.session_state.get("text_input", ""): # Only process if changed
|
479 |
with st.spinner("Processing..."):
|
480 |
process_text(text_input)
|
481 |
|
482 |
elif option == "Image":
|
483 |
+
col1, col2 = st.columns(2)
|
484 |
+
with col1:
|
485 |
+
if st.button("📝 Describe"):
|
486 |
+
st.session_state["image_prompt"] = "Describe this image and list ten facts in a markdown outline with emojis."
|
487 |
+
with col2:
|
488 |
+
if st.button("🔍 OCR"):
|
489 |
+
st.session_state["image_prompt"] = "Show electronic text of text in the image."
|
490 |
+
text_input = st.text_input("Image Prompt:", value=st.session_state.get("image_prompt", "Describe this image and list ten facts in a markdown outline with emojis."))
|
491 |
image_input = st.file_uploader("Upload an image (max 200MB)", type=["png", "jpg", "jpeg"], accept_multiple_files=False)
|
492 |
if image_input and text_input:
|
493 |
if image_input.size > 200 * 1024 * 1024:
|
|
|
497 |
image_response = process_image(image_input, text_input)
|
498 |
with st.chat_message("ai", avatar="🦖"):
|
499 |
st.markdown(image_response)
|
500 |
+
st.rerun()
|
501 |
|
502 |
elif option == "Audio":
|
503 |
text_input = st.text_input("Audio Prompt:", value="Summarize this audio transcription in Markdown.")
|
|
|
506 |
if audio_bytes:
|
507 |
with open("recorded_audio.wav", "wb") as f:
|
508 |
f.write(audio_bytes)
|
509 |
+
with st.spinner("Processing..."):
|
510 |
+
process_audio(audio_bytes, text_input)
|
511 |
+
st.rerun()
|
512 |
+
elif audio_input and text_input:
|
513 |
with st.spinner("Processing..."):
|
514 |
process_audio(audio_input, text_input)
|
515 |
+
st.rerun()
|
516 |
|
517 |
elif option == "Video":
|
518 |
text_input = st.text_input("Video Prompt:", value="Summarize this video and its transcription in Markdown.")
|
|
|
523 |
else:
|
524 |
with st.spinner("Processing..."):
|
525 |
process_audio_and_video(video_input)
|
526 |
+
st.rerun()
|
527 |
|
528 |
elif option == "ArXiv Search":
|
529 |
query = st.text_input("AI Search ArXiv Scholarly Articles:")
|
|
|
531 |
with st.spinner("Searching ArXiv..."):
|
532 |
result = search_arxiv(query)
|
533 |
st.markdown(result)
|
534 |
+
st.rerun()
|
535 |
|
536 |
elif option == "RAG PDF Gallery":
|
537 |
rag_pdf_gallery()
|
|
|
542 |
st.markdown(message["content"])
|
543 |
|
544 |
if prompt := st.chat_input("GPT-4o Multimodal ChatBot - What can I help you with?"):
|
545 |
+
with st.spinner("Processing..."):
|
546 |
+
process_text(prompt)
|
547 |
+
st.rerun()
|
548 |
|
549 |
FileSidebar()
|
550 |
main()
|