Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from io import BytesIO
|
|
4 |
from PIL import Image
|
5 |
import os
|
6 |
import tempfile
|
|
|
7 |
|
8 |
TOKEN = os.getenv("TOKEN1")
|
9 |
API_URL = os.getenv("API_URL")
|
@@ -69,9 +70,16 @@ def download_image(image_url):
|
|
69 |
try:
|
70 |
response = requests.get(image_url, stream=True)
|
71 |
response.raise_for_status()
|
72 |
-
|
73 |
-
#
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
for chunk in response.iter_content(chunk_size=8192):
|
76 |
tmp_file.write(chunk)
|
77 |
temp_file_path = tmp_file.name
|
|
|
4 |
from PIL import Image
|
5 |
import os
|
6 |
import tempfile
|
7 |
+
import mimetypes
|
8 |
|
9 |
TOKEN = os.getenv("TOKEN1")
|
10 |
API_URL = os.getenv("API_URL")
|
|
|
70 |
try:
|
71 |
response = requests.get(image_url, stream=True)
|
72 |
response.raise_for_status()
|
73 |
+
|
74 |
+
# Get the content type from the headers
|
75 |
+
content_type = response.headers.get('content-type')
|
76 |
+
extension = mimetypes.guess_extension(content_type)
|
77 |
+
|
78 |
+
if not extension:
|
79 |
+
extension = ".png" # Default to .png if can't determine the extension
|
80 |
+
|
81 |
+
# Create a temporary file with the correct extension
|
82 |
+
with tempfile.NamedTemporaryFile(suffix=extension, delete=False) as tmp_file:
|
83 |
for chunk in response.iter_content(chunk_size=8192):
|
84 |
tmp_file.write(chunk)
|
85 |
temp_file_path = tmp_file.name
|