Jamiiwej2903 commited on
Commit
4db1b30
·
verified ·
1 Parent(s): 584ebf3

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -32
main.py CHANGED
@@ -7,46 +7,39 @@ from PIL import Image
7
  import ffmpeg
8
  import tempfile
9
  import os
 
10
 
11
  app = FastAPI()
12
 
13
- API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
14
 
15
  @app.post("/generate_video/")
16
  async def generate_video_api(
17
- file: UploadFile = File(...),
18
- prompt: str = Form(...),
19
- num_frames: int = Form(14),
20
- fps: int = Form(7)
21
  ):
22
  try:
23
  # Read the uploaded image file
24
  image_content = await file.read()
25
-
26
- # Generate a sequence of images
27
- frames = []
28
- for i in range(num_frames):
29
- # Prepare the payload
30
- payload = {
31
- "inputs": f"{prompt}, frame {i+1} of {num_frames}",
32
  "parameters": {
33
- "num_inference_steps": 50,
34
- "guidance_scale": 7.5,
35
  }
36
  }
37
-
38
- # Make the API call
39
- response = requests.post(API_URL, json=payload)
40
- response.raise_for_status()
41
-
42
- # Add the generated image to frames
43
- frames.append(Image.open(io.BytesIO(response.content)))
44
 
45
  # Create a temporary directory
46
  with tempfile.TemporaryDirectory() as tmpdir:
47
  # Save frames as temporary files
48
  frame_files = []
49
- for i, frame in enumerate(frames):
50
  frame_file = os.path.join(tmpdir, f"frame_{i:03d}.png")
51
  frame.save(frame_file)
52
  frame_files.append(frame_file)
@@ -70,17 +63,8 @@ async def generate_video_api(
70
  # Return the video as a streaming response
71
  return StreamingResponse(io.BytesIO(video_content), media_type="video/mp4")
72
 
73
- except requests.exceptions.HTTPError as http_err:
74
- # Handle HTTP errors
75
- error_detail = f"HTTP error occurred: {http_err}"
76
- raise HTTPException(status_code=500, detail=error_detail)
77
-
78
- except ffmpeg.Error as e:
79
- # Handle FFmpeg errors
80
- raise HTTPException(status_code=500, detail=f"FFmpeg error occurred: {e.stderr.decode()}")
81
-
82
  except Exception as err:
83
- # Handle any other errors
84
  raise HTTPException(status_code=500, detail=f"An error occurred: {err}")
85
 
86
  if __name__ == "__main__":
 
7
  import ffmpeg
8
  import tempfile
9
  import os
10
+ from huggingface_hub import InferenceClient
11
 
12
  app = FastAPI()
13
 
14
+ client = InferenceClient("stabilityai/stable-video-diffusion-img2vid-xt-1-1-tensorrt")
15
 
16
  @app.post("/generate_video/")
17
  async def generate_video_api(
18
+ file: UploadFile = File(...),
19
+ num_frames: int = Form(14),
20
+ fps: int = Form(7)
 
21
  ):
22
  try:
23
  # Read the uploaded image file
24
  image_content = await file.read()
25
+ image = Image.open(io.BytesIO(image_content))
26
+
27
+ # Generate video frames using the stable-video-diffusion model
28
+ video_frames = client.post(
29
+ json={
30
+ "inputs": image,
 
31
  "parameters": {
32
+ "num_inference_steps": 25,
33
+ "num_frames": num_frames,
34
  }
35
  }
36
+ )
 
 
 
 
 
 
37
 
38
  # Create a temporary directory
39
  with tempfile.TemporaryDirectory() as tmpdir:
40
  # Save frames as temporary files
41
  frame_files = []
42
+ for i, frame in enumerate(video_frames):
43
  frame_file = os.path.join(tmpdir, f"frame_{i:03d}.png")
44
  frame.save(frame_file)
45
  frame_files.append(frame_file)
 
63
  # Return the video as a streaming response
64
  return StreamingResponse(io.BytesIO(video_content), media_type="video/mp4")
65
 
 
 
 
 
 
 
 
 
 
66
  except Exception as err:
67
+ # Handle any errors
68
  raise HTTPException(status_code=500, detail=f"An error occurred: {err}")
69
 
70
  if __name__ == "__main__":