Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -5,9 +5,10 @@ from transformers import LlavaForConditionalGeneration, AutoProcessor
|
|
| 5 |
from PIL import Image
|
| 6 |
import gc
|
| 7 |
import time
|
| 8 |
-
import
|
| 9 |
import os
|
| 10 |
import shutil
|
|
|
|
| 11 |
from pathlib import Path
|
| 12 |
|
| 13 |
# Storage optimization - redirect cache to temporary directories
|
|
@@ -323,6 +324,56 @@ def answer_question(image, question):
|
|
| 323 |
|
| 324 |
return result if result else "β No answer generated"
|
| 325 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
# JavaScript for export functionality
|
| 327 |
EXPORT_JS = """
|
| 328 |
<script>
|
|
@@ -496,9 +547,6 @@ EXPORT_JS = """
|
|
| 496 |
with gr.Blocks(title="Sequential Three-Tone JoyCaption", theme=gr.themes.Soft()) as demo:
|
| 497 |
gr.HTML(TITLE)
|
| 498 |
|
| 499 |
-
# JavaScript injection via HTML component
|
| 500 |
-
gr.HTML(EXPORT_JS)
|
| 501 |
-
|
| 502 |
with gr.Row():
|
| 503 |
# Left column - Image and controls
|
| 504 |
with gr.Column(scale=1):
|
|
@@ -646,7 +694,28 @@ with gr.Blocks(title="Sequential Three-Tone JoyCaption", theme=gr.themes.Soft())
|
|
| 646 |
5. Generate one at a time - no simultaneous processing
|
| 647 |
6. Copy the result and enhance it with the Text Enhancer space
|
| 648 |
7. Use π reload to regenerate that specific tone
|
|
|
|
| 649 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
|
| 651 |
# Individual generate button handlers
|
| 652 |
generate_engaging_btn.click(
|
|
@@ -747,6 +816,40 @@ with gr.Blocks(title="Sequential Three-Tone JoyCaption", theme=gr.themes.Soft())
|
|
| 747 |
outputs=uncensored_output
|
| 748 |
)
|
| 749 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 750 |
# Body Parts Focus clear button handler removed
|
| 751 |
|
| 752 |
if __name__ == "__main__":
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import gc
|
| 7 |
import time
|
| 8 |
+
import gc
|
| 9 |
import os
|
| 10 |
import shutil
|
| 11 |
+
import json
|
| 12 |
from pathlib import Path
|
| 13 |
|
| 14 |
# Storage optimization - redirect cache to temporary directories
|
|
|
|
| 324 |
|
| 325 |
return result if result else "β No answer generated"
|
| 326 |
|
| 327 |
+
def export_joycaption_data(keywords, custom_instructions, question, engaging_caption, casual_caption, keywords_caption, qa_answer):
|
| 328 |
+
"""Export all JoyCaption data as downloadable JSON"""
|
| 329 |
+
try:
|
| 330 |
+
# Collect all the data
|
| 331 |
+
data = {
|
| 332 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
| 333 |
+
"source": "JoyCaption",
|
| 334 |
+
"data": {}
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
# Add input fields
|
| 338 |
+
if keywords and keywords.strip():
|
| 339 |
+
data["data"]["keywords"] = keywords.strip()
|
| 340 |
+
|
| 341 |
+
if custom_instructions and custom_instructions.strip():
|
| 342 |
+
data["data"]["custom_instructions"] = custom_instructions.strip()
|
| 343 |
+
|
| 344 |
+
if question and question.strip():
|
| 345 |
+
data["data"]["question"] = question.strip()
|
| 346 |
+
|
| 347 |
+
# Add generated captions
|
| 348 |
+
if engaging_caption and engaging_caption.strip():
|
| 349 |
+
data["data"]["caption_engaging"] = engaging_caption.strip()
|
| 350 |
+
|
| 351 |
+
if casual_caption and casual_caption.strip():
|
| 352 |
+
data["data"]["caption_casual_friend"] = casual_caption.strip()
|
| 353 |
+
|
| 354 |
+
if keywords_caption and keywords_caption.strip():
|
| 355 |
+
data["data"]["caption_keywords"] = keywords_caption.strip()
|
| 356 |
+
|
| 357 |
+
if qa_answer and qa_answer.strip():
|
| 358 |
+
data["data"]["qa_answer"] = qa_answer.strip()
|
| 359 |
+
|
| 360 |
+
# Check if we have any data to export
|
| 361 |
+
if not data["data"]:
|
| 362 |
+
return "β No data to export. Generate some captions first!", None
|
| 363 |
+
|
| 364 |
+
# Create JSON string
|
| 365 |
+
json_string = json.dumps(data, indent=2, ensure_ascii=False)
|
| 366 |
+
|
| 367 |
+
# Create filename with timestamp
|
| 368 |
+
filename = f"joycaption_data_{time.strftime('%Y%m%d_%H%M%S')}.json"
|
| 369 |
+
|
| 370 |
+
# Return success message and file data
|
| 371 |
+
fields_count = len(data["data"])
|
| 372 |
+
return f"β
Exported {fields_count} fields: {', '.join(data['data'].keys())}", (json_string, filename)
|
| 373 |
+
|
| 374 |
+
except Exception as e:
|
| 375 |
+
return f"β Export failed: {str(e)}", None
|
| 376 |
+
|
| 377 |
# JavaScript for export functionality
|
| 378 |
EXPORT_JS = """
|
| 379 |
<script>
|
|
|
|
| 547 |
with gr.Blocks(title="Sequential Three-Tone JoyCaption", theme=gr.themes.Soft()) as demo:
|
| 548 |
gr.HTML(TITLE)
|
| 549 |
|
|
|
|
|
|
|
|
|
|
| 550 |
with gr.Row():
|
| 551 |
# Left column - Image and controls
|
| 552 |
with gr.Column(scale=1):
|
|
|
|
| 694 |
5. Generate one at a time - no simultaneous processing
|
| 695 |
6. Copy the result and enhance it with the Text Enhancer space
|
| 696 |
7. Use π reload to regenerate that specific tone
|
| 697 |
+
8. **π₯ Export data** to use with Venice Edition Enhancer
|
| 698 |
""")
|
| 699 |
+
|
| 700 |
+
# Export functionality
|
| 701 |
+
with gr.Row():
|
| 702 |
+
export_btn = gr.Button(
|
| 703 |
+
"π₯ Export All Data (JSON)",
|
| 704 |
+
variant="primary",
|
| 705 |
+
size="lg"
|
| 706 |
+
)
|
| 707 |
+
|
| 708 |
+
export_output = gr.Textbox(
|
| 709 |
+
label="Export Status",
|
| 710 |
+
lines=2,
|
| 711 |
+
interactive=False,
|
| 712 |
+
visible=False
|
| 713 |
+
)
|
| 714 |
+
|
| 715 |
+
export_file = gr.File(
|
| 716 |
+
label="Download JSON",
|
| 717 |
+
visible=False
|
| 718 |
+
)
|
| 719 |
|
| 720 |
# Individual generate button handlers
|
| 721 |
generate_engaging_btn.click(
|
|
|
|
| 816 |
outputs=uncensored_output
|
| 817 |
)
|
| 818 |
|
| 819 |
+
# Export functionality
|
| 820 |
+
def handle_export():
|
| 821 |
+
"""Handle the export button click"""
|
| 822 |
+
# Get current values from all fields
|
| 823 |
+
return export_joycaption_data(
|
| 824 |
+
keywords_input.value or "",
|
| 825 |
+
custom_instruction_input.value or "",
|
| 826 |
+
question_input.value or "",
|
| 827 |
+
engaging_output.value or "",
|
| 828 |
+
friend_output.value or "",
|
| 829 |
+
uncensored_output.value or "",
|
| 830 |
+
qa_output.value or ""
|
| 831 |
+
)
|
| 832 |
+
|
| 833 |
+
export_btn.click(
|
| 834 |
+
export_joycaption_data,
|
| 835 |
+
inputs=[
|
| 836 |
+
keywords_input,
|
| 837 |
+
custom_instruction_input,
|
| 838 |
+
question_input,
|
| 839 |
+
engaging_output,
|
| 840 |
+
friend_output,
|
| 841 |
+
uncensored_output,
|
| 842 |
+
qa_output
|
| 843 |
+
],
|
| 844 |
+
outputs=[export_output, export_file]
|
| 845 |
+
).then(
|
| 846 |
+
lambda: gr.update(visible=True),
|
| 847 |
+
outputs=[export_output]
|
| 848 |
+
).then(
|
| 849 |
+
lambda: gr.update(visible=True),
|
| 850 |
+
outputs=[export_file]
|
| 851 |
+
)
|
| 852 |
+
|
| 853 |
# Body Parts Focus clear button handler removed
|
| 854 |
|
| 855 |
if __name__ == "__main__":
|