Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import requests
|
|
| 3 |
from PIL import Image
|
| 4 |
import os
|
| 5 |
import io
|
|
|
|
| 6 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 7 |
import time
|
| 8 |
from gradio_client import Client
|
|
@@ -30,8 +31,25 @@ model = BlipForConditionalGeneration.from_pretrained(blipper)
|
|
| 30 |
chatbot_client = Client(chatter)
|
| 31 |
|
| 32 |
PROFILES_FILENAME = "character_profiles.json"
|
| 33 |
-
REPO_ID = "K00B404/
|
| 34 |
REPO_TYPE = "space"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def load_profiles():
|
| 37 |
"""Load profiles from HuggingFace space"""
|
|
@@ -155,7 +173,7 @@ def generate_persona(img, min_len, max_len, persona_detail_level):
|
|
| 155 |
# dramaturg to mae a solid role for a actor from pragmatic description
|
| 156 |
system_prompt="You are a Expert Dramaturg and your task is to use the input persona information and write a 'Role' description as compact instuctions for the actor"
|
| 157 |
persona = helper_llm(persona, system_prompt=system_prompt)
|
| 158 |
-
return caption, persona, total_time
|
| 159 |
|
| 160 |
|
| 161 |
def chat_with_persona(message, history, system_message, max_tokens, temperature, top_p):
|
|
@@ -211,11 +229,28 @@ def generate_flux_image(final_prompt, is_negative, steps, cfg_scale, seed, stren
|
|
| 211 |
print(f"Error when trying to open the image: {e}")
|
| 212 |
return None, None, None
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
# Create Gradio interface with tabs
|
| 215 |
with gr.Blocks(title="Image Character Persona Generator") as iface:
|
| 216 |
-
# Store
|
| 217 |
persona_state = gr.State("")
|
| 218 |
profiles_state = gr.State(load_profiles())
|
|
|
|
| 219 |
|
| 220 |
with gr.Tabs():
|
| 221 |
# First tab: Persona Generator
|
|
@@ -321,23 +356,59 @@ with gr.Blocks(title="Image Character Persona Generator") as iface:
|
|
| 321 |
|
| 322 |
with gr.Row():
|
| 323 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
# Profile Management Section
|
|
|
|
| 325 |
profile_name = gr.Textbox(label="Profile Name", placeholder="Enter a name for this character profile")
|
| 326 |
add_profile_btn = gr.Button("Save Current Profile")
|
| 327 |
status_msg = gr.Textbox(label="Status", interactive=False)
|
| 328 |
|
| 329 |
# Profile Selection Dropdown
|
| 330 |
-
profile_dropdown = gr.Dropdown(label="Select Profile", interactive=True)
|
| 331 |
load_profile_btn = gr.Button("Load Selected Profile")
|
| 332 |
update_profile_btn = gr.Button("Update Selected Profile")
|
| 333 |
delete_profile_btn = gr.Button("Delete Selected Profile")
|
| 334 |
refresh_profiles_btn = gr.Button("Refresh Profiles List")
|
| 335 |
|
| 336 |
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
| 337 |
# Profile Editor
|
| 338 |
profile_caption = gr.Textbox(label="Character Description", lines=3)
|
| 339 |
profile_persona = gr.Textbox(label="Character Persona", lines=15)
|
| 340 |
image_path_display = gr.Textbox(label="Image Path (Reference Only)", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
# Function to refresh the profiles dropdown
|
| 343 |
def refresh_profiles_list(profiles_data):
|
|
@@ -345,7 +416,7 @@ with gr.Blocks(title="Image Character Persona Generator") as iface:
|
|
| 345 |
return gr.Dropdown(choices=[p["name"] for p in profiles], value=None if not profiles else profiles[0]["name"])
|
| 346 |
|
| 347 |
# Function to save current profile
|
| 348 |
-
def save_current_profile(name, caption, persona,
|
| 349 |
if not name or not caption or not persona:
|
| 350 |
return profiles_data, "Error: Please provide a name, caption, and persona", gr.Dropdown(choices=[p["name"] for p in profiles_data["profiles"]])
|
| 351 |
|
|
@@ -354,7 +425,7 @@ with gr.Blocks(title="Image Character Persona Generator") as iface:
|
|
| 354 |
"name": name,
|
| 355 |
"caption": caption,
|
| 356 |
"persona": persona,
|
| 357 |
-
"image_path":
|
| 358 |
"created_at": datetime.now().isoformat()
|
| 359 |
}
|
| 360 |
|
|
@@ -378,12 +449,13 @@ with gr.Blocks(title="Image Character Persona Generator") as iface:
|
|
| 378 |
def load_selected_profile(profile_name, profiles_data):
|
| 379 |
for profile in profiles_data["profiles"]:
|
| 380 |
if profile["name"] == profile_name:
|
| 381 |
-
|
|
|
|
| 382 |
|
| 383 |
-
return "", "", "", "Error: Profile not found", ""
|
| 384 |
|
| 385 |
# Function to update selected profile
|
| 386 |
-
def update_selected_profile(profile_name, caption, persona, profiles_data):
|
| 387 |
if not profile_name:
|
| 388 |
return profiles_data, "Error: No profile selected"
|
| 389 |
|
|
@@ -392,6 +464,8 @@ with gr.Blocks(title="Image Character Persona Generator") as iface:
|
|
| 392 |
# Update profile data
|
| 393 |
profiles_data["profiles"][i]["caption"] = caption
|
| 394 |
profiles_data["profiles"][i]["persona"] = persona
|
|
|
|
|
|
|
| 395 |
profiles_data["profiles"][i]["updated_at"] = datetime.now().isoformat()
|
| 396 |
|
| 397 |
# Save updated profiles
|
|
@@ -415,22 +489,43 @@ with gr.Blocks(title="Image Character Persona Generator") as iface:
|
|
| 415 |
profile_names = [p["name"] for p in profiles_data["profiles"]]
|
| 416 |
return profiles_data, f"Deleted profile: {profile_name}", gr.Dropdown(choices=profile_names, value=None if not profile_names else profile_names[0])
|
| 417 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
# Connect the UI elements with their functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
add_profile_btn.click(
|
| 420 |
fn=save_current_profile,
|
| 421 |
-
inputs=[profile_name, profile_caption, profile_persona,
|
| 422 |
outputs=[profiles_state, status_msg, profile_dropdown]
|
| 423 |
)
|
| 424 |
|
| 425 |
load_profile_btn.click(
|
| 426 |
fn=load_selected_profile,
|
| 427 |
inputs=[profile_dropdown, profiles_state],
|
| 428 |
-
outputs=[profile_caption, profile_persona, image_path_display, status_msg, system_prompt]
|
| 429 |
)
|
| 430 |
|
| 431 |
update_profile_btn.click(
|
| 432 |
fn=update_selected_profile,
|
| 433 |
-
inputs=[profile_dropdown, profile_caption, profile_persona, profiles_state],
|
| 434 |
outputs=[profiles_state, status_msg]
|
| 435 |
)
|
| 436 |
|
|
@@ -446,35 +541,41 @@ with gr.Blocks(title="Image Character Persona Generator") as iface:
|
|
| 446 |
outputs=[profile_dropdown]
|
| 447 |
)
|
| 448 |
|
| 449 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
iface.load(
|
| 451 |
-
fn=refresh_profiles_list,
|
| 452 |
inputs=[profiles_state],
|
| 453 |
-
outputs=[profile_dropdown]
|
| 454 |
)
|
| 455 |
|
| 456 |
# Function to update system prompt in Test tab when persona is generated
|
| 457 |
-
def update_persona_state(caption, persona, time_output):
|
| 458 |
return persona, persona
|
| 459 |
|
| 460 |
# Connect the persona generator to update the system prompt
|
| 461 |
submit_btn.click(fn=generate_persona,
|
| 462 |
inputs=[input_image, min_length, max_length, detail_level],
|
| 463 |
-
outputs=[caption_output, persona_output, time_output])
|
| 464 |
|
| 465 |
# Update the system prompt in Test tab when persona is generated
|
| 466 |
submit_btn.click(fn=update_persona_state,
|
| 467 |
-
inputs=[caption_output, persona_output, time_output],
|
| 468 |
outputs=[persona_state, system_prompt])
|
| 469 |
|
| 470 |
# Function to update profile fields when a new persona is generated
|
| 471 |
-
def update_profile_fields(caption, persona):
|
| 472 |
-
return caption, persona
|
| 473 |
|
| 474 |
# Update the profile fields in Persona Manager tab when persona is generated
|
| 475 |
submit_btn.click(fn=update_profile_fields,
|
| 476 |
-
inputs=[caption_output, persona_output],
|
| 477 |
-
outputs=[profile_caption, profile_persona])
|
| 478 |
|
| 479 |
# Launch the interface
|
| 480 |
iface.launch()
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import os
|
| 5 |
import io
|
| 6 |
+
import glob
|
| 7 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 8 |
import time
|
| 9 |
from gradio_client import Client
|
|
|
|
| 31 |
chatbot_client = Client(chatter)
|
| 32 |
|
| 33 |
PROFILES_FILENAME = "character_profiles.json"
|
| 34 |
+
REPO_ID = "K00B404/Persona_from_Image"
|
| 35 |
REPO_TYPE = "space"
|
| 36 |
+
CHARACTERS_FOLDER = "characters" # Root folder for character images
|
| 37 |
+
|
| 38 |
+
# Ensure characters folder exists
|
| 39 |
+
if not os.path.exists(CHARACTERS_FOLDER):
|
| 40 |
+
os.makedirs(CHARACTERS_FOLDER)
|
| 41 |
+
|
| 42 |
+
def get_character_images():
|
| 43 |
+
"""Get all image files from the characters folder"""
|
| 44 |
+
image_files = []
|
| 45 |
+
# Get all PNG, JPG, JPEG, and WebP files
|
| 46 |
+
for ext in ['png', 'jpg', 'jpeg', 'webp']:
|
| 47 |
+
image_files.extend(glob.glob(f"{CHARACTERS_FOLDER}/*.{ext}"))
|
| 48 |
+
image_files.extend(glob.glob(f"{CHARACTERS_FOLDER}/*.{ext.upper()}"))
|
| 49 |
+
|
| 50 |
+
# Sort alphabetically
|
| 51 |
+
image_files.sort()
|
| 52 |
+
return image_files
|
| 53 |
|
| 54 |
def load_profiles():
|
| 55 |
"""Load profiles from HuggingFace space"""
|
|
|
|
| 173 |
# dramaturg to mae a solid role for a actor from pragmatic description
|
| 174 |
system_prompt="You are a Expert Dramaturg and your task is to use the input persona information and write a 'Role' description as compact instuctions for the actor"
|
| 175 |
persona = helper_llm(persona, system_prompt=system_prompt)
|
| 176 |
+
return caption, persona, total_time, img
|
| 177 |
|
| 178 |
|
| 179 |
def chat_with_persona(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
|
| 229 |
print(f"Error when trying to open the image: {e}")
|
| 230 |
return None, None, None
|
| 231 |
|
| 232 |
+
def process_selected_image(image_path):
|
| 233 |
+
"""Process a selected image from the gallery to generate a persona"""
|
| 234 |
+
if not image_path:
|
| 235 |
+
return "", "", "No image selected", None
|
| 236 |
+
|
| 237 |
+
try:
|
| 238 |
+
# Use default values for generation
|
| 239 |
+
min_len = 50
|
| 240 |
+
max_len = 200
|
| 241 |
+
persona_detail_level = "Comprehensive"
|
| 242 |
+
|
| 243 |
+
caption, persona, time_output, _ = generate_persona(image_path, min_len, max_len, persona_detail_level)
|
| 244 |
+
return caption, persona, time_output, image_path
|
| 245 |
+
except Exception as e:
|
| 246 |
+
return "", "", f"Error processing image: {str(e)}", None
|
| 247 |
+
|
| 248 |
# Create Gradio interface with tabs
|
| 249 |
with gr.Blocks(title="Image Character Persona Generator") as iface:
|
| 250 |
+
# Store state variables
|
| 251 |
persona_state = gr.State("")
|
| 252 |
profiles_state = gr.State(load_profiles())
|
| 253 |
+
selected_image_state = gr.State(None)
|
| 254 |
|
| 255 |
with gr.Tabs():
|
| 256 |
# First tab: Persona Generator
|
|
|
|
| 356 |
|
| 357 |
with gr.Row():
|
| 358 |
with gr.Column(scale=1):
|
| 359 |
+
# Character Gallery
|
| 360 |
+
gr.Markdown("### Character Images")
|
| 361 |
+
|
| 362 |
+
# Auto-load images from characters folder
|
| 363 |
+
character_gallery = gr.Gallery(
|
| 364 |
+
label="Available Character Images",
|
| 365 |
+
elem_id="character_gallery",
|
| 366 |
+
columns=3,
|
| 367 |
+
object_fit="contain",
|
| 368 |
+
height="auto"
|
| 369 |
+
)
|
| 370 |
+
|
| 371 |
+
refresh_gallery_btn = gr.Button("Refresh Character Gallery")
|
| 372 |
+
|
| 373 |
# Profile Management Section
|
| 374 |
+
gr.Markdown("### Profile Management")
|
| 375 |
profile_name = gr.Textbox(label="Profile Name", placeholder="Enter a name for this character profile")
|
| 376 |
add_profile_btn = gr.Button("Save Current Profile")
|
| 377 |
status_msg = gr.Textbox(label="Status", interactive=False)
|
| 378 |
|
| 379 |
# Profile Selection Dropdown
|
| 380 |
+
profile_dropdown = gr.Dropdown(label="Select Saved Profile", interactive=True)
|
| 381 |
load_profile_btn = gr.Button("Load Selected Profile")
|
| 382 |
update_profile_btn = gr.Button("Update Selected Profile")
|
| 383 |
delete_profile_btn = gr.Button("Delete Selected Profile")
|
| 384 |
refresh_profiles_btn = gr.Button("Refresh Profiles List")
|
| 385 |
|
| 386 |
with gr.Column(scale=2):
|
| 387 |
+
# Profile Preview
|
| 388 |
+
preview_image = gr.Image(label="Character Image Preview", type="filepath")
|
| 389 |
+
|
| 390 |
# Profile Editor
|
| 391 |
profile_caption = gr.Textbox(label="Character Description", lines=3)
|
| 392 |
profile_persona = gr.Textbox(label="Character Persona", lines=15)
|
| 393 |
image_path_display = gr.Textbox(label="Image Path (Reference Only)", interactive=False)
|
| 394 |
+
|
| 395 |
+
# Process Selected Image Button
|
| 396 |
+
process_image_btn = gr.Button("Process Selected Image")
|
| 397 |
+
|
| 398 |
+
# Copy to Chat Button
|
| 399 |
+
copy_to_chat_btn = gr.Button("Use This Persona in Chat")
|
| 400 |
+
|
| 401 |
+
# Function to refresh the character gallery
|
| 402 |
+
def refresh_character_gallery():
|
| 403 |
+
image_files = get_character_images()
|
| 404 |
+
return gr.Gallery(value=image_files)
|
| 405 |
+
|
| 406 |
+
# Function to handle gallery selection
|
| 407 |
+
def gallery_select(evt: gr.SelectData, gallery_images):
|
| 408 |
+
if gallery_images and evt.index < len(gallery_images):
|
| 409 |
+
selected_image = gallery_images[evt.index]
|
| 410 |
+
return selected_image, selected_image
|
| 411 |
+
return None, None
|
| 412 |
|
| 413 |
# Function to refresh the profiles dropdown
|
| 414 |
def refresh_profiles_list(profiles_data):
|
|
|
|
| 416 |
return gr.Dropdown(choices=[p["name"] for p in profiles], value=None if not profiles else profiles[0]["name"])
|
| 417 |
|
| 418 |
# Function to save current profile
|
| 419 |
+
def save_current_profile(name, caption, persona, image_path, profiles_data):
|
| 420 |
if not name or not caption or not persona:
|
| 421 |
return profiles_data, "Error: Please provide a name, caption, and persona", gr.Dropdown(choices=[p["name"] for p in profiles_data["profiles"]])
|
| 422 |
|
|
|
|
| 425 |
"name": name,
|
| 426 |
"caption": caption,
|
| 427 |
"persona": persona,
|
| 428 |
+
"image_path": image_path if image_path else "",
|
| 429 |
"created_at": datetime.now().isoformat()
|
| 430 |
}
|
| 431 |
|
|
|
|
| 449 |
def load_selected_profile(profile_name, profiles_data):
|
| 450 |
for profile in profiles_data["profiles"]:
|
| 451 |
if profile["name"] == profile_name:
|
| 452 |
+
image_path = profile["image_path"] if os.path.exists(profile["image_path"]) else None
|
| 453 |
+
return profile["caption"], profile["persona"], profile["image_path"], image_path, "Profile loaded successfully", profile["persona"]
|
| 454 |
|
| 455 |
+
return "", "", "", None, "Error: Profile not found", ""
|
| 456 |
|
| 457 |
# Function to update selected profile
|
| 458 |
+
def update_selected_profile(profile_name, caption, persona, image_path, profiles_data):
|
| 459 |
if not profile_name:
|
| 460 |
return profiles_data, "Error: No profile selected"
|
| 461 |
|
|
|
|
| 464 |
# Update profile data
|
| 465 |
profiles_data["profiles"][i]["caption"] = caption
|
| 466 |
profiles_data["profiles"][i]["persona"] = persona
|
| 467 |
+
if image_path and image_path != profile["image_path"]:
|
| 468 |
+
profiles_data["profiles"][i]["image_path"] = image_path
|
| 469 |
profiles_data["profiles"][i]["updated_at"] = datetime.now().isoformat()
|
| 470 |
|
| 471 |
# Save updated profiles
|
|
|
|
| 489 |
profile_names = [p["name"] for p in profiles_data["profiles"]]
|
| 490 |
return profiles_data, f"Deleted profile: {profile_name}", gr.Dropdown(choices=profile_names, value=None if not profile_names else profile_names[0])
|
| 491 |
|
| 492 |
+
# Function to use the current persona in chat
|
| 493 |
+
def use_persona_in_chat(persona):
|
| 494 |
+
return persona
|
| 495 |
+
|
| 496 |
# Connect the UI elements with their functions
|
| 497 |
+
refresh_gallery_btn.click(
|
| 498 |
+
fn=refresh_character_gallery,
|
| 499 |
+
outputs=[character_gallery]
|
| 500 |
+
)
|
| 501 |
+
|
| 502 |
+
character_gallery.select(
|
| 503 |
+
fn=gallery_select,
|
| 504 |
+
inputs=[character_gallery],
|
| 505 |
+
outputs=[selected_image_state, preview_image]
|
| 506 |
+
)
|
| 507 |
+
|
| 508 |
+
process_image_btn.click(
|
| 509 |
+
fn=process_selected_image,
|
| 510 |
+
inputs=[selected_image_state],
|
| 511 |
+
outputs=[profile_caption, profile_persona, status_msg, image_path_display]
|
| 512 |
+
)
|
| 513 |
+
|
| 514 |
add_profile_btn.click(
|
| 515 |
fn=save_current_profile,
|
| 516 |
+
inputs=[profile_name, profile_caption, profile_persona, selected_image_state, profiles_state],
|
| 517 |
outputs=[profiles_state, status_msg, profile_dropdown]
|
| 518 |
)
|
| 519 |
|
| 520 |
load_profile_btn.click(
|
| 521 |
fn=load_selected_profile,
|
| 522 |
inputs=[profile_dropdown, profiles_state],
|
| 523 |
+
outputs=[profile_caption, profile_persona, image_path_display, preview_image, status_msg, system_prompt]
|
| 524 |
)
|
| 525 |
|
| 526 |
update_profile_btn.click(
|
| 527 |
fn=update_selected_profile,
|
| 528 |
+
inputs=[profile_dropdown, profile_caption, profile_persona, selected_image_state, profiles_state],
|
| 529 |
outputs=[profiles_state, status_msg]
|
| 530 |
)
|
| 531 |
|
|
|
|
| 541 |
outputs=[profile_dropdown]
|
| 542 |
)
|
| 543 |
|
| 544 |
+
copy_to_chat_btn.click(
|
| 545 |
+
fn=use_persona_in_chat,
|
| 546 |
+
inputs=[profile_persona],
|
| 547 |
+
outputs=[system_prompt]
|
| 548 |
+
)
|
| 549 |
+
|
| 550 |
+
# Initialize the profiles dropdown and character gallery
|
| 551 |
iface.load(
|
| 552 |
+
fn=lambda: (refresh_profiles_list, refresh_character_gallery),
|
| 553 |
inputs=[profiles_state],
|
| 554 |
+
outputs=[profile_dropdown, character_gallery]
|
| 555 |
)
|
| 556 |
|
| 557 |
# Function to update system prompt in Test tab when persona is generated
|
| 558 |
+
def update_persona_state(caption, persona, time_output, img_path):
|
| 559 |
return persona, persona
|
| 560 |
|
| 561 |
# Connect the persona generator to update the system prompt
|
| 562 |
submit_btn.click(fn=generate_persona,
|
| 563 |
inputs=[input_image, min_length, max_length, detail_level],
|
| 564 |
+
outputs=[caption_output, persona_output, time_output, selected_image_state])
|
| 565 |
|
| 566 |
# Update the system prompt in Test tab when persona is generated
|
| 567 |
submit_btn.click(fn=update_persona_state,
|
| 568 |
+
inputs=[caption_output, persona_output, time_output, input_image],
|
| 569 |
outputs=[persona_state, system_prompt])
|
| 570 |
|
| 571 |
# Function to update profile fields when a new persona is generated
|
| 572 |
+
def update_profile_fields(caption, persona, img_path):
|
| 573 |
+
return caption, persona, img_path, img_path
|
| 574 |
|
| 575 |
# Update the profile fields in Persona Manager tab when persona is generated
|
| 576 |
submit_btn.click(fn=update_profile_fields,
|
| 577 |
+
inputs=[caption_output, persona_output, input_image],
|
| 578 |
+
outputs=[profile_caption, profile_persona, selected_image_state, preview_image])
|
| 579 |
|
| 580 |
# Launch the interface
|
| 581 |
iface.launch()
|