Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,47 @@
|
|
| 1 |
import os
|
| 2 |
-
import google-genai as genai
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import base64
|
| 5 |
import time
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
while
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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("
|
| 65 |
-
prompt_input = gr.Textbox(label="Prompt"
|
| 66 |
-
|
| 67 |
generate_btn = gr.Button("Generate Video")
|
| 68 |
-
status = gr.Textbox(label="Status")
|
| 69 |
video_output = gr.Video(label="Generated Video")
|
|
|
|
| 70 |
|
| 71 |
-
def
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
generate_btn.click(
|
| 76 |
|
| 77 |
demo.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import time
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from google import genai
|
| 5 |
+
|
| 6 |
+
# Initialize client with API key from env var
|
| 7 |
+
client = genai.Client()
|
| 8 |
+
|
| 9 |
+
def generate_video(prompt):
|
| 10 |
+
operation = client.models.generate_videos(
|
| 11 |
+
model="veo-3.0-generate-preview",
|
| 12 |
+
prompt=prompt,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
while not operation.done:
|
| 16 |
+
print("Waiting for video generation to complete...")
|
| 17 |
+
time.sleep(10)
|
| 18 |
+
operation = client.operations.get(operation)
|
| 19 |
+
|
| 20 |
+
generated_video = operation.response.generated_videos[0]
|
| 21 |
+
# Download video bytes
|
| 22 |
+
video_file = client.files.download(file=generated_video.video)
|
| 23 |
+
|
| 24 |
+
# Save locally
|
| 25 |
+
video_path = "dialogue_example.mp4"
|
| 26 |
+
with open(video_path, "wb") as f:
|
| 27 |
+
f.write(video_file)
|
| 28 |
+
|
| 29 |
+
return video_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
|
|
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("# Gemini Veo Video Generator")
|
| 33 |
+
prompt_input = gr.Textbox(label="Video Prompt", value="""A close up of two people staring at a cryptic drawing on a wall, torchlight flickering.
|
| 34 |
+
A man murmurs, 'This must be it. That's the secret code.' The woman looks at him and whispering excitedly, 'What did you find?'""")
|
| 35 |
generate_btn = gr.Button("Generate Video")
|
|
|
|
| 36 |
video_output = gr.Video(label="Generated Video")
|
| 37 |
+
status = gr.Textbox(label="Status")
|
| 38 |
|
| 39 |
+
def on_generate(prompt):
|
| 40 |
+
status.update("Generating video, please wait...")
|
| 41 |
+
video_path = generate_video(prompt)
|
| 42 |
+
status.update("Generation complete!")
|
| 43 |
+
return video_path, "Generation complete!"
|
| 44 |
|
| 45 |
+
generate_btn.click(on_generate, inputs=prompt_input, outputs=[video_output, status])
|
| 46 |
|
| 47 |
demo.launch()
|