Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -1,12 +1,10 @@
|
|
1 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
2 |
import uvicorn
|
3 |
-
from fastapi.responses import StreamingResponse
|
4 |
import io
|
5 |
-
import base64
|
6 |
import requests
|
7 |
-
import os
|
8 |
-
import subprocess
|
9 |
from PIL import Image
|
|
|
10 |
|
11 |
app = FastAPI()
|
12 |
|
@@ -23,12 +21,8 @@ async def generate_video_api(
|
|
23 |
# Read the uploaded image file
|
24 |
image_content = await file.read()
|
25 |
|
26 |
-
# Save the image temporarily
|
27 |
-
with open("input_image.png", "wb") as f:
|
28 |
-
f.write(image_content)
|
29 |
-
|
30 |
# Generate a sequence of images
|
31 |
-
|
32 |
for i in range(num_frames):
|
33 |
# Prepare the payload
|
34 |
payload = {
|
@@ -43,26 +37,39 @@ async def generate_video_api(
|
|
43 |
response = requests.post(API_URL, json=payload)
|
44 |
response.raise_for_status()
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
# Use FFmpeg to combine images into a video
|
52 |
-
|
53 |
-
'ffmpeg',
|
54 |
-
'-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
os.remove(img)
|
64 |
-
os.remove("input_image.png")
|
65 |
-
os.remove("output.mp4")
|
66 |
|
67 |
# Return the video as a streaming response
|
68 |
return StreamingResponse(io.BytesIO(video_content), media_type="video/mp4")
|
@@ -70,9 +77,7 @@ async def generate_video_api(
|
|
70 |
except requests.exceptions.HTTPError as http_err:
|
71 |
# Handle HTTP errors
|
72 |
error_detail = f"HTTP error occurred: {http_err}"
|
73 |
-
|
74 |
-
error_detail += f"\nResponse content: {response.text}"
|
75 |
-
raise HTTPException(status_code=response.status_code, detail=error_detail)
|
76 |
|
77 |
except Exception as err:
|
78 |
# Handle any other errors
|
|
|
1 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
2 |
import uvicorn
|
3 |
+
from fastapi.responses import StreamingResponse
|
4 |
import io
|
|
|
5 |
import requests
|
|
|
|
|
6 |
from PIL import Image
|
7 |
+
import subprocess
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
|
|
21 |
# Read the uploaded image file
|
22 |
image_content = await file.read()
|
23 |
|
|
|
|
|
|
|
|
|
24 |
# Generate a sequence of images
|
25 |
+
frames = []
|
26 |
for i in range(num_frames):
|
27 |
# Prepare the payload
|
28 |
payload = {
|
|
|
37 |
response = requests.post(API_URL, json=payload)
|
38 |
response.raise_for_status()
|
39 |
|
40 |
+
# Add the generated image to frames
|
41 |
+
frames.append(Image.open(io.BytesIO(response.content)))
|
42 |
+
|
43 |
+
# Create a video from frames using PIL
|
44 |
+
video_frames = []
|
45 |
+
for frame in frames:
|
46 |
+
img_byte_arr = io.BytesIO()
|
47 |
+
frame.save(img_byte_arr, format='PNG')
|
48 |
+
video_frames.append(img_byte_arr.getvalue())
|
49 |
|
50 |
# Use FFmpeg to combine images into a video
|
51 |
+
ffmpeg_cmd = [
|
52 |
+
'ffmpeg',
|
53 |
+
'-f', 'image2pipe',
|
54 |
+
'-framerate', str(fps),
|
55 |
+
'-i', '-',
|
56 |
+
'-c:v', 'libx264',
|
57 |
+
'-pix_fmt', 'yuv420p',
|
58 |
+
'-movflags', '+faststart',
|
59 |
+
'-f', 'mp4',
|
60 |
+
'-'
|
61 |
+
]
|
62 |
|
63 |
+
process = subprocess.Popen(ffmpeg_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
64 |
+
|
65 |
+
for frame in video_frames:
|
66 |
+
process.stdin.write(frame)
|
67 |
+
|
68 |
+
process.stdin.close()
|
69 |
+
video_content, stderr = process.communicate()
|
70 |
|
71 |
+
if process.returncode != 0:
|
72 |
+
raise Exception(f"FFmpeg error: {stderr.decode()}")
|
|
|
|
|
|
|
73 |
|
74 |
# Return the video as a streaming response
|
75 |
return StreamingResponse(io.BytesIO(video_content), media_type="video/mp4")
|
|
|
77 |
except requests.exceptions.HTTPError as http_err:
|
78 |
# Handle HTTP errors
|
79 |
error_detail = f"HTTP error occurred: {http_err}"
|
80 |
+
raise HTTPException(status_code=500, detail=error_detail)
|
|
|
|
|
81 |
|
82 |
except Exception as err:
|
83 |
# Handle any other errors
|