Fecolywill's picture
Update app.py
f257ee8 verified
#@title 🎬 AI Film Studio Pro (Advanced Watermark + Analytics)
import gradio as gr
import numpy as np
import random
from diffusers import DiffusionPipeline
from PIL import Image, ImageDraw, ImageFont
import base64
# ===== CONFIG =====
SUPPORTERS = random.randint(50, 200)
KO_FI_LINK = "https://ko-fi.com/fecolywill"
GA_TRACKING_ID = "G-XXXXXXXXXX" # Replace with your Google Analytics ID
# In your Watermarker class:
class Watermarker:
def __init__(self):
try:
# Try loading default font first
self.font = ImageFont.load_default().font_variant(size=24)
except:
# Fallback to basic font
self.font = ImageFont.load_default()
self.color = (255, 255, 255, 128)
self.position = "bottom-right"
self.text = "Made with AI Film Studio"
def apply(self, frames):
watermarked_frames = []
for frame in frames:
img = Image.fromarray(frame)
draw = ImageDraw.Draw(img, "RGBA")
if self.position == "bottom-right":
x = img.width - draw.textlength(self.text, self.font) - 20
y = img.height - 30
elif self.position == "top-left":
x, y = 20, 20
else: # center
x = (img.width - draw.textlength(self.text, self.font)) // 2
y = (img.height - 30) // 2
draw.text((x, y), self.text, fill=self.color, font=self.font)
if self.logo:
logo_img = Image.open(self.logo).resize((50, 50))
img.paste(logo_img, (x-60, y-5), logo_img)
watermarked_frames.append(np.array(img))
return np.stack(watermarked_frames)
watermarker = Watermarker()
# ===== VIDEO GENERATION =====
def generate_video(prompt):
pipe = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_576w")
frames = pipe(prompt, num_frames=24).frames
return watermarker.apply(frames), 12 # (watermarked_frames, fps)
# ===== ENHANCED ANALYTICS =====
analytics_js = f"""
<script async src="https://www.googletagmanager.com/gtag/js?id={GA_TRACKING_ID}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){{dataLayer.push(arguments);}}
gtag('js', new Date());
// Enhanced tracking
gtag('config', '{GA_TRACKING_ID}', {{
'page_title': 'AI Film Studio',
'page_location': location.href
}});
function trackAction(type, label) {{
gtag('event', 'user_action', {{
'event_category': type,
'event_label': label,
'value': 1
}});
}}
</script>
"""
# ===== SUPPORT SECTION =====
support_html = f"""
<div style="border-top:1px solid #e6e6e6; padding:25px; text-align:center; margin-top:30px; background:#f9f9f9; border-radius:8px;">
<h3 style="margin-bottom:10px;">❀️ Support This Project</h3>
<p style="margin-bottom:15px;">Enjoying this free tool? Help me add more features!</p>
<a href="{KO_FI_LINK}" target="_blank" onclick="trackAction('support', 'kofi_click')" style="padding:10px 20px; background:#29abe0; color:white; border-radius:5px; text-decoration:none; font-weight:bold; display:inline-block; margin-bottom:15px;">
β˜• Buy Me a Coffee
</a>
<p style="color:#666; font-size:0.9em;">πŸŽ‰ {SUPPORTERS}+ creators have supported!</p>
</div>
"""
# ===== MAIN APP =====
with gr.Blocks(theme=gr.themes.Soft(), title="AI Film Studio Pro") as app:
# Google Analytics
gr.HTML(analytics_js)
# Header
gr.Markdown("# 🎬 AI Film Studio Pro")
gr.Markdown("Generate **watermarked videos** with professional tracking!")
# Video Generation UI
with gr.Tab("πŸŽ₯ Video Creator"):
with gr.Row():
prompt = gr.Textbox(label="Describe Your Scene",
placeholder="A spaceship lands in medieval times...")
style = gr.Dropdown(["Cinematic", "Anime", "Cyberpunk"], label="Style")
generate_btn = gr.Button("Generate Video", variant="primary")
# Video with enhanced download tracking
video = gr.Video(
label="Your Video",
show_download_button=True,
interactive=False
)
# Download tracking with quality selection
download_js = """
<script>
document.querySelector('button[aria-label="Download"]').addEventListener('click', () => {
trackAction('download', 'video_mp4');
alert('Video downloaded! Consider supporting to unlock HD versions πŸš€');
});
</script>
"""
gr.HTML(download_js)
# Watermark Customization (Advanced)
with gr.Accordion("βš™οΈ Watermark Settings", open=False):
watermark_text = gr.Textbox(label="Watermark Text", value="Made with AI Film Studio")
watermark_color = gr.ColorPicker(label="Color", value="#ffffff")
watermark_opacity = gr.Slider(0, 100, value=50, label="Opacity (%)")
watermark_position = gr.Dropdown(["bottom-right", "top-left", "center"], label="Position")
def update_watermark(text, color, opacity, position):
r, g, b = tuple(int(color.lstrip('#')[i:i+2], 16) for i in (0, 2, 4))
watermarker.text = text
watermarker.color = (r, g, b, int(255 * (opacity/100)))
watermarker.position = position
return "Watermark settings updated!"
update_btn = gr.Button("Update Watermark")
update_btn.click(
fn=update_watermark,
inputs=[watermark_text, watermark_color, watermark_opacity, watermark_position],
outputs=gr.Textbox(visible=False)
)
# Support Footer
gr.HTML(support_html)
# Generation function
generate_btn.click(
fn=generate_video,
inputs=[prompt],
outputs=video
).then(
lambda: trackAction("generation", "video_created"), # Track successful generations
outputs=None
)
# ===== DEPLOYMENT =====
if __name__ == "__main__":
app.launch(debug=True)