Kia09 commited on
Commit
eeb07d1
·
verified ·
1 Parent(s): f5aff3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -35
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
- API_KEY = "sk-ESkTZ28wTlIcedRXuq2KTYbgunw578RkXprkewhJpMsu69Ec" # REPLACE THIS!
 
 
8
 
9
  def generate_image(prompt):
10
- # Step 1: Make the API request
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
- # Step 4: Gradio Interface — PIL Image Return Only
38
- demo = gr.Interface(
39
  fn=generate_image,
40
- inputs=gr.Textbox(label="Enter a prompt"),
41
  outputs=gr.Image(type="pil", label="Generated Image"),
42
- title="Speech2Image Generator "
 
43
  )
44
 
45
- demo.launch()
 
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