Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -330,7 +330,7 @@ def get_model_files(model_type="causal_lm"):
|
|
| 330 |
return [d for d in glob.glob(path) if os.path.isdir(d)]
|
| 331 |
|
| 332 |
def get_gallery_files(file_types=["png", "pdf"]):
|
| 333 |
-
return sorted([f for ext in file_types for f in glob.glob(f"*.{ext}")])
|
| 334 |
|
| 335 |
def get_pdf_files():
|
| 336 |
return sorted(glob.glob("*.pdf"))
|
|
@@ -539,17 +539,18 @@ def update_gallery():
|
|
| 539 |
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 540 |
st.image(img, caption=os.path.basename(file), use_container_width=True)
|
| 541 |
doc.close()
|
| 542 |
-
checkbox_key = f"asset_{file}"
|
| 543 |
-
st.session_state['asset_checkboxes'][
|
| 544 |
"Use for SFT/Input",
|
| 545 |
-
value=st.session_state['asset_checkboxes'].get(
|
| 546 |
key=checkbox_key
|
| 547 |
)
|
| 548 |
mime_type = "image/png" if file.endswith('.png') else "application/pdf"
|
| 549 |
st.markdown(get_download_link(file, mime_type, "Snag It! 📥"), unsafe_allow_html=True)
|
| 550 |
-
if st.button("Zap It! 🗑️", key=f"delete_{file}"):
|
| 551 |
os.remove(file)
|
| 552 |
-
|
|
|
|
| 553 |
if file.endswith('.pdf'):
|
| 554 |
url_key = next((k for k, v in st.session_state['downloaded_pdfs'].items() if v == file), None)
|
| 555 |
if url_key:
|
|
@@ -664,7 +665,7 @@ with tab2:
|
|
| 664 |
|
| 665 |
mode = st.selectbox("Snapshot Mode", ["Single Page (High-Res)", "Two Pages (High-Res)", "All Pages (High-Res)"], key="download_mode")
|
| 666 |
if st.button("Snapshot Selected 📸"):
|
| 667 |
-
selected_pdfs = [path for path in get_gallery_files() if path.endswith('.pdf') and st.session_state['asset_checkboxes'].get(
|
| 668 |
if selected_pdfs:
|
| 669 |
for pdf_path in selected_pdfs:
|
| 670 |
mode_key = {"Single Page (High-Res)": "single", "Two Pages (High-Res)": "twopage", "All Pages (High-Res)": "allpages"}[mode]
|
|
@@ -672,7 +673,7 @@ with tab2:
|
|
| 672 |
for snapshot in snapshots:
|
| 673 |
st.image(Image.open(snapshot), caption=snapshot, use_container_width=True)
|
| 674 |
else:
|
| 675 |
-
st.warning("No PDFs selected for snapshotting! Check some boxes
|
| 676 |
|
| 677 |
with tab3:
|
| 678 |
st.header("Build Titan 🌱")
|
|
@@ -732,7 +733,7 @@ with tab4:
|
|
| 732 |
st.markdown(get_download_link(zip_path, "application/zip", "Download Fine-Tuned Titan"), unsafe_allow_html=True)
|
| 733 |
st.rerun()
|
| 734 |
elif isinstance(st.session_state['builder'], DiffusionBuilder):
|
| 735 |
-
selected_files = [path for path in get_gallery_files() if st.session_state['asset_checkboxes'].get(
|
| 736 |
if len(selected_files) >= 2:
|
| 737 |
demo_data = [{"image": file, "text": f"Asset {os.path.basename(file).split('.')[0]}"} for file in selected_files]
|
| 738 |
edited_data = st.data_editor(pd.DataFrame(demo_data), num_rows="dynamic")
|
|
@@ -829,7 +830,7 @@ with tab6:
|
|
| 829 |
|
| 830 |
with tab7:
|
| 831 |
st.header("Test OCR 🔍")
|
| 832 |
-
all_files = [path for path in get_gallery_files() if st.session_state['asset_checkboxes'].get(
|
| 833 |
if all_files:
|
| 834 |
selected_file = st.selectbox("Select Image or PDF", all_files, key="ocr_select")
|
| 835 |
if selected_file:
|
|
@@ -856,7 +857,7 @@ with tab7:
|
|
| 856 |
|
| 857 |
with tab8:
|
| 858 |
st.header("Test Image Gen 🎨")
|
| 859 |
-
all_files = [path for path in get_gallery_files() if st.session_state['asset_checkboxes'].get(
|
| 860 |
if all_files:
|
| 861 |
selected_file = st.selectbox("Select Image or PDF", all_files, key="gen_select")
|
| 862 |
if selected_file:
|
|
@@ -885,7 +886,7 @@ with tab8:
|
|
| 885 |
with tab9:
|
| 886 |
st.header("Custom Diffusion 🎨🤓")
|
| 887 |
st.write("Unleash your inner artist with our tiny diffusion models!")
|
| 888 |
-
all_files = [path for path in get_gallery_files() if st.session_state['asset_checkboxes'].get(
|
| 889 |
if all_files:
|
| 890 |
st.subheader("Select Images or PDFs to Train")
|
| 891 |
selected_files = st.multiselect("Pick Images or PDFs", all_files, key="diffusion_select")
|
|
|
|
| 330 |
return [d for d in glob.glob(path) if os.path.isdir(d)]
|
| 331 |
|
| 332 |
def get_gallery_files(file_types=["png", "pdf"]):
|
| 333 |
+
return sorted(list(set([f for ext in file_types for f in glob.glob(f"*.{ext}")]))) # Deduplicate files
|
| 334 |
|
| 335 |
def get_pdf_files():
|
| 336 |
return sorted(glob.glob("*.pdf"))
|
|
|
|
| 539 |
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 540 |
st.image(img, caption=os.path.basename(file), use_container_width=True)
|
| 541 |
doc.close()
|
| 542 |
+
checkbox_key = f"asset_{file}_{idx}" # Unique key with index
|
| 543 |
+
st.session_state['asset_checkboxes'][file] = st.checkbox(
|
| 544 |
"Use for SFT/Input",
|
| 545 |
+
value=st.session_state['asset_checkboxes'].get(file, False),
|
| 546 |
key=checkbox_key
|
| 547 |
)
|
| 548 |
mime_type = "image/png" if file.endswith('.png') else "application/pdf"
|
| 549 |
st.markdown(get_download_link(file, mime_type, "Snag It! 📥"), unsafe_allow_html=True)
|
| 550 |
+
if st.button("Zap It! 🗑️", key=f"delete_{file}_{idx}"): # Unique key with index
|
| 551 |
os.remove(file)
|
| 552 |
+
if file in st.session_state['asset_checkboxes']:
|
| 553 |
+
del st.session_state['asset_checkboxes'][file]
|
| 554 |
if file.endswith('.pdf'):
|
| 555 |
url_key = next((k for k, v in st.session_state['downloaded_pdfs'].items() if v == file), None)
|
| 556 |
if url_key:
|
|
|
|
| 665 |
|
| 666 |
mode = st.selectbox("Snapshot Mode", ["Single Page (High-Res)", "Two Pages (High-Res)", "All Pages (High-Res)"], key="download_mode")
|
| 667 |
if st.button("Snapshot Selected 📸"):
|
| 668 |
+
selected_pdfs = [path for path in get_gallery_files() if path.endswith('.pdf') and st.session_state['asset_checkboxes'].get(path, False)]
|
| 669 |
if selected_pdfs:
|
| 670 |
for pdf_path in selected_pdfs:
|
| 671 |
mode_key = {"Single Page (High-Res)": "single", "Two Pages (High-Res)": "twopage", "All Pages (High-Res)": "allpages"}[mode]
|
|
|
|
| 673 |
for snapshot in snapshots:
|
| 674 |
st.image(Image.open(snapshot), caption=snapshot, use_container_width=True)
|
| 675 |
else:
|
| 676 |
+
st.warning("No PDFs selected for snapshotting! Check some boxes in the sidebar gallery.")
|
| 677 |
|
| 678 |
with tab3:
|
| 679 |
st.header("Build Titan 🌱")
|
|
|
|
| 733 |
st.markdown(get_download_link(zip_path, "application/zip", "Download Fine-Tuned Titan"), unsafe_allow_html=True)
|
| 734 |
st.rerun()
|
| 735 |
elif isinstance(st.session_state['builder'], DiffusionBuilder):
|
| 736 |
+
selected_files = [path for path in get_gallery_files() if st.session_state['asset_checkboxes'].get(path, False)]
|
| 737 |
if len(selected_files) >= 2:
|
| 738 |
demo_data = [{"image": file, "text": f"Asset {os.path.basename(file).split('.')[0]}"} for file in selected_files]
|
| 739 |
edited_data = st.data_editor(pd.DataFrame(demo_data), num_rows="dynamic")
|
|
|
|
| 830 |
|
| 831 |
with tab7:
|
| 832 |
st.header("Test OCR 🔍")
|
| 833 |
+
all_files = [path for path in get_gallery_files() if st.session_state['asset_checkboxes'].get(path, False)]
|
| 834 |
if all_files:
|
| 835 |
selected_file = st.selectbox("Select Image or PDF", all_files, key="ocr_select")
|
| 836 |
if selected_file:
|
|
|
|
| 857 |
|
| 858 |
with tab8:
|
| 859 |
st.header("Test Image Gen 🎨")
|
| 860 |
+
all_files = [path for path in get_gallery_files() if st.session_state['asset_checkboxes'].get(path, False)]
|
| 861 |
if all_files:
|
| 862 |
selected_file = st.selectbox("Select Image or PDF", all_files, key="gen_select")
|
| 863 |
if selected_file:
|
|
|
|
| 886 |
with tab9:
|
| 887 |
st.header("Custom Diffusion 🎨🤓")
|
| 888 |
st.write("Unleash your inner artist with our tiny diffusion models!")
|
| 889 |
+
all_files = [path for path in get_gallery_files() if st.session_state['asset_checkboxes'].get(path, False)]
|
| 890 |
if all_files:
|
| 891 |
st.subheader("Select Images or PDFs to Train")
|
| 892 |
selected_files = st.multiselect("Pick Images or PDFs", all_files, key="diffusion_select")
|