artintel235 commited on
Commit
314a6d7
·
verified ·
1 Parent(s): 153920b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -3,7 +3,6 @@ import requests
3
  from io import BytesIO
4
  from PIL import Image
5
  import os
6
- # HARDCODED_URL = "" # Replace with your desired URL
7
 
8
  TOKEN = os.getenv("TOKEN1")
9
  API_URL = os.getenv("API_URL")
@@ -14,13 +13,23 @@ model_id = os.getenv("MODEL_ID")
14
 
15
  def get_image_from_url(url):
16
  """
17
- Fetches and returns an image from a given URL.
18
  """
19
  try:
20
  response = requests.get(url, stream=True)
21
  response.raise_for_status()
22
  image = Image.open(BytesIO(response.content))
 
 
 
 
 
 
 
 
 
23
  return image
 
24
  except requests.exceptions.RequestException as e:
25
  return f"Error fetching image: {e}"
26
  except Exception as e:
@@ -33,7 +42,7 @@ def generate_image(prompt, aspect_ratio, realism):
33
  global no_of_accounts
34
  global model_id
35
  payload = {
36
- "id": model_id, # Replace with appropriate ID if necessary
37
  "inputs": [prompt, aspect_ratio, str(realism).lower()],
38
  }
39
  headers = {"Authorization": f"Bearer {TOKEN}"}
@@ -59,7 +68,7 @@ def generate_image(prompt, aspect_ratio, realism):
59
  except Exception as e:
60
  return f"Error"
61
 
62
- # Define the Gradio interface
63
  interface = gr.Interface(
64
  fn=generate_image,
65
  inputs=[
@@ -77,4 +86,4 @@ interface = gr.Interface(
77
  )
78
 
79
  # Launch the interface
80
- interface.launch()
 
3
  from io import BytesIO
4
  from PIL import Image
5
  import os
 
6
 
7
  TOKEN = os.getenv("TOKEN1")
8
  API_URL = os.getenv("API_URL")
 
13
 
14
  def get_image_from_url(url):
15
  """
16
+ Fetches and returns an image from a given URL, converting to PNG if needed.
17
  """
18
  try:
19
  response = requests.get(url, stream=True)
20
  response.raise_for_status()
21
  image = Image.open(BytesIO(response.content))
22
+
23
+ # Convert to PNG if the image is WebP
24
+ if image.format == "WEBP":
25
+ image = image.convert("RGBA") # Convert to RGBA to handle transparency
26
+ output_buffer = BytesIO()
27
+ image.save(output_buffer, format="PNG")
28
+ output_buffer.seek(0) # Reset buffer position for reading
29
+ image = Image.open(output_buffer) # Re-open image from buffer
30
+
31
  return image
32
+
33
  except requests.exceptions.RequestException as e:
34
  return f"Error fetching image: {e}"
35
  except Exception as e:
 
42
  global no_of_accounts
43
  global model_id
44
  payload = {
45
+ "id": model_id,
46
  "inputs": [prompt, aspect_ratio, str(realism).lower()],
47
  }
48
  headers = {"Authorization": f"Bearer {TOKEN}"}
 
68
  except Exception as e:
69
  return f"Error"
70
 
71
+
72
  interface = gr.Interface(
73
  fn=generate_image,
74
  inputs=[
 
86
  )
87
 
88
  # Launch the interface
89
+ interface.launch()