Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 9,533 Bytes
ea02521 596ce81 273b01b 2a06b1f 0bc7df2 273b01b 2a06b1f 273b01b 80db606 dce996d 80db606 273b01b 2a06b1f 6a7b482 940de5b 6a7b482 940de5b 6a7b482 940de5b 6a7b482 273b01b 6a7b482 dce996d 596ce81 dce996d 596ce81 dce996d 273b01b dce996d 6257581 dce996d 273b01b 34035f1 273b01b 596ce81 273b01b 596ce81 23adf11 596ce81 23adf11 596ce81 6dd2909 6c6d37f 596ce81 2a06b1f 6323c73 2eee636 1d1bb6e ab5da9e d8c258c 9d7c660 b3e0e63 21b61fa 2eee636 596ce81 6323c73 d8c258c 2263afc 0bc7df2 2a06b1f 0bc7df2 8fbaa9e 0bc7df2 b5b30fd 0238b02 ad08ca8 d74e2ab 16f2c1e 21b61fa 1bb0117 0415fd2 ac1a0c2 0922281 596ce81 d74e2ab 596ce81 0922281 d74e2ab 0bc7df2 9b29685 dce996d 0238b02 dce996d 9b29685 596ce81 d74e2ab 596ce81 23adf11 596ce81 9b29685 596ce81 0bc7df2 6a7b482 0bc7df2 6a7b482 2263afc 470324c 2263afc 6a7b482 0238b02 6a7b482 0bc7df2 2a06b1f 97dc2fb bb1f9a6 |
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
import gradio as gr
from gradio_client import Client, handle_file
from google import genai
from google.genai import types
import os
from typing import Optional, List
from huggingface_hub import whoami
from PIL import Image
from io import BytesIO
import tempfile
# --- Google Gemini API Configuration ---
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
if not GOOGLE_API_KEY:
raise ValueError("GOOGLE_API_KEY environment variable not set.")
client = genai.Client(
api_key=os.environ.get("GOOGLE_API_KEY"),
)
GEMINI_MODEL_NAME = 'gemini-2.5-flash-image-preview'
def verify_pro_status(token: Optional[gr.OAuthToken]) -> bool:
"""Verifies if the user is a Hugging Face PRO user or part of an enterprise org."""
if not token:
return False
try:
user_info = whoami(token=token.token)
if user_info.get("isPro", False):
return True
orgs = user_info.get("orgs", [])
if any(org.get("isEnterprise", False) for org in orgs):
return True
return False
except Exception as e:
print(f"Could not verify user's PRO/Enterprise status: {e}")
return False
def _extract_image_data_from_response(response) -> Optional[bytes]:
"""Helper to extract image data from the model's response."""
if hasattr(response, 'candidates') and response.candidates:
for candidate in response.candidates:
if hasattr(candidate, 'content') and hasattr(candidate.content, 'parts') and candidate.content.parts:
for part in candidate.content.parts:
if hasattr(part, 'inline_data') and hasattr(part.inline_data, 'data'):
return part.inline_data.data
return None
def unified_image_generator(
prompt: str,
images: Optional[List[str]] = None,
oauth_token: Optional[gr.OAuthToken] = None
) -> tuple:
"""
Handles all image generation tasks based on the number of input images.
Returns: (output_image_path, video_button_visible, video_output_visible)
"""
if not verify_pro_status(oauth_token):
raise gr.Error("Access Denied. This service is for PRO users only.")
try:
# Dynamically build the 'contents' list for the API
contents = []
if images:
# If there are images, open them and add to contents
for image_path in images:
print(image_path)
contents.append(Image.open(image_path[0]))
# Always add the prompt to the contents
contents.append(prompt)
response = client.models.generate_content(
model=GEMINI_MODEL_NAME,
contents=contents,
)
image_data = _extract_image_data_from_response(response)
if not image_data:
raise ValueError("No image data found in the model response.")
# Save the generated image to a temporary file to return its path
pil_image = Image.open(BytesIO(image_data))
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmpfile:
pil_image.save(tmpfile.name)
output_path = tmpfile.name
# Determine if video button should be shown (only if exactly 1 input image)
show_video_button = images and len(images) == 1
# Return output image path, video button visibility, and hide video output
return output_path, gr.update(visible=show_video_button), gr.update(visible=False)
except Exception as e:
raise gr.Error(f"Image generation failed: {e}")
def create_video_transition(
input_image_gallery: List[str],
prompt_input: str,
output_image: str,
oauth_token: Optional[gr.OAuthToken] = None
) -> tuple:
"""
Creates a video transition between the input and output images.
Returns: (video_path, video_visible)
"""
if not verify_pro_status(oauth_token):
raise gr.Error("Access Denied. This service is for PRO users only.")
if not input_image_gallery or not output_image:
raise gr.Error("Both input and output images are required for video creation.")
try:
video_client = Client("multimodalart/wan-2-2-first-last-frame", hf_token=oauth_token.token)
input_image_path = input_image_gallery[0][0]
result = video_client.predict(
start_image_pil=handle_file(input_image_path),
end_image_pil=handle_file(output_image),
prompt=prompt_input,
api_name="/generate_video"
)
print(result)
return result["video"]
except Exception as e:
raise gr.Error(f"Video creation failed: {e}")
# --- Gradio App UI ---
css = '''
#sub_title{margin-top: -35px !important}
.tab-wrapper{margin-bottom: -33px !important}
.tabitem{padding: 0px !important}
.fillable{max-width: 980px !important}
.dark .progress-text {color: white}
.logo-dark{display: none}
.dark .logo-dark{display: block !important}
.dark .logo-light{display: none}
.grid-container img{object-fit: contain}
.grid-container {display: grid;grid-template-columns: repeat(2, 1fr)}
.grid-container:has(> .gallery-item:only-child) {grid-template-columns: 1fr}
#wan_ad p{text-align: center;padding: .5em}
'''
with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo:
gr.HTML('''
<img class="logo-dark" src='https://huggingface.co/spaces/multimodalart/nano-banana/resolve/main/nano_banana_pros.png' style='margin: 0 auto; max-width: 500px' />
<img class="logo-light" src='https://huggingface.co/spaces/multimodalart/nano-banana/resolve/main/nano_banana_pros_light.png' style='margin: 0 auto; max-width: 500px' />
''')
gr.HTML("<h3 style='text-align:center'>Hugging Face PRO users can use Google's Nano Banana (Gemini 2.5 Flash Image Preview) on this Space. <a href='http://huggingface.co/subscribe/pro?source=nana_banana' target='_blank'>Subscribe to PRO</a></h3>", elem_id="sub_title")
pro_message = gr.Markdown(visible=False)
main_interface = gr.Column(visible=False)
with main_interface:
with gr.Row():
with gr.Column(scale=1):
with gr.Group():
image_input_gallery = gr.Gallery(
label="Upload one or more images here. Leave empty for text-to-image",
file_types=["image"],
height="auto"
)
prompt_input = gr.Textbox(
label="Prompt",
placeholder="Turns this photo into a masterpiece"
)
generate_button = gr.Button("Generate", variant="primary")
with gr.Column(scale=1):
output_image = gr.Image(label="Output", interactive=False, elem_id="output", type="filepath")
use_image_button = gr.Button("♻️ Use this Image for Next Edit")
create_video_button = gr.Button("Create a video between the two images 🎥", variant="primary", visible=False)
with gr.Group(visible=False) as video_group:
video_output = gr.Video(label="Generated Video", show_download_button=True, autoplay=True)
gr.Markdown("Generate more with [Wan 2.2 first-last-frame](https://huggingface.co/spaces/multimodalart/wan-2-2-first-last-frame)", elem_id="wan_ad")
gr.Markdown("## Thank you for being a PRO! 🤗")
login_button = gr.LoginButton()
# --- Event Handlers ---
gr.on(
triggers=[generate_button.click, prompt_input.submit],
fn=lambda: [gr.update(visible=False), gr.update(visible=False)],
inputs=[],
outputs=[create_video_button, video_group],
).then(
fn=unified_image_generator,
inputs=[prompt_input, image_input_gallery],
outputs=[output_image, create_video_button, video_group],
)
use_image_button.click(
lambda img_path: [img_path] if img_path else None,
inputs=[output_image],
outputs=[image_input_gallery]
)
# Video creation handler
create_video_button.click(
fn=lambda: gr.update(visible=True),
inputs=[],
outputs=[video_group],
).then(
fn=create_video_transition,
inputs=[image_input_gallery, prompt_input, output_image],
outputs=[video_output],
)
# --- Access Control Logic ---
def control_access(
profile: Optional[gr.OAuthProfile] = None,
oauth_token: Optional[gr.OAuthToken] = None
):
if not profile:
return gr.update(visible=False), gr.update(visible=False)
if verify_pro_status(oauth_token):
return gr.update(visible=True), gr.update(visible=False)
else:
message = (
"## ✨ Exclusive Access for PRO Users\n\n"
"Thank you for your interest! This app is available exclusively for our Hugging Face **PRO** members.\n\n"
"To unlock this and many other cool stuff, please consider upgrading your account.\n\n"
"### [**Become a PRO Today!**](http://huggingface.co/subscribe/pro?source=nana_banana)"
)
return gr.update(visible=False), gr.update(visible=True, value=message)
demo.load(control_access, inputs=None, outputs=[main_interface, pro_message])
if __name__ == "__main__":
demo.queue(max_size=None, default_concurrency_limit=None)
demo.launch() |