File size: 7,515 Bytes
3368275 868c32d 3368275 868c32d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
import streamlit as st
import json
from huggingface_hub import InferenceClient
import time
st.set_page_config(page_title="AI Video Ad Generator", page_icon="π¬", layout="wide")
# Initialize session state
if 'api_token' not in st.session_state:
st.session_state.api_token = ""
# Sidebar - API Token
with st.sidebar:
st.header("π Configuration")
api_token = st.text_input(
"HuggingFace API Token",
type="password",
value=st.session_state.api_token,
help="Get your token from https://huggingface.co/settings/tokens"
)
if api_token:
st.session_state.api_token = api_token
st.divider()
st.markdown("### π JSON Structure")
st.code("""{
"product": "laptop",
"style": "cinematic",
"mood": "premium",
"duration": "short",
"camera": "rotating",
"lighting": "studio"
}""", language="json")
# Main title
st.title("π¬ AI Video Advertisement Generator")
st.markdown("Generate professional video ads from JSON specifications using state-of-the-art AI")
# Tabs
tab1, tab2 = st.tabs(["π₯ Generate Video", "π Guide"])
with tab1:
col1, col2 = st.columns([1, 1])
with col1:
st.subheader("Input Configuration")
# JSON Input
json_input = st.text_area(
"Ad Specification (JSON)",
value="""{
"product": "premium laptop",
"brand_style": "modern tech",
"visual_style": "cinematic commercial",
"camera_movement": "smooth 360 rotation",
"lighting": "dramatic studio backlight",
"background": "gradient dark to light",
"mood": "premium luxury",
"key_features": ["ultra-thin", "metallic finish", "glowing edges"],
"duration": "5 seconds"
}""",
height=300
)
# Advanced settings
with st.expander("βοΈ Advanced Settings"):
model_choice = st.selectbox(
"Model",
["tencent/HunyuanVideo", "THUDM/CogVideoX-5b", "genmo/mochi-1-preview"],
help="HunyuanVideo: Best quality | CogVideoX: Longer videos | Mochi: Fastest"
)
resolution = st.selectbox("Resolution", ["720p", "1080p"], index=0)
fps = st.slider("FPS", 24, 30, 24)
# Generate button
generate_btn = st.button("π¬ Generate Video Ad", type="primary", use_container_width=True)
with col2:
st.subheader("Generated Video")
video_placeholder = st.empty()
status_placeholder = st.empty()
# Generation logic
if generate_btn:
if not st.session_state.api_token:
st.error("β οΈ Please enter your HuggingFace API token in the sidebar")
else:
try:
# Parse JSON
ad_config = json.loads(json_input)
# Build cinematic prompt from JSON
prompt = f"""Professional commercial advertisement video showcasing {ad_config.get('product', 'product')},
{ad_config.get('visual_style', 'cinematic')} style, {ad_config.get('camera_movement', 'smooth camera movement')},
{ad_config.get('lighting', 'professional lighting')}, {ad_config.get('background', 'modern background')},
{ad_config.get('mood', 'premium')} aesthetic, product-focused hero shot,
{', '.join(ad_config.get('key_features', []))}, commercial quality, 4K resolution,
professional advertising photography, luxury brand style, high-end production value"""
status_placeholder.info(f"π¨ Generating with {model_choice}...")
# Initialize client
client = InferenceClient(token=st.session_state.api_token)
# Progress simulation
progress_bar = st.progress(0)
for i in range(100):
time.sleep(0.3)
progress_bar.progress(i + 1)
# Generate video
with st.spinner("π¬ Creating your video ad... (this may take 30-60 seconds)"):
video_bytes = client.text_to_video(
prompt=prompt,
model=model_choice
)
progress_bar.empty()
status_placeholder.success("β
Video generated successfully!")
# Display video
with col2:
video_placeholder.video(video_bytes)
st.download_button(
label="β¬οΈ Download Video",
data=video_bytes,
file_name=f"ad_{ad_config.get('product', 'video').replace(' ', '_')}.mp4",
mime="video/mp4",
use_container_width=True
)
# Show generated prompt
with st.expander("π Generated Prompt"):
st.text(prompt)
except json.JSONDecodeError:
st.error("β Invalid JSON format. Please check your input.")
except Exception as e:
st.error(f"β Error: {str(e)}")
status_placeholder.empty()
with tab2:
st.markdown("""
## π― How to Use
### 1οΈβ£ Get API Token
- Visit [HuggingFace Tokens](https://huggingface.co/settings/tokens)
- Create a new token with "read" permissions
- Paste it in the sidebar
### 2οΈβ£ Configure Your Ad
Define your video ad using JSON with these properties:
- **product**: What you're advertising
- **brand_style**: Your brand aesthetic (modern, minimal, bold)
- **visual_style**: Video style (cinematic, dynamic, elegant)
- **camera_movement**: How camera moves (rotation, zoom, pan)
- **lighting**: Lighting setup (dramatic, soft, studio)
- **background**: Background style (gradient, solid, abstract)
- **mood**: Overall feeling (premium, energetic, calm)
- **key_features**: List of product highlights
- **duration**: Video length preference
### 3οΈβ£ Generate
Click "Generate Video Ad" and wait 30-60 seconds
### π¨ Model Comparison
- **HunyuanVideo**: Best quality, photorealistic, 5-6 seconds
- **CogVideoX**: Good quality, longer duration, 10+ seconds
- **Mochi**: Fastest generation, 3-5 seconds, lightweight
### π‘ Tips for Best Results
- Be specific about visual style and camera movement
- Include 3-5 key features maximum
- Use cinematic/commercial terminology
- Describe lighting and mood clearly
- Keep product name concise
### β‘ Example JSON Templates
**Tech Product:**
```json
{
"product": "smartphone",
"visual_style": "Apple-style commercial",
"camera_movement": "slow orbit around device",
"lighting": "soft gradient backlight",
"mood": "minimalist premium"
}
```
**Fashion/Lifestyle:**
```json
{
"product": "luxury watch",
"visual_style": "high-fashion editorial",
"camera_movement": "close-up macro details",
"lighting": "dramatic side lighting",
"mood": "elegant timeless"
}
```
""")
# Footer
st.divider()
st.markdown("""
<div style='text-align: center; color: #666;'>
<p>Powered by HuggingFace Inference API | Free GPU-accelerated video generation</p>
</div>
""", unsafe_allow_html=True) |