Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from huggingface_hub import InferenceApi
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
# Load environment variables from the .env file
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Hugging Face API token
|
| 12 |
+
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 13 |
+
|
| 14 |
+
# Initialize the Hugging Face Inference API
|
| 15 |
+
inference = InferenceApi(repo_id="stabilityai/stable-diffusion-3.5-large", token=HUGGINGFACE_API_TOKEN)
|
| 16 |
+
|
| 17 |
+
# Streamlit App UI
|
| 18 |
+
st.set_page_config(page_title="Stable Diffusion Demo", page_icon="🖼️")
|
| 19 |
+
st.title("Stable Diffusion 3.5 - Text-to-Image")
|
| 20 |
+
|
| 21 |
+
# Text input for the prompt
|
| 22 |
+
prompt = st.text_input("Enter a prompt for the image:")
|
| 23 |
+
|
| 24 |
+
# Button to generate the image
|
| 25 |
+
if st.button("Generate Image"):
|
| 26 |
+
if prompt:
|
| 27 |
+
try:
|
| 28 |
+
# Make request to the Hugging Face model
|
| 29 |
+
output = inference(inputs=prompt)
|
| 30 |
+
|
| 31 |
+
# Convert the output to an image
|
| 32 |
+
image = Image.open(BytesIO(output))
|
| 33 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
| 34 |
+
except Exception as e:
|
| 35 |
+
st.error(f"Error: {str(e)}")
|
| 36 |
+
else:
|
| 37 |
+
st.warning("Please enter a prompt.")
|