remade-effects / app.py
alex-remade's picture
Much better styling for page along with discord nudge
722a50c
raw
history blame
19.8 kB
import os
import requests
import gradio as gr
import uuid
import datetime
from supabase import create_client, Client
from supabase.lib.client_options import ClientOptions
import dotenv
from google.cloud import storage
import json
from pathlib import Path
import mimetypes
from workflow_handler import WanVideoWorkflow
from video_config import MODEL_FRAME_RATES, calculate_frames
import asyncio
dotenv.load_dotenv()
SCRIPT_DIR = Path(__file__).parent
CONFIG_PATH = SCRIPT_DIR / "config.json"
WORKFLOW_PATH = SCRIPT_DIR / "wani2v.json"
loras = [
{
#I suggest it to be a gif instead of an mp4!
"image": "https://huggingface.co/Remade-AI/Squish/resolve/main/example_videos/tank_squish.mp4",
#This is an id you can send to your backend, obviously you can change it
"id": "06ce6840-f976-4963-9644-b6cf7f323f90",
#This is the title that is shown on the front end
"title": "Squish",
"example_prompt": "In the video, a miniature rodent is presented. The rodent is held in a person's hands. The person then presses on the rodent, causing a sq41sh squish effect. The person keeps pressing down on the rodent, further showing the sq41sh squish effect.",
},
{
"image": "https://huggingface.co/Remade-AI/Rotate/resolve/main/example_videos/man_rotate.mp4",
"id": "4ac08cfa-841e-4aa9-9022-c3fc80fb6ef4",
"title": "Rotate",
"example_prompt": "The video shows an elderly Asian man's head and shoulders with blurred background, performing a r0t4tion 360 degrees rotation.",
},
{
"image": "https://huggingface.co/Remade-AI/Cakeify/resolve/main/example_videos/timberland_cakeify.mp4",
"id": "b05c1dc7-a71c-4d24-b512-4877a12dea7e",
"title": "Cakeify",
"example_prompt": "The video opens on a woman. A knife, held by a hand, is coming into frame and hovering over the woman. The knife then begins cutting into the woman to c4k3 cakeify it. As the knife slices the woman open, the inside of the woman is revealed to be cake with chocolate layers. The knife cuts through and the contents of the woman are revealed."
},
]
# Initialize Supabase client with async support
supabase: Client = create_client(
os.getenv('SUPABASE_URL'),
os.getenv('SUPABASE_KEY'),
)
def initialize_gcs():
"""Initialize Google Cloud Storage client with credentials from environment"""
try:
# Parse service account JSON from environment variable
service_account_json = os.getenv('SERVICE_ACCOUNT_JSON')
if not service_account_json:
raise ValueError("SERVICE_ACCOUNT_JSON environment variable not found")
credentials_info = json.loads(service_account_json)
# Initialize storage client
storage_client = storage.Client.from_service_account_info(credentials_info)
print("Successfully initialized Google Cloud Storage client")
return storage_client
except Exception as e:
print(f"Error initializing Google Cloud Storage: {e}")
raise
def upload_to_gcs(file_path, content_type=None, folder='user_uploads'):
"""
Uploads a file to Google Cloud Storage
Args:
file_path: Path to the file to upload
content_type: MIME type of the file (optional)
folder: Folder path in bucket (default: 'user_uploads')
Returns:
str: Public URL of the uploaded file
"""
try:
bucket_name = 'remade-v2'
storage_client = initialize_gcs()
bucket = storage_client.bucket(bucket_name)
# Get file extension and generate unique filename
file_extension = Path(file_path).suffix
if not content_type:
content_type = mimetypes.guess_type(file_path)[0] or 'application/octet-stream'
# Validate file type
valid_types = ['image/jpeg', 'image/png', 'image/gif']
if content_type not in valid_types:
raise ValueError("Invalid file type. Please upload a JPG, PNG or GIF image.")
# Generate unique filename with proper path structure
filename = f"{str(uuid.uuid4())}{file_extension}"
file_path_in_gcs = f"{folder}/{filename}"
# Create blob and set metadata
blob = bucket.blob(file_path_in_gcs)
blob.content_type = content_type
blob.cache_control = 'public, max-age=31536000'
print(f'Uploading file to GCS: {file_path_in_gcs}')
# Upload the file
blob.upload_from_filename(
file_path,
timeout=120 # 2 minute timeout
)
# Generate public URL with correct path format
image_url = f"https://storage.googleapis.com/{bucket_name}/{file_path_in_gcs}"
print(f"Successfully uploaded to GCS: {image_url}")
return image_url
except Exception as e:
print(f"Error uploading to GCS: {e}")
raise ValueError(f"Failed to upload image to storage: {str(e)}")
def build_lora_prompt(subject, lora_id):
"""
Builds a standardized prompt based on the selected LoRA and subject
"""
# Get LoRA config
lora_config = next((lora for lora in loras if lora["id"] == lora_id), None)
if not lora_config:
raise ValueError(f"Invalid LoRA ID: {lora_id}")
if lora_id == "06ce6840-f976-4963-9644-b6cf7f323f90": # Squish
return (
f"In the video, a miniature {subject} is presented. "
f"The {subject} is held in a person's hands. "
f"The person then presses on the {subject}, causing a sq41sh squish effect. "
f"The person keeps pressing down on the {subject}, further showing the sq41sh squish effect."
)
elif lora_id == "4ac08cfa-841e-4aa9-9022-c3fc80fb6ef4": # Rotate
return (
f"The video shows a {subject} performing a r0t4tion 360 degrees rotation."
)
elif lora_id == "b05c1dc7-a71c-4d24-b512-4877a12dea7e": # Cakeify
return (
f"The video opens on a {subject}. A knife, held by a hand, is coming into frame "
f"and hovering over the {subject}. The knife then begins cutting into the {subject} "
f"to c4k3 cakeify it. As the knife slices the {subject} open, the inside of the "
f"{subject} is revealed to be cake with chocolate layers. The knife cuts through "
f"and the contents of the {subject} are revealed."
)
else:
raise ValueError(f"Unknown LoRA ID: {lora_id}")
def poll_generation_status(generation_id):
"""Poll generation status from database"""
try:
# Query the database for the current status
response = supabase.table('generations') \
.select('*') \
.eq('generation_id', generation_id) \
.execute()
if not response.data:
return None
return response.data[0]
except Exception as e:
print(f"Error polling generation status: {e}")
raise e
async def generate_video(input_image, subject, duration, selected_index, progress=gr.Progress()):
try:
# Initialize workflow handler with explicit paths
workflow_handler = WanVideoWorkflow(
supabase,
config_path=str(CONFIG_PATH),
workflow_path=str(WORKFLOW_PATH)
)
# Upload image to GCS and get public URL
image_url = upload_to_gcs(input_image)
# Map duration selection to actual seconds
duration_mapping = {
"Short (3s)": 3,
"Long (5s)": 5
}
video_duration = duration_mapping[duration]
# Get LoRA config
lora_config = next((lora for lora in loras if lora["id"] == selected_index), None)
if not lora_config:
raise ValueError(f"Invalid LoRA ID: {selected_index}")
# Generate unique ID
generation_id = str(uuid.uuid4())
# Update workflow
prompt = build_lora_prompt(subject, selected_index)
workflow_handler.update_prompt(prompt)
workflow_handler.update_input_image(image_url)
await workflow_handler.update_lora(lora_config)
workflow_handler.update_length(video_duration)
workflow_handler.update_output_name(generation_id)
# Get final workflow
workflow = workflow_handler.get_workflow()
# Store generation data in Supabase
generation_data = {
"generation_id": generation_id,
"user_id": "anonymous",
"status": "queued",
"progress": 0,
"worker_id": None,
"created_at": datetime.datetime.utcnow().isoformat(),
"message": {
"generationId": generation_id,
"workflow": {
"prompt": workflow
}
},
"metadata": {
"prompt": {
"original": subject,
"enhanced": subject
},
"lora": {
"id": selected_index,
"strength": 1.0,
"name": lora_config["title"]
},
"workflow": "img2vid",
"dimensions": None,
"input_image_url": image_url,
"video_length": {"duration": video_duration},
},
"error": None,
"output_url": None,
"batch_id": None,
"platform": "huggingface"
}
# Remove await - the execute() method returns the response directly
response = supabase.table('generations').insert(generation_data).execute()
print(f"Stored generation data with ID: {generation_id}")
# Return generation ID for tracking
return generation_id
except Exception as e:
print(f"Error in generate_video: {e}")
raise e
def update_selection(evt: gr.SelectData):
selected_lora = loras[evt.index]
sentence = f"Selected LoRA: {selected_lora['title']}"
return selected_lora['id'], sentence
async def handle_generation(input_image, subject, duration, selected_index, progress=gr.Progress(track_tqdm=True)):
try:
if selected_index is None:
raise gr.Error("You must select a LoRA before proceeding.")
# Generate the video and get generation ID
generation_id = await generate_video(input_image, subject, duration, selected_index)
# Poll for status updates
while True:
generation = poll_generation_status(generation_id)
if not generation:
raise ValueError(f"Generation {generation_id} not found")
# Update progress
if 'progress' in generation:
progress_value = generation['progress']
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {progress_value}; --total: 100;"><span class="progress-text">Processing: {progress_value}%</span></div></div><div class="refresh-warning">Please do not refresh this page while processing</div>'
# Check status
if generation['status'] == 'completed':
# Final yield with completed video
yield generation['output_url'], generation_id, gr.update(visible=False)
break # Exit the loop
elif generation['status'] == 'error':
raise ValueError(f"Generation failed: {generation.get('error')}")
else:
# Yield progress update
yield None, generation_id, gr.update(value=progress_bar, visible=True)
# Wait before next poll
await asyncio.sleep(2)
except Exception as e:
print(f"Error in handle_generation: {e}")
raise e
css = '''
#gen_btn{height: 100%}
#gen_column{align-self: stretch}
#title{text-align: center}
#title h1{font-size: 3em; display:inline-flex; align-items:center}
#title img{width: 100px; margin-right: 0.5em}
#gallery .grid-wrap{height: auto; min-height: 350px}
#gallery .gallery-item {height: 100%; width: 100%; object-fit: cover}
#lora_list{background: var(--block-background-fill);padding: 0 1em .3em; font-size: 90%}
.card_internal{display: flex;height: 100px;margin-top: .5em}
.card_internal img{margin-right: 1em}
.styler{--form-gap-width: 0px !important}
#progress{height:30px}
#progress .generating{display:none}
.progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px;position: relative;}
.progress-bar {height: 100%;background-color: #90EE90;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
.progress-text {position: absolute;width: 100%;text-align: center;top: 50%;left: 0;transform: translateY(-50%);color: #000;font-weight: bold;}
.refresh-warning {color: #e74c3c;font-weight: bold;text-align: center;margin-top: 5px;}
/* Improved Discord styling */
.discord-banner {
background: linear-gradient(135deg, #5865F2 0%, #404EED 100%);
color: white;
padding: 20px;
border-radius: 12px;
margin: 15px 0;
text-align: center;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.discord-banner h3 {
margin-top: 0;
font-size: 1.5em;
text-shadow: 0 1px 2px rgba(0,0,0,0.2);
}
.discord-banner a {
display: inline-block;
background-color: white;
color: #5865F2;
text-decoration: none;
font-weight: bold;
padding: 8px 16px;
border-radius: 20px;
margin-top: 10px;
transition: all 0.2s ease;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
.discord-banner a:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
.discord-feature {
background-color: #F6F6F6;
border-left: 4px solid #5865F2;
padding: 12px 15px;
margin: 10px 0;
border-radius: 0 8px 8px 0;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
.discord-feature-title {
font-weight: bold;
color: #5865F2;
}
.discord-locked {
opacity: 0.7;
position: relative;
pointer-events: none;
}
.discord-locked::after {
content: "🔒 Discord members only";
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(88,101,242,0.9);
color: white;
padding: 5px 10px;
border-radius: 20px;
white-space: nowrap;
font-size: 0.9em;
font-weight: bold;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
.discord-benefits-list {
text-align: left;
display: inline-block;
margin: 10px 0;
}
.discord-benefits-list li {
margin: 8px 0;
position: relative;
padding-left: 25px;
}
.discord-benefits-list li::before {
content: "✨";
position: absolute;
left: 0;
color: #FFD700;
}
.locked-option {
opacity: 0.6;
cursor: not-allowed;
}
'''
with gr.Blocks(css=css) as demo:
selected_index = gr.State(None)
current_generation_id = gr.State(None)
gr.Markdown("# Remade AI - Wan 2.1 I2V effects LoRAs ")
# Discord banner at the top
discord_banner = gr.HTML(
"""<div class="discord-banner">
<h3>✨ Unlock Premium Features! ✨</h3>
<p>Join our Discord community to access longer videos, 100+ LoRAs, audio features, and faster generation times!</p>
<a href="https://discord.gg/remade-1" target="_blank">Join Discord Now</a>
</div>"""
)
selected_info = gr.HTML("")
with gr.Row():
with gr.Column(scale=1):
gallery = gr.Gallery(
[(item["image"], item["title"]) for item in loras],
label="Select LoRA",
allow_preview=False,
columns=4,
elem_id="gallery",
show_share_button=False,
height="350px",
object_fit="contain"
)
# Discord feature callout for LoRAs
gr.HTML(
"""<div class="discord-feature">
<span class="discord-feature-title">✨ Discord Members:</span> Access 100+ additional LoRAs beyond these 3 samples!
</div>"""
)
input_image = gr.Image(type="filepath")
subject = gr.Textbox(label="Describe your subject", placeholder="Cat toy")
# Modified duration options - only one active option
duration = gr.Radio(
["Short (3s)"],
label="Duration",
value="Short (3s)"
)
# Add disabled duration option with Discord callout
gr.HTML(
"""<div class="discord-feature">
<span class="discord-feature-title">⏱️ Discord Members:</span> Access longer video durations (up to 10 seconds)!
</div>"""
)
# Add disabled audio button with Discord callout
with gr.Row():
button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
audio_button = gr.Button("Add Audio 🔒", interactive=False)
# Discord feature callout for generation speed
gr.HTML(
"""<div class="discord-feature">
<span class="discord-feature-title">⚡ Discord Members:</span> Enjoy priority queue with faster generation times!
</div>"""
)
with gr.Column(scale=1):
progress_bar = gr.Markdown(elem_id="progress", visible=False)
output = gr.Video(interactive=False, label="Output video")
# Discord feature summary at the bottom
gr.HTML(
"""<div class="discord-banner" style="margin-top: 20px;">
<h3>Join our Discord to unlock:</h3>
<ul class="discord-benefits-list">
<li>Longer video durations (up to 10 seconds)</li>
<li>100+ creative LoRAs to choose from</li>
<li>Audio generation features</li>
<li>Priority queue with faster generation</li>
<li>Community support and inspiration</li>
</ul>
<a href="https://discord.gg/remade" target="_blank">Join Discord Now</a>
</div>"""
)
gallery.select(
update_selection,
outputs=[selected_index, selected_info]
)
# Add a custom handler to check if inputs are valid
def check_inputs(subject, input_image, selected_index):
if not selected_index:
return gr.Error("You must select a LoRA before proceeding.")
if not subject.strip():
return gr.Error("Please describe your subject.")
if input_image is None:
return gr.Error("Please upload an image.")
return None
# Use gr.on for the button click with validation
button.click(
fn=check_inputs,
inputs=[subject, input_image, selected_index],
outputs=None
).success(
fn=handle_generation,
inputs=[input_image, subject, duration, selected_index],
outputs=[output, current_generation_id, progress_bar]
)
# Add a click handler for the disabled audio button
audio_button.click(
fn=lambda: gr.Info("Join our Discord to unlock audio generation features!"),
inputs=None,
outputs=None
)
if __name__ == "__main__":
demo.queue()
demo.launch()