Kia09 commited on
Commit
4504f36
·
verified ·
1 Parent(s): b0f8105

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import base64
3
+ from PIL import Image
4
+ from io import BytesIO
5
+ import gradio as gr
6
+ import uuid
7
+ import os
8
+
9
+ API_KEY = "sk-k8c1MQmc6OLzrJRs9zJb9ox3VeLmO4Fho7bQo49uLxTX6OTs"
10
+
11
+ def generate_image(prompt):
12
+ response = requests.post(
13
+ "https://api.stability.ai/v1/generation/stable-diffusion-v1-5/text-to-image",
14
+ headers={
15
+ "Authorization": f"Bearer {API_KEY}",
16
+ "Content-Type": "application/json"
17
+ },
18
+ json={
19
+ "text_prompts": [{"text": prompt}],
20
+ "cfg_scale": 7,
21
+ "height": 512,
22
+ "width": 512,
23
+ "samples": 1,
24
+ "steps": 30
25
+ }
26
+ )
27
+
28
+ if response.status_code != 200:
29
+ return f"Error: {response.status_code} - {response.text}"
30
+
31
+ data = response.json()
32
+ b64_image = data["artifacts"][0]["base64"]
33
+ image_data = base64.b64decode(b64_image)
34
+ image = Image.open(BytesIO(image_data))
35
+
36
+ # Save temporarily
37
+ filename = f"output_{uuid.uuid4().hex[:8]}.png"
38
+ filepath = f"/tmp/{filename}" # Hugging Face allows writing to /tmp
39
+ image.save(filepath)
40
+
41
+ return filepath # return file path, not just image
42
+
43
+ gr.Interface(
44
+ fn=generate_image,
45
+ inputs="text",
46
+ outputs=gr.Image(type="filepath", label="Generated Image"), # <- Important!
47
+ title="Speech2Image Generator (Fast API Powered)",
48
+ ).launch()