Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import io
|
4 |
+
from PIL import Image
|
5 |
+
import base64
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Use environment variable for API key
|
9 |
+
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
10 |
+
|
11 |
+
def get_api_key():
|
12 |
+
"""Retrieve API key from environment variable or Streamlit secrets"""
|
13 |
+
# First, try to get from environment variable
|
14 |
+
api_key = os.getenv('HUGGINGFACE_API_KEY')
|
15 |
+
|
16 |
+
# If not found, try Streamlit secrets
|
17 |
+
if not api_key:
|
18 |
+
try:
|
19 |
+
api_key = st.secrets["HUGGINGFACE_API_KEY"]
|
20 |
+
except Exception:
|
21 |
+
st.error("API Key not found. Please configure your API key.")
|
22 |
+
return None
|
23 |
+
|
24 |
+
return api_key
|
25 |
+
|
26 |
+
# Lego Minifigurine Style
|
27 |
+
style_list = [
|
28 |
+
{
|
29 |
+
"name": "lego minifigurine",
|
30 |
+
"prompt": "legoman, lego minifigurine, lego universe, lego, lego figurine, lego style, lego-man",
|
31 |
+
"negative_prompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white",
|
32 |
+
}
|
33 |
+
]
|
34 |
+
|
35 |
+
def query_image_generation(image_bytes):
|
36 |
+
"""Generate Lego minifigurine style image using Hugging Face API"""
|
37 |
+
# Get API key
|
38 |
+
api_key = get_api_key()
|
39 |
+
if not api_key:
|
40 |
+
st.error("Cannot proceed without API key")
|
41 |
+
return None
|
42 |
+
|
43 |
+
# Prepare headers with API key
|
44 |
+
headers = {"Authorization": f"Bearer {api_key}"}
|
45 |
+
|
46 |
+
payload = {
|
47 |
+
"inputs": style_list[0]["prompt"],
|
48 |
+
"negative_prompt": style_list[0]["negative_prompt"],
|
49 |
+
"image": base64.b64encode(image_bytes).decode('utf-8')
|
50 |
+
}
|
51 |
+
|
52 |
+
try:
|
53 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
54 |
+
response.raise_for_status()
|
55 |
+
return response.content
|
56 |
+
except requests.exceptions.RequestException as e:
|
57 |
+
st.error(f"API request failed: {e}")
|
58 |
+
return None
|
59 |
+
|
60 |
+
def main():
|
61 |
+
st.title("🧱 Lego Minifigurine Image Generator")
|
62 |
+
st.write("Upload an image to transform it into a Lego minifigurine style!")
|
63 |
+
|
64 |
+
# Image upload
|
65 |
+
uploaded_file = st.file_uploader(
|
66 |
+
"Choose an image...",
|
67 |
+
type=["jpg", "jpeg", "png"],
|
68 |
+
help="Upload an image to convert to Lego style"
|
69 |
+
)
|
70 |
+
|
71 |
+
if uploaded_file is not None:
|
72 |
+
# Display original image
|
73 |
+
original_image = Image.open(uploaded_file)
|
74 |
+
st.subheader("Original Image")
|
75 |
+
st.image(original_image, use_column_width=True)
|
76 |
+
|
77 |
+
# Convert image to bytes
|
78 |
+
img_byte_arr = io.BytesIO()
|
79 |
+
original_image.save(img_byte_arr, format='PNG')
|
80 |
+
img_byte_arr = img_byte_arr.getvalue()
|
81 |
+
|
82 |
+
# Generate button
|
83 |
+
if st.button("Generate Lego Style"):
|
84 |
+
with st.spinner('Generating Lego Minifigurine...'):
|
85 |
+
generated_image_bytes = query_image_generation(img_byte_arr)
|
86 |
+
|
87 |
+
if generated_image_bytes:
|
88 |
+
# Display generated image
|
89 |
+
st.subheader("Lego Minifigurine Style")
|
90 |
+
generated_image = Image.open(io.BytesIO(generated_image_bytes))
|
91 |
+
st.image(generated_image, use_column_width=True)
|
92 |
+
|
93 |
+
# Download button
|
94 |
+
buffered = io.BytesIO()
|
95 |
+
generated_image.save(buffered, format="PNG")
|
96 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
97 |
+
href = f'<a href="data:image/png;base64,{img_str}" download="lego_minifigurine.png">Download Lego Image</a>'
|
98 |
+
st.markdown(href, unsafe_allow_html=True)
|
99 |
+
else:
|
100 |
+
st.error("Failed to generate image. Please try again.")
|
101 |
+
|
102 |
+
if __name__ == "__main__":
|
103 |
+
main()
|