Update main.py
Browse files
main.py
CHANGED
@@ -19,18 +19,6 @@ app = FastAPI()
|
|
19 |
API_URL = "https://api-inference.huggingface.co/models/ehristoforu/dalle-3-xl"
|
20 |
API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
|
21 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
22 |
-
def generate_image(prompt):
|
23 |
-
try:
|
24 |
-
payload = {"inputs": prompt}
|
25 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
26 |
-
response.raise_for_status() # Raise an exception for HTTP errors
|
27 |
-
image_bytes = response.content
|
28 |
-
image = Image.open(io.BytesIO(image_bytes))
|
29 |
-
return image
|
30 |
-
except Exception as e:
|
31 |
-
# Handle the error gracefully, such as logging the error or returning a default image
|
32 |
-
raise gr.Error(f"Error generating image: {e}")
|
33 |
-
return "Experiencing high demand. Please retry shortly. Thank you for your patience" # Return None or a default image in case of error
|
34 |
|
35 |
|
36 |
|
@@ -76,6 +64,23 @@ def send_it1(inputs, noise_level, proc=proc1):
|
|
76 |
output = proc(prompt_with_noise)
|
77 |
return output
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
def get_prompts(prompt_text):
|
81 |
if not prompt_text:
|
|
|
19 |
API_URL = "https://api-inference.huggingface.co/models/ehristoforu/dalle-3-xl"
|
20 |
API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
|
21 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
|
24 |
|
|
|
64 |
output = proc(prompt_with_noise)
|
65 |
return output
|
66 |
|
67 |
+
def generate_image(prompt_with_noise):
|
68 |
+
try:
|
69 |
+
global request_counter
|
70 |
+
request_counter += 1
|
71 |
+
timestamp = f"{time.time()}_{request_counter}"
|
72 |
+
prompt_with_noise = add_random_noise(inputs, noise_level) + f" - {timestamp}"
|
73 |
+
payload = {"inputs": prompt_with_noise}
|
74 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
75 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
76 |
+
image_bytes = response.content
|
77 |
+
image = Image.open(io.BytesIO(image_bytes))
|
78 |
+
return image
|
79 |
+
except Exception as e:
|
80 |
+
# Handle the error gracefully, such as logging the error or returning a default image
|
81 |
+
raise gr.Error(f"Error generating image: {e}")
|
82 |
+
return "Experiencing high demand. Please retry shortly. Thank you for your patience" # Return None or a default image in case of error
|
83 |
+
|
84 |
|
85 |
def get_prompts(prompt_text):
|
86 |
if not prompt_text:
|