dschandra commited on
Commit
286723a
·
verified ·
1 Parent(s): 7153980

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import requests
3
  from PIL import Image
4
  import io
 
5
 
6
  # Define the API URL for the Craiyon model (lightweight text-to-image generator)
7
  API_URL = "https://api-inference.huggingface.co/models/dalle-mini"
@@ -13,18 +14,25 @@ def generate_comic(prompt):
13
  # Sending request to the model with user prompt
14
  response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
15
 
16
- # Checking for successful response
 
 
 
17
  if response.status_code != 200:
18
  return f"Error: {response.status_code}, {response.text}"
19
 
20
- # Extracting generated images
21
  images = response.json().get("generated_images", [])
22
-
23
  if not images:
24
  return "No images were generated, please try again with a different prompt."
25
 
26
- # Convert image bytes into PIL Image format
27
- pil_images = [Image.open(io.BytesIO(img)) for img in images]
 
 
 
 
28
 
29
  return pil_images # Return the list of PIL images
30
 
 
2
  import requests
3
  from PIL import Image
4
  import io
5
+ import base64
6
 
7
  # Define the API URL for the Craiyon model (lightweight text-to-image generator)
8
  API_URL = "https://api-inference.huggingface.co/models/dalle-mini"
 
14
  # Sending request to the model with user prompt
15
  response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
16
 
17
+ # Print the response for debugging
18
+ print(f"API Response: {response.status_code}, {response.text}")
19
+
20
+ # Check if the response is successful
21
  if response.status_code != 200:
22
  return f"Error: {response.status_code}, {response.text}"
23
 
24
+ # Extracting generated images (ensure this key is correct, it might differ depending on the model)
25
  images = response.json().get("generated_images", [])
26
+
27
  if not images:
28
  return "No images were generated, please try again with a different prompt."
29
 
30
+ # Process the images (assuming they are base64 encoded)
31
+ pil_images = []
32
+ for img in images:
33
+ image_data = base64.b64decode(img)
34
+ image = Image.open(io.BytesIO(image_data))
35
+ pil_images.append(image)
36
 
37
  return pil_images # Return the list of PIL images
38