Update app.py
Browse files
app.py
CHANGED
@@ -4,22 +4,30 @@ import io
|
|
4 |
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 |
-
# Function to query the Hugging Face API
|
13 |
def query(payload):
|
14 |
response = requests.post(API_URL, headers=headers, json=payload)
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def generate_image(prompt):
|
22 |
-
# Generate a random seed to force a new image
|
23 |
random_seed = random.randint(0, 999999)
|
24 |
payload = {
|
25 |
"inputs": prompt,
|
@@ -27,25 +35,37 @@ def generate_image(prompt):
|
|
27 |
"seed": random_seed
|
28 |
}
|
29 |
}
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
image = generate_image(prompt)
|
37 |
-
st.image(image, caption="Generated Image", use_container_width=True)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
image
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
49 |
else:
|
50 |
-
# Show instructions when no parameter is provided
|
51 |
st.info("Add a 'text' parameter to the URL to generate an image. Example: ?text=astronaut+riding+a+horse")
|
|
|
4 |
import random
|
5 |
from PIL import Image
|
6 |
import os
|
7 |
+
import json
|
8 |
|
9 |
# API Configuration
|
10 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
11 |
headers = {"Authorization": f"Bearer {os.getenv('HF')}"}
|
12 |
|
|
|
13 |
def query(payload):
|
14 |
response = requests.post(API_URL, headers=headers, json=payload)
|
15 |
+
|
16 |
+
# Check for API errors
|
17 |
+
if response.status_code != 200:
|
18 |
+
try:
|
19 |
+
error = response.json()
|
20 |
+
return {"error": error.get("error", f"API Error: {response.status_code}")}
|
21 |
+
except:
|
22 |
+
return {"error": f"API Error: {response.status_code} - {response.text}"}
|
23 |
+
|
24 |
+
# Check if response is actually an image
|
25 |
+
if 'image' not in response.headers.get('Content-Type', ''):
|
26 |
+
return {"error": "Unexpected non-image response from API"}
|
27 |
+
|
28 |
+
return {"image": response.content}
|
29 |
|
30 |
def generate_image(prompt):
|
|
|
31 |
random_seed = random.randint(0, 999999)
|
32 |
payload = {
|
33 |
"inputs": prompt,
|
|
|
35 |
"seed": random_seed
|
36 |
}
|
37 |
}
|
38 |
+
|
39 |
+
result = query(payload)
|
40 |
+
|
41 |
+
if "error" in result:
|
42 |
+
st.error(f"API Error: {result['error']}")
|
43 |
+
return None
|
44 |
+
|
45 |
+
try:
|
46 |
+
return Image.open(io.BytesIO(result["image"]))
|
47 |
+
except Exception as e:
|
48 |
+
st.error(f"Failed to process image: {str(e)}")
|
49 |
+
return None
|
50 |
|
51 |
+
# Check for 'text' parameter in URL
|
52 |
+
query_params = st.query_params
|
53 |
+
prompt_from_url = query_params.get('text')
|
|
|
|
|
54 |
|
55 |
+
if prompt_from_url:
|
56 |
+
image = generate_image(prompt_from_url)
|
57 |
+
if image:
|
58 |
+
st.image(image, caption="Generated Image", use_container_width=True)
|
59 |
+
|
60 |
+
# Provide download link
|
61 |
+
img_buffer = io.BytesIO()
|
62 |
+
image.save(img_buffer, format="PNG")
|
63 |
+
img_buffer.seek(0)
|
64 |
+
st.download_button(
|
65 |
+
label="Download Image 📥",
|
66 |
+
data=img_buffer,
|
67 |
+
file_name="generated_image.png",
|
68 |
+
mime="image/png"
|
69 |
+
)
|
70 |
else:
|
|
|
71 |
st.info("Add a 'text' parameter to the URL to generate an image. Example: ?text=astronaut+riding+a+horse")
|