Spaces:
Sleeping
Sleeping
Tharunika1601
commited on
Commit
•
adb4a2a
1
Parent(s):
7736ae8
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv # Install the 'python-dotenv' package
|
4 |
+
|
5 |
+
# Load environment variables from a .env file
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
|
9 |
+
API_TOKEN = os.getenv("HF_API_TOKEN") # Use os.getenv to retrieve the API token from environment variables
|
10 |
+
|
11 |
+
if API_TOKEN is None:
|
12 |
+
raise ValueError("HF_API_TOKEN environment variable not set")
|
13 |
+
|
14 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
15 |
+
|
16 |
+
def query(payload):
|
17 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
18 |
+
return response.content
|
19 |
+
|
20 |
+
image_bytes = query({
|
21 |
+
"inputs": "cat is drinking milk ",
|
22 |
+
})
|
23 |
+
|
24 |
+
import io
|
25 |
+
from PIL import Image
|
26 |
+
|
27 |
+
image = Image.open(io.BytesIO(image_bytes))
|
28 |
+
image.show()
|
29 |
+
|
30 |
+
# If you want to save the generated image to a file, you can use the save method
|
31 |
+
image.save("generated_image.png")
|