Nifemi Alpine Durin commited on
Commit
fcd2b73
·
1 Parent(s): 8d3409d

image url fix

Browse files
Files changed (2) hide show
  1. README.md +3 -0
  2. app.py +15 -3
README.md CHANGED
@@ -15,3 +15,6 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
15
  ![image/png](https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/f4xH6ZNx1rVbKzr8jAHmt.png)
16
 
17
  ![image/png](https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/A9BoWW2ulZycv0OQaihKO.png)
 
 
 
 
15
  ![image/png](https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/f4xH6ZNx1rVbKzr8jAHmt.png)
16
 
17
  ![image/png](https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/A9BoWW2ulZycv0OQaihKO.png)
18
+
19
+
20
+ [![Generated Graphic](https://api.slidegen.civai.co/images/image_20240606065804.png)](https://api.slidegen.civai.co/images/image_20240606065804.png)
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import requests, base64, io
3
  from dotenv import load_dotenv
4
- import os, datetime
5
  from PIL import Image
6
  load_dotenv()
7
  API_URL = os.environ.get("API_URL", "http://0.0.0.0:8021")
@@ -9,19 +9,31 @@ APP_ENV = os.environ.get("APP_ENV", None)
9
 
10
 
11
  def image_to_base64(input_file):
 
 
 
12
  if input_file is None:
13
  return None
14
 
15
- file_bytes = None
16
  # Check if the input is a string (file path)
17
  if isinstance(input_file, str):
 
 
18
  with open(input_file, "rb") as file:
19
  file_bytes = file.read()
20
  else:
21
  # Handle file-like object (_TemporaryFileWrapper)
 
22
  file_bytes = input_file.read()
23
 
24
- return base64.b64encode(file_bytes).decode()
 
 
 
 
 
 
 
25
 
26
 
27
  # Assuming the API returns an image URL in the response
 
1
  import gradio as gr
2
  import requests, base64, io
3
  from dotenv import load_dotenv
4
+ import os, datetime, mimetypes
5
  from PIL import Image
6
  load_dotenv()
7
  API_URL = os.environ.get("API_URL", "http://0.0.0.0:8021")
 
9
 
10
 
11
  def image_to_base64(input_file):
12
+ file_bytes = None
13
+ file_path = None
14
+
15
  if input_file is None:
16
  return None
17
 
 
18
  # Check if the input is a string (file path)
19
  if isinstance(input_file, str):
20
+
21
+ file_path = input_file
22
  with open(input_file, "rb") as file:
23
  file_bytes = file.read()
24
  else:
25
  # Handle file-like object (_TemporaryFileWrapper)
26
+ file_path = input_file.name
27
  file_bytes = input_file.read()
28
 
29
+ # Guess the MIME type of the file based on the file extension
30
+ mime_type, _ = mimetypes.guess_type(file_path)
31
+ if mime_type is None:
32
+ mime_type = 'application/octet-stream' # Use a binary data MIME type as a fallback
33
+
34
+ data_url = f"data:{mime_type};base64,{base64.b64encode(file_bytes).decode()}"
35
+
36
+ return data_url
37
 
38
 
39
  # Assuming the API returns an image URL in the response