Update back.branched.PDFAddedRAG.03282025.app.py
Browse files
back.branched.PDFAddedRAG.03282025.app.py
CHANGED
|
@@ -99,13 +99,13 @@ 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 and file_type == "md": # For images
|
| 103 |
base_name = os.path.splitext(original_name)[0]
|
| 104 |
-
safe_prompt = re.sub(r'[<>:"/\\|?*\n]', ' ', prompt).strip()[:50]
|
| 105 |
file_stem = f"{safe_date_time}_{safe_prompt}_{base_name}"[:100] # Cap at 100 chars
|
| 106 |
return f"{file_stem}.{file_type}"
|
| 107 |
-
|
| 108 |
-
return f"{
|
| 109 |
|
| 110 |
def create_and_save_file(content, file_type="md", prompt=None, original_name=None, should_save=True):
|
| 111 |
if not should_save:
|
|
@@ -152,7 +152,7 @@ def process_image(image_input, user_prompt):
|
|
| 152 |
temperature=0.0
|
| 153 |
)
|
| 154 |
image_response = response.choices[0].message.content
|
| 155 |
-
filename = generate_filename(user_prompt, "md", original_name)
|
| 156 |
create_and_save_file(image_response, "md", user_prompt, original_name, should_save=should_save)
|
| 157 |
return image_response
|
| 158 |
|
|
@@ -428,34 +428,56 @@ def FileSidebar():
|
|
| 428 |
st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
|
| 429 |
|
| 430 |
for file in all_files:
|
| 431 |
-
ext = os.path.splitext(file)[1]
|
| 432 |
col1, col2, col3, col4, col5 = st.sidebar.columns([1, 6, 1, 1, 1])
|
|
|
|
| 433 |
with col1:
|
| 434 |
-
icon = "π" if ext == ".md" else "π" if ext == ".pdf" else "πΌοΈ" if ext
|
| 435 |
if st.button(icon, key=f"view_{file}"):
|
| 436 |
with open(file, "rb") as f:
|
| 437 |
content = f.read()
|
| 438 |
if ext == ".md":
|
| 439 |
-
|
| 440 |
-
|
|
|
|
| 441 |
elif ext == ".pdf":
|
| 442 |
st.download_button("Download PDF", content, file, "application/pdf")
|
| 443 |
st.write("PDF Viewer not natively supported; download to view.")
|
| 444 |
-
elif ext
|
| 445 |
st.image(content, use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
with col2:
|
| 447 |
st.markdown(get_table_download_link(file), unsafe_allow_html=True)
|
| 448 |
with col3:
|
| 449 |
if st.button("π", key=f"open_{file}"):
|
| 450 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 451 |
with col4:
|
| 452 |
if st.button("βΆοΈ", key=f"run_{file}"):
|
| 453 |
-
|
|
|
|
| 454 |
with col5:
|
| 455 |
if st.button("π", key=f"delete_{file}"):
|
| 456 |
os.remove(file)
|
| 457 |
st.rerun()
|
| 458 |
-
|
|
|
|
|
|
|
|
|
|
| 459 |
def create_zip_of_files(files):
|
| 460 |
zip_name = "Files.zip"
|
| 461 |
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
|
@@ -475,8 +497,8 @@ def get_table_download_link(file_path):
|
|
| 475 |
data = f.read()
|
| 476 |
b64 = base64.b64encode(data).decode()
|
| 477 |
file_name = os.path.basename(file_path)
|
| 478 |
-
ext = os.path.splitext(file_name)[1]
|
| 479 |
-
mime_type = "text/markdown" if ext == ".md" else "application/pdf" if ext == ".pdf" else "image/png" if ext
|
| 480 |
return f'<a href="data:{mime_type};base64,{b64}" download="{file_name}">{file_name}</a>'
|
| 481 |
|
| 482 |
# Main Function
|
|
|
|
| 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 |
+
safe_prompt = re.sub(r'[<>:"/\\|?*\n]', ' ', prompt).strip()[:50]
|
| 103 |
if original_name and file_type == "md": # For images
|
| 104 |
base_name = os.path.splitext(original_name)[0]
|
|
|
|
| 105 |
file_stem = f"{safe_date_time}_{safe_prompt}_{base_name}"[:100] # Cap at 100 chars
|
| 106 |
return f"{file_stem}.{file_type}"
|
| 107 |
+
file_stem = f"{safe_date_time}_{safe_prompt}"[:100] # Cap at 100 chars
|
| 108 |
+
return f"{file_stem}.{file_type}"
|
| 109 |
|
| 110 |
def create_and_save_file(content, file_type="md", prompt=None, original_name=None, should_save=True):
|
| 111 |
if not should_save:
|
|
|
|
| 152 |
temperature=0.0
|
| 153 |
)
|
| 154 |
image_response = response.choices[0].message.content
|
| 155 |
+
filename = generate_filename(user_prompt, "md", original_name) # Include prompt in filename
|
| 156 |
create_and_save_file(image_response, "md", user_prompt, original_name, should_save=should_save)
|
| 157 |
return image_response
|
| 158 |
|
|
|
|
| 428 |
st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
|
| 429 |
|
| 430 |
for file in all_files:
|
| 431 |
+
ext = os.path.splitext(file)[1].lower()
|
| 432 |
col1, col2, col3, col4, col5 = st.sidebar.columns([1, 6, 1, 1, 1])
|
| 433 |
+
colFollowUp=""
|
| 434 |
with col1:
|
| 435 |
+
icon = "π" if ext == ".md" else "π" if ext == ".pdf" else "πΌοΈ" if ext in [".png", ".jpg", ".jpeg"] else "π΅" if ext in [".wav", ".mp3"] else "π₯" if ext == ".mp4" else "π"
|
| 436 |
if st.button(icon, key=f"view_{file}"):
|
| 437 |
with open(file, "rb") as f:
|
| 438 |
content = f.read()
|
| 439 |
if ext == ".md":
|
| 440 |
+
colFollowUp=ext
|
| 441 |
+
#st.markdown(content.decode("utf-8"))
|
| 442 |
+
#SpeechSynthesis(content.decode("utf-8"))
|
| 443 |
elif ext == ".pdf":
|
| 444 |
st.download_button("Download PDF", content, file, "application/pdf")
|
| 445 |
st.write("PDF Viewer not natively supported; download to view.")
|
| 446 |
+
elif ext in [".png", ".jpg", ".jpeg"]:
|
| 447 |
st.image(content, use_column_width=True)
|
| 448 |
+
elif ext in [".wav", ".mp3"]:
|
| 449 |
+
st.audio(content)
|
| 450 |
+
elif ext == ".mp4":
|
| 451 |
+
st.video(content)
|
| 452 |
with col2:
|
| 453 |
st.markdown(get_table_download_link(file), unsafe_allow_html=True)
|
| 454 |
with col3:
|
| 455 |
if st.button("π", key=f"open_{file}"):
|
| 456 |
+
with open(file, "rb") as f:
|
| 457 |
+
content = f.read()
|
| 458 |
+
if ext == ".md":
|
| 459 |
+
st.text_area(f"Editing {file}", value=content.decode("utf-8"), height=300, key=f"edit_{file}")
|
| 460 |
+
elif ext == ".pdf":
|
| 461 |
+
st.download_button("Download PDF to Edit", content, file, "application/pdf")
|
| 462 |
+
st.write("PDF editing not supported in-app; download to edit externally.")
|
| 463 |
+
elif ext in [".png", ".jpg", ".jpeg"]:
|
| 464 |
+
st.image(content, use_column_width=True, caption=f"Viewing {file}")
|
| 465 |
+
elif ext in [".wav", ".mp3"]:
|
| 466 |
+
st.audio(content, format=f"audio/{ext[1:]}")
|
| 467 |
+
elif ext == ".mp4":
|
| 468 |
+
st.video(content, format="video/mp4")
|
| 469 |
with col4:
|
| 470 |
if st.button("βΆοΈ", key=f"run_{file}"):
|
| 471 |
+
if ext == ".md":
|
| 472 |
+
process_text(open(file, "r", encoding="utf-8").read())
|
| 473 |
with col5:
|
| 474 |
if st.button("π", key=f"delete_{file}"):
|
| 475 |
os.remove(file)
|
| 476 |
st.rerun()
|
| 477 |
+
if colFollowUp == ".md":
|
| 478 |
+
colFollowUp=ext
|
| 479 |
+
st.markdown(content.decode("utf-8"))
|
| 480 |
+
SpeechSynthesis(content.decode("utf-8"))
|
| 481 |
def create_zip_of_files(files):
|
| 482 |
zip_name = "Files.zip"
|
| 483 |
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
|
|
|
| 497 |
data = f.read()
|
| 498 |
b64 = base64.b64encode(data).decode()
|
| 499 |
file_name = os.path.basename(file_path)
|
| 500 |
+
ext = os.path.splitext(file_name)[1].lower()
|
| 501 |
+
mime_type = "text/markdown" if ext == ".md" else "application/pdf" if ext == ".pdf" else "image/png" if ext in [".png", ".jpg", ".jpeg"] else "audio/wav" if ext == ".wav" else "audio/mpeg" if ext == ".mp3" else "video/mp4" if ext == ".mp4" else "application/octet-stream"
|
| 502 |
return f'<a href="data:{mime_type};base64,{b64}" download="{file_name}">{file_name}</a>'
|
| 503 |
|
| 504 |
# Main Function
|