File size: 1,067 Bytes
f025a7e
56994dc
f025a7e
56994dc
 
f025a7e
56994dc
f025a7e
56994dc
 
 
 
 
 
 
 
 
f025a7e
 
56994dc
 
 
 
f025a7e
 
cb2b794
f025a7e
 
 
 
 
 
56994dc
 
f025a7e
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import streamlit as st
import requests
from PIL import Image
import io
import os

token = os.getenv("HF_TOKEN")

API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
headers = {"Authorization": f"Bearer {token}"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    if response.status_code != 200:
        st.error(f"Error: {response.status_code} - {response.text}")
        return None
    return response.content

def generate_image(prompt):
    image_bytes = query({"inputs": prompt})
    if image_bytes:
        return Image.open(io.BytesIO(image_bytes))
    return None

def main():
    st.title("Stable Diffusion XL 1.0")

    prompt = st.text_input("Enter a prompt for image generation:")
    
    if st.button("Generate Image"):
        if prompt:
            image = generate_image(prompt)
            if image:
                st.image(image, caption="Generated Image")
        else:
            st.warning("Please enter a prompt.")

if __name__ == "__main__":
    main()