Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,77 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
import google.generativeai as genai
|
| 5 |
import base64
|
| 6 |
-
import requests
|
| 7 |
import time
|
| 8 |
-
import
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
if image_file is None:
|
| 15 |
-
return None
|
| 16 |
-
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 17 |
-
|
| 18 |
-
def generate_video(prompt, image_file):
|
| 19 |
-
image_bytes = encode_image_to_base64(image_file) if image_file else None
|
| 20 |
-
|
| 21 |
-
config = {
|
| 22 |
-
"model": "veo-2.0-generate-001",
|
| 23 |
-
"prompt": prompt,
|
| 24 |
-
"config": {
|
| 25 |
-
"numberOfVideos": 1,
|
| 26 |
-
# Optional settings:
|
| 27 |
-
# "durationSeconds": 3,
|
| 28 |
-
# "fps": 24,
|
| 29 |
-
# "resolution": "720p",
|
| 30 |
-
}
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
if image_bytes:
|
| 34 |
-
config["image"] = {
|
| 35 |
-
"imageBytes": image_bytes,
|
| 36 |
-
"mimeType": "image/png"
|
| 37 |
-
}
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
-
op =
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
videos = op.response.get("generatedVideos", [])
|
| 46 |
if not videos:
|
| 47 |
-
return "No video
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
filename = "generated_video.mp4"
|
| 52 |
-
with open(filename, "wb") as f:
|
| 53 |
-
f.write(response.content)
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
return f"Error: {str(e)}", None
|
| 59 |
|
| 60 |
-
#
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
-
gr.Markdown("##
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
image_input = gr.File(label="Optional Image (PNG/JPG)", file_types=[".png", ".jpg", ".jpeg"])
|
| 66 |
-
|
| 67 |
generate_btn = gr.Button("Generate Video")
|
| 68 |
-
|
| 69 |
-
status_output = gr.Textbox(label="Status")
|
| 70 |
video_output = gr.Video(label="Generated Video")
|
| 71 |
|
| 72 |
-
def
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
generate_btn.click(
|
| 76 |
-
fn=run_generation,
|
| 77 |
-
inputs=[prompt_input, image_input],
|
| 78 |
-
outputs=[status_output, video_output]
|
| 79 |
-
)
|
| 80 |
|
| 81 |
-
|
| 82 |
-
if __name__ == "__main__":
|
| 83 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import genai
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
import base64
|
|
|
|
| 5 |
import time
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
# Set up your API key
|
| 9 |
+
genai.configure(api_key=os.getenv("GENAI_API_KEY", "your-api-key-here"))
|
| 10 |
|
| 11 |
+
# Helper to encode image
|
| 12 |
+
def encode_image(image_path):
|
| 13 |
+
with open(image_path, "rb") as f:
|
| 14 |
+
return base64.b64encode(f.read()).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Polling for operation
|
| 17 |
+
def poll_operation(op_name):
|
| 18 |
+
while True:
|
| 19 |
+
op = genai.get_operation(name=op_name)
|
| 20 |
+
if op.get("done"):
|
| 21 |
+
return op
|
| 22 |
+
time.sleep(2)
|
| 23 |
+
|
| 24 |
+
# Main generation function
|
| 25 |
+
def generate_video(prompt, image):
|
| 26 |
+
if not prompt or image is None:
|
| 27 |
+
return "Prompt and image are required.", None
|
| 28 |
+
|
| 29 |
+
image_b64 = encode_image(image)
|
| 30 |
+
|
| 31 |
+
model = genai.GenerativeModel("veo-2.0-generate-001") # Use correct model name
|
| 32 |
try:
|
| 33 |
+
op = model.generate_videos(
|
| 34 |
+
prompt=prompt,
|
| 35 |
+
image={
|
| 36 |
+
"imageBytes": image_b64,
|
| 37 |
+
"mimeType": "image/png"
|
| 38 |
+
},
|
| 39 |
+
config={
|
| 40 |
+
"numberOfVideos": 1
|
| 41 |
+
}
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
op_result = poll_operation(op["name"])
|
| 45 |
+
videos = op_result["response"].get("generatedVideos", [])
|
| 46 |
|
|
|
|
| 47 |
if not videos:
|
| 48 |
+
return "No video returned", None
|
| 49 |
|
| 50 |
+
video_url = videos[0]["video"]["uri"]
|
| 51 |
+
video_data = requests.get(video_url).content
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
output_path = "generated_video.mp4"
|
| 54 |
+
with open(output_path, "wb") as f:
|
| 55 |
+
f.write(video_data)
|
| 56 |
+
|
| 57 |
+
return "Video generated successfully.", output_path
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
return f"Error: {str(e)}", None
|
| 61 |
|
| 62 |
+
# Gradio UI
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("## 🎬 Gemini Veo Video Generator")
|
| 65 |
+
prompt_input = gr.Textbox(label="Prompt")
|
| 66 |
+
image_input = gr.Image(type="filepath", label="Upload an Image")
|
|
|
|
|
|
|
| 67 |
generate_btn = gr.Button("Generate Video")
|
| 68 |
+
status = gr.Textbox(label="Status")
|
|
|
|
| 69 |
video_output = gr.Video(label="Generated Video")
|
| 70 |
|
| 71 |
+
def run(prompt, image):
|
| 72 |
+
msg, path = generate_video(prompt, image)
|
| 73 |
+
return msg, path
|
| 74 |
|
| 75 |
+
generate_btn.click(fn=run, inputs=[prompt_input, image_input], outputs=[status, video_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
demo.launch()
|
|
|
|
|
|