Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,23 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
import base64
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from io import BytesIO
|
| 5 |
import gradio as gr
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def generate_image(prompt):
|
| 10 |
-
|
| 11 |
-
response = requests.post(
|
| 12 |
-
"https://api.stability.ai/v1/generation/stable-diffusion-v1-5/text-to-image",
|
| 13 |
-
headers={
|
| 14 |
-
"Authorization": f"Bearer {API_KEY}",
|
| 15 |
-
"Content-Type": "application/json"
|
| 16 |
-
},
|
| 17 |
-
json={
|
| 18 |
-
"text_prompts": [{"text": prompt}],
|
| 19 |
-
"cfg_scale": 7,
|
| 20 |
-
"height": 512,
|
| 21 |
-
"width": 512,
|
| 22 |
-
"samples": 1,
|
| 23 |
-
"steps": 30
|
| 24 |
-
}
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# Step 2: Error check
|
| 28 |
-
if response.status_code != 200:
|
| 29 |
-
return f"Error: {response.status_code} - {response.text}"
|
| 30 |
-
|
| 31 |
-
# Step 3: Convert base64 to PIL.Image
|
| 32 |
-
image_data = response.json()["artifacts"][0]["base64"]
|
| 33 |
-
image = Image.open(BytesIO(base64.b64decode(image_data))).convert("RGB") # Ensure RGB
|
| 34 |
-
|
| 35 |
return image
|
| 36 |
|
| 37 |
-
|
| 38 |
-
demo = gr.Interface(
|
| 39 |
fn=generate_image,
|
| 40 |
-
inputs=gr.Textbox(label="Enter
|
| 41 |
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 42 |
-
title="
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
|
|
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
| 6 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 7 |
+
pipe = pipe.to("cuda")
|
| 8 |
|
| 9 |
def generate_image(prompt):
|
| 10 |
+
image = pipe(prompt).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
return image
|
| 12 |
|
| 13 |
+
iface = gr.Interface(
|
|
|
|
| 14 |
fn=generate_image,
|
| 15 |
+
inputs=gr.Textbox(label="prompt", placeholder="Enter your prompt here..."),
|
| 16 |
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 17 |
+
title="Speech2Image Generator (Fast API Powered)",
|
| 18 |
+
theme="default",
|
| 19 |
)
|
| 20 |
|
| 21 |
+
iface.launch()
|
| 22 |
+
|
| 23 |
|