Spaces:
Runtime error
Runtime error
File size: 2,495 Bytes
8048fac 3d59cc1 ba4fa84 3d59cc1 48e275e fd3d654 4b71a9d 4db1b30 3d59cc1 4db1b30 3d59cc1 f566057 ee258e0 4db1b30 ee258e0 8048fac fd3d654 4db1b30 fd3d654 4db1b30 fd3d654 4db1b30 ba4fa84 4b71a9d 4db1b30 4b71a9d fd3d654 12de95f 4b71a9d 12de95f 48e275e 8048fac 12de95f 8048fac 4db1b30 8048fac 3d59cc1 e593310 |
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 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
import uvicorn
from fastapi.responses import StreamingResponse
import io
import requests
from PIL import Image
import ffmpeg
import tempfile
import os
from huggingface_hub import InferenceClient
app = FastAPI()
client = InferenceClient("stabilityai/stable-video-diffusion-img2vid-xt-1-1-tensorrt")
@app.post("/generate_video/")
async def generate_video_api(
file: UploadFile = File(...),
num_frames: int = Form(14),
fps: int = Form(7)
):
try:
# Read the uploaded image file
image_content = await file.read()
image = Image.open(io.BytesIO(image_content))
# Generate video frames using the stable-video-diffusion model
video_frames = client.post(
json={
"inputs": image,
"parameters": {
"num_inference_steps": 25,
"num_frames": num_frames,
}
}
)
# Create a temporary directory
with tempfile.TemporaryDirectory() as tmpdir:
# Save frames as temporary files
frame_files = []
for i, frame in enumerate(video_frames):
frame_file = os.path.join(tmpdir, f"frame_{i:03d}.png")
frame.save(frame_file)
frame_files.append(frame_file)
# Create a temporary file for the video
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_video:
temp_video_path = temp_video.name
# Use ffmpeg-python to combine images into a video
input_stream = ffmpeg.input(os.path.join(tmpdir, 'frame_%03d.png'), framerate=fps)
output_stream = ffmpeg.output(input_stream, temp_video_path, vcodec='libx264', pix_fmt='yuv420p')
ffmpeg.run(output_stream, overwrite_output=True)
# Read the temporary video file
with open(temp_video_path, 'rb') as video_file:
video_content = video_file.read()
# Delete the temporary video file
os.unlink(temp_video_path)
# Return the video as a streaming response
return StreamingResponse(io.BytesIO(video_content), media_type="video/mp4")
except Exception as err:
# Handle any errors
raise HTTPException(status_code=500, detail=f"An error occurred: {err}")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |