Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,7 +48,7 @@ class Prodia:
|
|
| 48 |
response = requests.post(url, headers=headers, data=json.dumps(params))
|
| 49 |
|
| 50 |
if response.status_code != 200:
|
| 51 |
-
raise Exception(f"Bad Prodia Response: {response.status_code}")
|
| 52 |
|
| 53 |
return response
|
| 54 |
|
|
@@ -56,42 +56,47 @@ class Prodia:
|
|
| 56 |
response = requests.get(url, headers=self.headers)
|
| 57 |
|
| 58 |
if response.status_code != 200:
|
| 59 |
-
raise Exception(f"Bad Prodia Response: {response.status_code}")
|
| 60 |
|
| 61 |
return response
|
| 62 |
|
| 63 |
-
|
| 64 |
def image_to_base64(image_path):
|
| 65 |
-
# Open the image with PIL
|
| 66 |
with Image.open(image_path) as image:
|
| 67 |
-
# Convert the image to bytes
|
| 68 |
buffered = BytesIO()
|
| 69 |
-
image.save(buffered, format="PNG")
|
| 70 |
-
|
| 71 |
-
# Encode the bytes to base64
|
| 72 |
img_str = base64.b64encode(buffered.getvalue())
|
|
|
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
prodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))
|
| 78 |
|
| 79 |
def flip_text(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
css = """
|
| 97 |
/* Overall Styling */
|
|
@@ -132,7 +137,7 @@ body {
|
|
| 132 |
|
| 133 |
/* Button Styling */
|
| 134 |
#generate {
|
| 135 |
-
background-color: #007bff;
|
| 136 |
color: white;
|
| 137 |
padding: 15px 25px;
|
| 138 |
border: none;
|
|
@@ -141,10 +146,10 @@ body {
|
|
| 141 |
}
|
| 142 |
|
| 143 |
#generate:hover {
|
| 144 |
-
background-color: #0056b3;
|
| 145 |
}
|
| 146 |
|
| 147 |
-
/* Responsive Design
|
| 148 |
@media screen and (max-width: 768px) {
|
| 149 |
#settings {
|
| 150 |
grid-template-columns: 1fr;
|
|
@@ -152,10 +157,8 @@ body {
|
|
| 152 |
}
|
| 153 |
"""
|
| 154 |
|
| 155 |
-
# --- Gradio Interface ---
|
| 156 |
-
|
| 157 |
with gr.Blocks(css=css) as demo:
|
| 158 |
-
state = gr.State(value="Welcome Screen")
|
| 159 |
|
| 160 |
with gr.Tabs() as tabs:
|
| 161 |
with gr.TabItem("Welcome Screen"):
|
|
|
|
| 48 |
response = requests.post(url, headers=headers, data=json.dumps(params))
|
| 49 |
|
| 50 |
if response.status_code != 200:
|
| 51 |
+
raise Exception(f"Bad Prodia Response: {response.status_code} - {response.text}")
|
| 52 |
|
| 53 |
return response
|
| 54 |
|
|
|
|
| 56 |
response = requests.get(url, headers=self.headers)
|
| 57 |
|
| 58 |
if response.status_code != 200:
|
| 59 |
+
raise Exception(f"Bad Prodia Response: {response.status_code} - {response.text}")
|
| 60 |
|
| 61 |
return response
|
| 62 |
|
|
|
|
| 63 |
def image_to_base64(image_path):
|
|
|
|
| 64 |
with Image.open(image_path) as image:
|
|
|
|
| 65 |
buffered = BytesIO()
|
| 66 |
+
image.save(buffered, format="PNG")
|
|
|
|
|
|
|
| 67 |
img_str = base64.b64encode(buffered.getvalue())
|
| 68 |
+
return img_str.decode('utf-8')
|
| 69 |
|
| 70 |
+
# Ensure that the API key is available
|
| 71 |
+
api_key = os.getenv("PRODIA_API_KEY")
|
| 72 |
+
if not api_key:
|
| 73 |
+
raise ValueError("Prodia API key not found in environment variables")
|
| 74 |
|
| 75 |
+
prodia_client = Prodia(api_key=api_key)
|
|
|
|
| 76 |
|
| 77 |
def flip_text(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
|
| 78 |
+
try:
|
| 79 |
+
result = prodia_client.generate({
|
| 80 |
+
"prompt": prompt,
|
| 81 |
+
"negative_prompt": negative_prompt,
|
| 82 |
+
"model": model,
|
| 83 |
+
"steps": steps,
|
| 84 |
+
"sampler": sampler,
|
| 85 |
+
"cfg_scale": cfg_scale,
|
| 86 |
+
"width": width,
|
| 87 |
+
"height": height,
|
| 88 |
+
"seed": seed
|
| 89 |
+
})
|
| 90 |
+
|
| 91 |
+
job = prodia_client.wait(result)
|
| 92 |
+
|
| 93 |
+
if job['status'] == 'succeeded':
|
| 94 |
+
return job["imageUrl"]
|
| 95 |
+
else:
|
| 96 |
+
return "Generation failed, please try again."
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
return f"An error occurred: {e}"
|
| 100 |
|
| 101 |
css = """
|
| 102 |
/* Overall Styling */
|
|
|
|
| 137 |
|
| 138 |
/* Button Styling */
|
| 139 |
#generate {
|
| 140 |
+
background-color: #007bff;
|
| 141 |
color: white;
|
| 142 |
padding: 15px 25px;
|
| 143 |
border: none;
|
|
|
|
| 146 |
}
|
| 147 |
|
| 148 |
#generate:hover {
|
| 149 |
+
background-color: #0056b3;
|
| 150 |
}
|
| 151 |
|
| 152 |
+
/* Responsive Design */
|
| 153 |
@media screen and (max-width: 768px) {
|
| 154 |
#settings {
|
| 155 |
grid-template-columns: 1fr;
|
|
|
|
| 157 |
}
|
| 158 |
"""
|
| 159 |
|
|
|
|
|
|
|
| 160 |
with gr.Blocks(css=css) as demo:
|
| 161 |
+
state = gr.State(value="Welcome Screen")
|
| 162 |
|
| 163 |
with gr.Tabs() as tabs:
|
| 164 |
with gr.TabItem("Welcome Screen"):
|