ProfessorLeVesseur commited on
Commit
8b3ed0b
1 Parent(s): bb3e65f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from PIL import Image
4
+ import io
5
+ import os
6
+
7
+ token = os.getenv("HF_TOKEN")
8
+
9
+ API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
10
+ headers = {"Authorization": f"Bearer {token}"}
11
+
12
+ def query(payload):
13
+ response = requests.post(API_URL, headers=headers, json=payload)
14
+ if response.status_code != 200:
15
+ st.error(f"Error: {response.status_code} - {response.text}")
16
+ return None
17
+ return response.content
18
+
19
+ def generate_image(prompt):
20
+ image_bytes = query({"inputs": prompt})
21
+ if image_bytes:
22
+ return Image.open(io.BytesIO(image_bytes))
23
+ return None
24
+
25
+ def main():
26
+ st.title("Stable Diffusion XL 1.0")
27
+
28
+ prompt = st.text_input("Enter a prompt for image generation:")
29
+
30
+ if st.button("Generate Image"):
31
+ if prompt:
32
+ image = generate_image(prompt)
33
+ if image:
34
+ st.image(image, caption="Generated Image")
35
+ else:
36
+ st.warning("Please enter a prompt.")
37
+
38
+ if __name__ == "__main__":
39
+ main()