Spaces:
Runtime error
Runtime error
generate files with extensions
Browse files
app.py
CHANGED
|
@@ -8,6 +8,11 @@ from dotenv import load_dotenv
|
|
| 8 |
from pymongo.mongo_client import MongoClient
|
| 9 |
from pymongo.server_api import ServerApi
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
openai_key = os.getenv("OPENAI_API_KEY")
|
|
@@ -69,7 +74,19 @@ def generate_image(text, pw, model):
|
|
| 69 |
print(e)
|
| 70 |
raise gr.Error("An error occurred while saving the prompt to the database.")
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
with gr.Blocks() as demo:
|
|
|
|
| 8 |
from pymongo.mongo_client import MongoClient
|
| 9 |
from pymongo.server_api import ServerApi
|
| 10 |
|
| 11 |
+
# file extension stuff
|
| 12 |
+
import requests
|
| 13 |
+
import tempfile
|
| 14 |
+
from io import BytesIO
|
| 15 |
+
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
openai_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 74 |
print(e)
|
| 75 |
raise gr.Error("An error occurred while saving the prompt to the database.")
|
| 76 |
|
| 77 |
+
# create a temporary file to store the image with extension
|
| 78 |
+
image_response = requests.get(image_url)
|
| 79 |
+
if image_response.status_code == 200:
|
| 80 |
+
# Use a temporary file to automatically clean up after the file is closed
|
| 81 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')
|
| 82 |
+
temp_file.write(image_response.content)
|
| 83 |
+
temp_file.close()
|
| 84 |
+
# return the file with extension for download
|
| 85 |
+
return temp_file.name
|
| 86 |
+
else:
|
| 87 |
+
raise gr.Error("Failed to download the image.")
|
| 88 |
+
|
| 89 |
+
#return image_url
|
| 90 |
|
| 91 |
|
| 92 |
with gr.Blocks() as demo:
|