swift-app-96 / app.py
AverageAiLiker's picture
Deploy Gradio app with multiple files
3203372 verified
import gradio as gr
import requests
def generate_video(prompt, duration, progress=gr.Progress()):
"""Generate video using akhaliq-Sora-2 API"""
if not prompt.strip():
raise gr.Error("Please enter a text prompt")
progress(0.1, desc="Starting video generation...")
try:
# API endpoint for akhaliq-Sora-2
api_url = "https://akhaliq-sora-2.hf.space/api/predict"
# Prepare the payload
payload = {
"data": [
prompt, # text prompt
duration, # duration in seconds
30, # fps (fixed)
512, # width (fixed)
512, # height (fixed)
]
}
progress(0.5, desc="Generating video...")
# Make the API request
response = requests.post(api_url, json=payload)
if response.status_code == 200:
progress(0.9, desc="Processing...")
result = response.json()
# Check if the generation is complete
if "data" in result and len(result["data"]) > 0:
video_url = result["data"][0]
if video_url:
progress(1.0, desc="Complete!")
return video_url
else:
raise gr.Error("Video generation failed")
else:
raise gr.Error("Invalid response from API")
else:
raise gr.Error(f"API request failed with status code: {response.status_code}")
except requests.exceptions.RequestException as e:
raise gr.Error(f"Network error: {str(e)}")
except Exception as e:
raise gr.Error(f"Error generating video: {str(e)}")
# Create the Gradio interface
with gr.Blocks(title="Text-to-Video AI Generator") as demo:
gr.Markdown("# 🎬 Text-to-Video AI Generator")
gr.Markdown("Transform your text into AI-generated videos using Sora-2 model")
gr.Markdown('<p><a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">Built with anycoder</a></p>')
with gr.Row():
with gr.Column():
prompt_input = gr.Textbox(
label="Video Description",
placeholder="Describe the video you want to generate...",
lines=3
)
duration_slider = gr.Slider(
minimum=1,
maximum=10,
value=4,
step=1,
label="Video Duration (seconds)"
)
generate_btn = gr.Button("Generate Video", variant="primary")
with gr.Column():
video_output = gr.Video(
label="Generated Video",
height=400
)
gr.Examples(
examples=[
["A cat playing piano", 4],
["A robot dancing", 6],
["A sunset over the ocean", 3],
],
inputs=[prompt_input, duration_slider]
)
generate_btn.click(
fn=generate_video,
inputs=[prompt_input, duration_slider],
outputs=[video_output],
show_progress=True
)
# Launch the app
if __name__ == "__main__":
demo.launch()