Update app.py
Browse files
app.py
CHANGED
@@ -9,18 +9,19 @@ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-
|
|
9 |
headers = {"Authorization": f"Bearer {os.getenv('HF')}"}
|
10 |
|
11 |
# Function to query the Hugging Face API
|
12 |
-
|
13 |
-
|
|
|
14 |
return response.content
|
15 |
|
16 |
# Check for 'text' parameter in URL
|
17 |
-
query_params = st.
|
18 |
-
prompt_from_url = query_params.get('text')
|
19 |
|
20 |
if prompt_from_url:
|
21 |
# If 'text' parameter is present, generate and display the image only
|
22 |
-
prompt = prompt_from_url
|
23 |
-
image_bytes =
|
24 |
image = Image.open(io.BytesIO(image_bytes))
|
25 |
st.image(image, caption="Generated Image", use_container_width=True)
|
26 |
|
@@ -45,7 +46,7 @@ else:
|
|
45 |
if st.button("Generate Image"):
|
46 |
if prompt:
|
47 |
st.write("Generating image... Please wait ⏳")
|
48 |
-
image_bytes =
|
49 |
|
50 |
# Display Image
|
51 |
image = Image.open(io.BytesIO(image_bytes))
|
|
|
9 |
headers = {"Authorization": f"Bearer {os.getenv('HF')}"}
|
10 |
|
11 |
# Function to query the Hugging Face API
|
12 |
+
@st.cache_data(ttl=300) # Cache the function's output for 300 seconds (5 minutes)
|
13 |
+
def query_image(prompt):
|
14 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
|
15 |
return response.content
|
16 |
|
17 |
# Check for 'text' parameter in URL
|
18 |
+
query_params = st.experimental_get_query_params()
|
19 |
+
prompt_from_url = query_params.get('text', None)
|
20 |
|
21 |
if prompt_from_url:
|
22 |
# If 'text' parameter is present, generate and display the image only
|
23 |
+
prompt = prompt_from_url[0]
|
24 |
+
image_bytes = query_image(prompt)
|
25 |
image = Image.open(io.BytesIO(image_bytes))
|
26 |
st.image(image, caption="Generated Image", use_container_width=True)
|
27 |
|
|
|
46 |
if st.button("Generate Image"):
|
47 |
if prompt:
|
48 |
st.write("Generating image... Please wait ⏳")
|
49 |
+
image_bytes = query_image(prompt)
|
50 |
|
51 |
# Display Image
|
52 |
image = Image.open(io.BytesIO(image_bytes))
|