Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# gemini_video_gradio.py
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
import base64
|
| 6 |
+
import requests
|
| 7 |
+
import time
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Configure your API key
|
| 11 |
+
genai.configure(api_key=os.getenv("API_KEY") or "YOUR_API_KEY_HERE")
|
| 12 |
+
|
| 13 |
+
def encode_image_to_base64(image_file):
|
| 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 = genai.models.generate_videos(**config)
|
| 41 |
+
while not op.done:
|
| 42 |
+
time.sleep(1)
|
| 43 |
+
op = genai.operations.get_videos_operation(operation=op)
|
| 44 |
+
|
| 45 |
+
videos = op.response.get("generatedVideos", [])
|
| 46 |
+
if not videos:
|
| 47 |
+
return "No video was generated.", None
|
| 48 |
+
|
| 49 |
+
video_uri = videos[0]["video"]["uri"]
|
| 50 |
+
response = requests.get(video_uri)
|
| 51 |
+
filename = "generated_video.mp4"
|
| 52 |
+
with open(filename, "wb") as f:
|
| 53 |
+
f.write(response.content)
|
| 54 |
+
|
| 55 |
+
return "Video generated successfully!", filename
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return f"Error: {str(e)}", None
|
| 59 |
+
|
| 60 |
+
# Build Gradio interface
|
| 61 |
+
with gr.Blocks() as demo:
|
| 62 |
+
gr.Markdown("## 🎥 Gemini Video Generator")
|
| 63 |
+
with gr.Row():
|
| 64 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="e.g. A futuristic city at night")
|
| 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 run_generation(prompt, image):
|
| 73 |
+
return generate_video(prompt, image)
|
| 74 |
+
|
| 75 |
+
generate_btn.click(
|
| 76 |
+
fn=run_generation,
|
| 77 |
+
inputs=[prompt_input, image_input],
|
| 78 |
+
outputs=[status_output, video_output]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Run the app
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.launch()
|