Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
|
|
4 |
|
5 |
# Get API key from environment variable
|
6 |
api_key = os.environ.get("NVCF_API_KEY")
|
@@ -35,15 +36,30 @@ def generate_image(prompt, negative_prompt, sampler, seed, guidance_scale, infer
|
|
35 |
fetch_url = fetch_url_format + request_id
|
36 |
response = session.get(fetch_url, headers=headers)
|
37 |
|
|
|
38 |
response.raise_for_status()
|
39 |
response_body = response.json()
|
|
|
40 |
# Print the API response for debugging
|
41 |
print("API Response:", response_body)
|
42 |
|
43 |
-
if response_body.get("
|
44 |
-
return "Error: API response does not contain '
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
return image_url
|
48 |
|
49 |
# Create Gradio interface
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
+
import base64
|
5 |
|
6 |
# Get API key from environment variable
|
7 |
api_key = os.environ.get("NVCF_API_KEY")
|
|
|
36 |
fetch_url = fetch_url_format + request_id
|
37 |
response = session.get(fetch_url, headers=headers)
|
38 |
|
39 |
+
|
40 |
response.raise_for_status()
|
41 |
response_body = response.json()
|
42 |
+
|
43 |
# Print the API response for debugging
|
44 |
print("API Response:", response_body)
|
45 |
|
46 |
+
if response_body.get("b64_json") is None:
|
47 |
+
return "Error: API response does not contain 'b64_json' key."
|
48 |
+
|
49 |
+
# Decode the base64-encoded JSON string
|
50 |
+
b64_json_string = response_body.get("b64_json")
|
51 |
+
decoded_json_bytes = base64.b64decode(b64_json_string)
|
52 |
+
decoded_json_string = decoded_json_bytes.decode("utf-8")
|
53 |
+
|
54 |
+
# Load the decoded JSON string into a Python dictionary
|
55 |
+
output_dict = json.loads(decoded_json_string)
|
56 |
+
|
57 |
+
# Extract the image URL from the output dictionary
|
58 |
+
image_url = output_dict.get("output").get("image_url")
|
59 |
+
|
60 |
+
if image_url is None:
|
61 |
+
return "Error: API response does not contain 'image_url' key."
|
62 |
+
|
63 |
return image_url
|
64 |
|
65 |
# Create Gradio interface
|