Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -54,7 +54,7 @@ st.set_page_config(
|
|
| 54 |
|
| 55 |
# Initialize st.session_state
|
| 56 |
if 'captured_files' not in st.session_state:
|
| 57 |
-
st.session_state['captured_files'] = {'cam0': None, 'cam1': None} #
|
| 58 |
if 'builder' not in st.session_state:
|
| 59 |
st.session_state['builder'] = None
|
| 60 |
if 'model_loaded' not in st.session_state:
|
|
@@ -62,7 +62,7 @@ if 'model_loaded' not in st.session_state:
|
|
| 62 |
if 'processing' not in st.session_state:
|
| 63 |
st.session_state['processing'] = {}
|
| 64 |
if 'history' not in st.session_state:
|
| 65 |
-
st.session_state['history'] =
|
| 66 |
|
| 67 |
# Model Configuration Classes
|
| 68 |
@dataclass
|
|
@@ -506,20 +506,15 @@ st.title("AI Vision & SFT Titans 🚀")
|
|
| 506 |
|
| 507 |
# Sidebar
|
| 508 |
st.sidebar.header("Captured Files 📜")
|
| 509 |
-
gallery_size = st.sidebar.slider("Gallery Size", 1, 10,
|
| 510 |
def update_gallery():
|
| 511 |
-
media_files =
|
| 512 |
-
if
|
|
|
|
| 513 |
cols = st.sidebar.columns(2)
|
| 514 |
-
for idx, file in enumerate(
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
if file.endswith(".png"):
|
| 518 |
-
st.image(Image.open(file), caption=os.path.basename(file), use_container_width=True)
|
| 519 |
-
elif file.endswith(".txt"):
|
| 520 |
-
with open(file, "r") as f:
|
| 521 |
-
content = f.read()
|
| 522 |
-
st.text(content[:50] + "..." if len(content) > 50 else content)
|
| 523 |
update_gallery()
|
| 524 |
|
| 525 |
st.sidebar.subheader("Model Management 🗂️")
|
|
@@ -543,7 +538,8 @@ with log_container:
|
|
| 543 |
st.sidebar.subheader("History 📜")
|
| 544 |
history_container = st.sidebar.empty()
|
| 545 |
with history_container:
|
| 546 |
-
|
|
|
|
| 547 |
st.write(entry)
|
| 548 |
|
| 549 |
# Tabs
|
|
@@ -563,9 +559,9 @@ with tab1:
|
|
| 563 |
with open(filename, "wb") as f:
|
| 564 |
f.write(cam0_img.getvalue())
|
| 565 |
st.session_state['captured_files']['cam0'] = filename
|
|
|
|
| 566 |
st.image(Image.open(filename), caption="Camera 0", use_container_width=True)
|
| 567 |
logger.info(f"Saved snapshot from Camera 0: {filename}")
|
| 568 |
-
st.session_state['history'].append(f"Snapshot from Cam 0: {filename}")
|
| 569 |
update_gallery()
|
| 570 |
with cols[1]:
|
| 571 |
cam1_img = st.camera_input("Take a picture - Cam 1", key="cam1")
|
|
@@ -574,9 +570,9 @@ with tab1:
|
|
| 574 |
with open(filename, "wb") as f:
|
| 575 |
f.write(cam1_img.getvalue())
|
| 576 |
st.session_state['captured_files']['cam1'] = filename
|
|
|
|
| 577 |
st.image(Image.open(filename), caption="Camera 1", use_container_width=True)
|
| 578 |
logger.info(f"Saved snapshot from Camera 1: {filename}")
|
| 579 |
-
st.session_state['history'].append(f"Snapshot from Cam 1: {filename}")
|
| 580 |
update_gallery()
|
| 581 |
|
| 582 |
with tab2:
|
|
@@ -590,7 +586,7 @@ with tab2:
|
|
| 590 |
pdf_path = generate_filename("downloaded", "pdf")
|
| 591 |
if download_pdf(url, pdf_path):
|
| 592 |
logger.info(f"Downloaded PDF from {url} to {pdf_path}")
|
| 593 |
-
st.session_state['history']
|
| 594 |
snapshots = asyncio.run(process_pdf_snapshot(pdf_path, mode.lower().replace(" ", "")))
|
| 595 |
for snapshot in snapshots:
|
| 596 |
st.image(Image.open(snapshot), caption=snapshot, use_container_width=True)
|
|
@@ -612,7 +608,7 @@ with tab3:
|
|
| 612 |
builder.save_model(config.model_path)
|
| 613 |
st.session_state['builder'] = builder
|
| 614 |
st.session_state['model_loaded'] = True
|
| 615 |
-
st.session_state['history']
|
| 616 |
st.success(f"Model downloaded and saved to {config.model_path}! 🎉")
|
| 617 |
st.rerun()
|
| 618 |
|
|
@@ -647,7 +643,7 @@ with tab4:
|
|
| 647 |
st.session_state['builder'].save_model(new_config.model_path)
|
| 648 |
zip_path = f"{new_config.model_path}.zip"
|
| 649 |
zip_directory(new_config.model_path, zip_path)
|
| 650 |
-
st.session_state['history']
|
| 651 |
st.markdown(get_download_link(zip_path, "application/zip", "Download Fine-Tuned Titan"), unsafe_allow_html=True)
|
| 652 |
st.rerun()
|
| 653 |
elif isinstance(st.session_state['builder'], DiffusionBuilder):
|
|
@@ -665,7 +661,7 @@ with tab4:
|
|
| 665 |
st.session_state['builder'].save_model(new_config.model_path)
|
| 666 |
zip_path = f"{new_config.model_path}.zip"
|
| 667 |
zip_directory(new_config.model_path, zip_path)
|
| 668 |
-
st.session_state['history']
|
| 669 |
st.markdown(get_download_link(zip_path, "application/zip", "Download Fine-Tuned Diffusion Model"), unsafe_allow_html=True)
|
| 670 |
csv_path = f"sft_dataset_{int(time.time())}.csv"
|
| 671 |
with open(csv_path, "w", newline="") as f:
|
|
@@ -697,7 +693,7 @@ with tab5:
|
|
| 697 |
if st.button("Run Test ▶️"):
|
| 698 |
status_container = st.empty()
|
| 699 |
result = st.session_state['builder'].evaluate(test_prompt, status_container)
|
| 700 |
-
st.session_state['history']
|
| 701 |
st.write(f"**Generated Response**: {result}")
|
| 702 |
status_container.empty()
|
| 703 |
elif isinstance(st.session_state['builder'], DiffusionBuilder):
|
|
@@ -707,7 +703,7 @@ with tab5:
|
|
| 707 |
output_file = generate_filename("diffusion_test", "png")
|
| 708 |
image.save(output_file)
|
| 709 |
st.session_state['captured_files']['diffusion_test'] = output_file
|
| 710 |
-
st.session_state['history']
|
| 711 |
st.image(image, caption="Generated Image")
|
| 712 |
update_gallery()
|
| 713 |
|
|
@@ -721,14 +717,14 @@ with tab6:
|
|
| 721 |
agent = PartyPlannerAgent(st.session_state['builder'].model, st.session_state['builder'].tokenizer)
|
| 722 |
task = "Plan a luxury superhero-themed party at Wayne Manor."
|
| 723 |
plan_df = agent.plan_party(task)
|
| 724 |
-
st.session_state['history']
|
| 725 |
st.dataframe(plan_df)
|
| 726 |
elif isinstance(st.session_state['builder'], DiffusionBuilder):
|
| 727 |
if st.button("Run CV RAG Demo 🎉"):
|
| 728 |
agent = CVPartyPlannerAgent(st.session_state['builder'].pipeline)
|
| 729 |
task = "Generate images for a luxury superhero-themed party."
|
| 730 |
plan_df = agent.plan_party(task)
|
| 731 |
-
st.session_state['history']
|
| 732 |
st.dataframe(plan_df)
|
| 733 |
for _, row in plan_df.iterrows():
|
| 734 |
image = agent.generate(row["Image Idea"])
|
|
@@ -750,7 +746,7 @@ with tab7:
|
|
| 750 |
output_file = generate_filename("ocr_output", "txt")
|
| 751 |
st.session_state['processing']['ocr'] = True
|
| 752 |
result = asyncio.run(process_ocr(image, output_file))
|
| 753 |
-
st.session_state['history']
|
| 754 |
st.text_area("OCR Result", result, height=200, key="ocr_result")
|
| 755 |
st.success(f"OCR output saved to {output_file}")
|
| 756 |
st.session_state['processing']['ocr'] = False
|
|
@@ -770,7 +766,7 @@ with tab8:
|
|
| 770 |
output_file = generate_filename("gen_output", "png")
|
| 771 |
st.session_state['processing']['gen'] = True
|
| 772 |
result = asyncio.run(process_image_gen(prompt, output_file))
|
| 773 |
-
st.session_state['history']
|
| 774 |
st.image(result, caption="Generated Image", use_container_width=True)
|
| 775 |
st.success(f"Image saved to {output_file}")
|
| 776 |
st.session_state['processing']['gen'] = False
|
|
@@ -805,7 +801,7 @@ with tab9:
|
|
| 805 |
result = builder.generate("A superhero scene inspired by captured images")
|
| 806 |
result.save(output_file)
|
| 807 |
st.session_state['captured_files']['diffusion'] = output_file
|
| 808 |
-
st.session_state['history']
|
| 809 |
st.image(result, caption=f"{model_choice} Masterpiece", use_container_width=True)
|
| 810 |
st.success(f"Image saved to {output_file}")
|
| 811 |
st.session_state['processing']['diffusion'] = False
|
|
|
|
| 54 |
|
| 55 |
# Initialize st.session_state
|
| 56 |
if 'captured_files' not in st.session_state:
|
| 57 |
+
st.session_state['captured_files'] = {'cam0': None, 'cam1': None} # One file per camera
|
| 58 |
if 'builder' not in st.session_state:
|
| 59 |
st.session_state['builder'] = None
|
| 60 |
if 'model_loaded' not in st.session_state:
|
|
|
|
| 62 |
if 'processing' not in st.session_state:
|
| 63 |
st.session_state['processing'] = {}
|
| 64 |
if 'history' not in st.session_state:
|
| 65 |
+
st.session_state['history'] = {'cam0': None, 'cam1': None} # One history entry per camera
|
| 66 |
|
| 67 |
# Model Configuration Classes
|
| 68 |
@dataclass
|
|
|
|
| 506 |
|
| 507 |
# Sidebar
|
| 508 |
st.sidebar.header("Captured Files 📜")
|
| 509 |
+
gallery_size = st.sidebar.slider("Gallery Size", 1, 10, 2) # Default to 2 for two cameras
|
| 510 |
def update_gallery():
|
| 511 |
+
media_files = [st.session_state['captured_files']['cam0'], st.session_state['captured_files']['cam1']]
|
| 512 |
+
valid_files = [f for f in media_files if f and os.path.exists(f)] # Only valid files
|
| 513 |
+
if valid_files:
|
| 514 |
cols = st.sidebar.columns(2)
|
| 515 |
+
for idx, file in enumerate(valid_files[:2]): # Limit to 2 images
|
| 516 |
+
with cols[idx]:
|
| 517 |
+
st.image(Image.open(file), caption=os.path.basename(file), use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 518 |
update_gallery()
|
| 519 |
|
| 520 |
st.sidebar.subheader("Model Management 🗂️")
|
|
|
|
| 538 |
st.sidebar.subheader("History 📜")
|
| 539 |
history_container = st.sidebar.empty()
|
| 540 |
with history_container:
|
| 541 |
+
valid_history = [st.session_state['history']['cam0'], st.session_state['history']['cam1']]
|
| 542 |
+
for entry in [e for e in valid_history if e][:2]: # Show only latest two
|
| 543 |
st.write(entry)
|
| 544 |
|
| 545 |
# Tabs
|
|
|
|
| 559 |
with open(filename, "wb") as f:
|
| 560 |
f.write(cam0_img.getvalue())
|
| 561 |
st.session_state['captured_files']['cam0'] = filename
|
| 562 |
+
st.session_state['history']['cam0'] = f"Snapshot from Cam 0: {filename}"
|
| 563 |
st.image(Image.open(filename), caption="Camera 0", use_container_width=True)
|
| 564 |
logger.info(f"Saved snapshot from Camera 0: {filename}")
|
|
|
|
| 565 |
update_gallery()
|
| 566 |
with cols[1]:
|
| 567 |
cam1_img = st.camera_input("Take a picture - Cam 1", key="cam1")
|
|
|
|
| 570 |
with open(filename, "wb") as f:
|
| 571 |
f.write(cam1_img.getvalue())
|
| 572 |
st.session_state['captured_files']['cam1'] = filename
|
| 573 |
+
st.session_state['history']['cam1'] = f"Snapshot from Cam 1: {filename}"
|
| 574 |
st.image(Image.open(filename), caption="Camera 1", use_container_width=True)
|
| 575 |
logger.info(f"Saved snapshot from Camera 1: {filename}")
|
|
|
|
| 576 |
update_gallery()
|
| 577 |
|
| 578 |
with tab2:
|
|
|
|
| 586 |
pdf_path = generate_filename("downloaded", "pdf")
|
| 587 |
if download_pdf(url, pdf_path):
|
| 588 |
logger.info(f"Downloaded PDF from {url} to {pdf_path}")
|
| 589 |
+
st.session_state['history']['pdf'] = f"Downloaded PDF: {pdf_path}"
|
| 590 |
snapshots = asyncio.run(process_pdf_snapshot(pdf_path, mode.lower().replace(" ", "")))
|
| 591 |
for snapshot in snapshots:
|
| 592 |
st.image(Image.open(snapshot), caption=snapshot, use_container_width=True)
|
|
|
|
| 608 |
builder.save_model(config.model_path)
|
| 609 |
st.session_state['builder'] = builder
|
| 610 |
st.session_state['model_loaded'] = True
|
| 611 |
+
st.session_state['history']['build'] = f"Built {model_type} model: {model_name}"
|
| 612 |
st.success(f"Model downloaded and saved to {config.model_path}! 🎉")
|
| 613 |
st.rerun()
|
| 614 |
|
|
|
|
| 643 |
st.session_state['builder'].save_model(new_config.model_path)
|
| 644 |
zip_path = f"{new_config.model_path}.zip"
|
| 645 |
zip_directory(new_config.model_path, zip_path)
|
| 646 |
+
st.session_state['history']['sft'] = f"Fine-tuned Causal LM: {new_model_name}"
|
| 647 |
st.markdown(get_download_link(zip_path, "application/zip", "Download Fine-Tuned Titan"), unsafe_allow_html=True)
|
| 648 |
st.rerun()
|
| 649 |
elif isinstance(st.session_state['builder'], DiffusionBuilder):
|
|
|
|
| 661 |
st.session_state['builder'].save_model(new_config.model_path)
|
| 662 |
zip_path = f"{new_config.model_path}.zip"
|
| 663 |
zip_directory(new_config.model_path, zip_path)
|
| 664 |
+
st.session_state['history']['sft'] = f"Fine-tuned Diffusion: {new_model_name}"
|
| 665 |
st.markdown(get_download_link(zip_path, "application/zip", "Download Fine-Tuned Diffusion Model"), unsafe_allow_html=True)
|
| 666 |
csv_path = f"sft_dataset_{int(time.time())}.csv"
|
| 667 |
with open(csv_path, "w", newline="") as f:
|
|
|
|
| 693 |
if st.button("Run Test ▶️"):
|
| 694 |
status_container = st.empty()
|
| 695 |
result = st.session_state['builder'].evaluate(test_prompt, status_container)
|
| 696 |
+
st.session_state['history']['test'] = f"Causal LM Test: {test_prompt} -> {result}"
|
| 697 |
st.write(f"**Generated Response**: {result}")
|
| 698 |
status_container.empty()
|
| 699 |
elif isinstance(st.session_state['builder'], DiffusionBuilder):
|
|
|
|
| 703 |
output_file = generate_filename("diffusion_test", "png")
|
| 704 |
image.save(output_file)
|
| 705 |
st.session_state['captured_files']['diffusion_test'] = output_file
|
| 706 |
+
st.session_state['history']['test'] = f"Diffusion Test: {test_prompt} -> {output_file}"
|
| 707 |
st.image(image, caption="Generated Image")
|
| 708 |
update_gallery()
|
| 709 |
|
|
|
|
| 717 |
agent = PartyPlannerAgent(st.session_state['builder'].model, st.session_state['builder'].tokenizer)
|
| 718 |
task = "Plan a luxury superhero-themed party at Wayne Manor."
|
| 719 |
plan_df = agent.plan_party(task)
|
| 720 |
+
st.session_state['history']['rag'] = f"NLP RAG Demo: Planned party at Wayne Manor"
|
| 721 |
st.dataframe(plan_df)
|
| 722 |
elif isinstance(st.session_state['builder'], DiffusionBuilder):
|
| 723 |
if st.button("Run CV RAG Demo 🎉"):
|
| 724 |
agent = CVPartyPlannerAgent(st.session_state['builder'].pipeline)
|
| 725 |
task = "Generate images for a luxury superhero-themed party."
|
| 726 |
plan_df = agent.plan_party(task)
|
| 727 |
+
st.session_state['history']['rag'] = f"CV RAG Demo: Generated party images"
|
| 728 |
st.dataframe(plan_df)
|
| 729 |
for _, row in plan_df.iterrows():
|
| 730 |
image = agent.generate(row["Image Idea"])
|
|
|
|
| 746 |
output_file = generate_filename("ocr_output", "txt")
|
| 747 |
st.session_state['processing']['ocr'] = True
|
| 748 |
result = asyncio.run(process_ocr(image, output_file))
|
| 749 |
+
st.session_state['history']['ocr'] = f"OCR Test: {selected_file} -> {output_file}"
|
| 750 |
st.text_area("OCR Result", result, height=200, key="ocr_result")
|
| 751 |
st.success(f"OCR output saved to {output_file}")
|
| 752 |
st.session_state['processing']['ocr'] = False
|
|
|
|
| 766 |
output_file = generate_filename("gen_output", "png")
|
| 767 |
st.session_state['processing']['gen'] = True
|
| 768 |
result = asyncio.run(process_image_gen(prompt, output_file))
|
| 769 |
+
st.session_state['history']['gen'] = f"Image Gen Test: {prompt} -> {output_file}"
|
| 770 |
st.image(result, caption="Generated Image", use_container_width=True)
|
| 771 |
st.success(f"Image saved to {output_file}")
|
| 772 |
st.session_state['processing']['gen'] = False
|
|
|
|
| 801 |
result = builder.generate("A superhero scene inspired by captured images")
|
| 802 |
result.save(output_file)
|
| 803 |
st.session_state['captured_files']['diffusion'] = output_file
|
| 804 |
+
st.session_state['history']['diffusion'] = f"Custom Diffusion: {model_choice} -> {output_file}"
|
| 805 |
st.image(result, caption=f"{model_choice} Masterpiece", use_container_width=True)
|
| 806 |
st.success(f"Image saved to {output_file}")
|
| 807 |
st.session_state['processing']['diffusion'] = False
|