Update app.py
Browse files
app.py
CHANGED
@@ -5,50 +5,50 @@ import random
|
|
5 |
from PIL import Image
|
6 |
import os
|
7 |
|
8 |
-
# API Configuration
|
9 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
10 |
headers = {"Authorization": f"Bearer {os.getenv('HF')}"}
|
11 |
|
|
|
12 |
def query(payload):
|
13 |
response = requests.post(API_URL, headers=headers, json=payload)
|
14 |
-
if response.status_code != 200:
|
15 |
-
st.error("Error calling the Hugging Face API.")
|
16 |
-
st.stop()
|
17 |
return response.content
|
18 |
|
19 |
-
def generate_image(prompt
|
20 |
-
#
|
21 |
random_seed = random.randint(0, 999999)
|
22 |
payload = {
|
23 |
"inputs": prompt,
|
24 |
-
"parameters": {
|
|
|
|
|
25 |
}
|
26 |
image_bytes = query(payload)
|
27 |
return Image.open(io.BytesIO(image_bytes))
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
st.write("
|
|
|
5 |
from PIL import Image
|
6 |
import os
|
7 |
|
8 |
+
# API Configuration
|
9 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
10 |
headers = {"Authorization": f"Bearer {os.getenv('HF')}"}
|
11 |
|
12 |
+
# Function to query the Hugging Face API
|
13 |
def query(payload):
|
14 |
response = requests.post(API_URL, headers=headers, json=payload)
|
|
|
|
|
|
|
15 |
return response.content
|
16 |
|
17 |
+
def generate_image(prompt):
|
18 |
+
# Generate a random seed to ensure a new image each time
|
19 |
random_seed = random.randint(0, 999999)
|
20 |
payload = {
|
21 |
"inputs": prompt,
|
22 |
+
"parameters": {
|
23 |
+
"seed": random_seed
|
24 |
+
}
|
25 |
}
|
26 |
image_bytes = query(payload)
|
27 |
return Image.open(io.BytesIO(image_bytes))
|
28 |
|
29 |
+
# Default prompt (used since we're not using a query parameter anymore)
|
30 |
+
default_prompt = "Astronaut riding a horse"
|
31 |
+
|
32 |
+
# Use session_state to avoid regenerating the image on every rerun
|
33 |
+
if "image" not in st.session_state:
|
34 |
+
st.session_state.image = generate_image(default_prompt)
|
35 |
+
|
36 |
+
if st.button("Generate"):
|
37 |
+
st.session_state.image = generate_image(default_prompt)
|
38 |
+
|
39 |
+
# Display the image using the updated parameter
|
40 |
+
image = st.session_state.image
|
41 |
+
st.image(image, caption="Result", use_container_width=True)
|
42 |
+
|
43 |
+
# Prepare image for download
|
44 |
+
img_buffer = io.BytesIO()
|
45 |
+
image.save(img_buffer, format="PNG")
|
46 |
+
img_buffer.seek(0)
|
47 |
+
st.download_button(
|
48 |
+
label="Download",
|
49 |
+
data=img_buffer,
|
50 |
+
file_name="generated_image.png",
|
51 |
+
mime="image/png"
|
52 |
+
)
|
53 |
+
|
54 |
+
st.write("---")
|