BowoZZZ commited on
Commit
a21a8cf
·
verified ·
1 Parent(s): 3449aac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -9,19 +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
- @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,7 +46,7 @@ else:
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))
 
9
  headers = {"Authorization": f"Bearer {os.getenv('HF')}"}
10
 
11
  # Function to query the Hugging Face API
12
+ @st.cache_data(ttl=60)
13
+ def query(payload):
14
+ response = requests.post(API_URL, headers=headers, json=payload)
15
  return response.content
16
 
17
  # Check for 'text' parameter in URL
18
+ query_params = st.query_params
19
+ prompt_from_url = query_params.get('text')
20
 
21
  if prompt_from_url:
22
  # If 'text' parameter is present, generate and display the image only
23
+ prompt = prompt_from_url
24
+ image_bytes = query({"inputs": 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({"inputs": prompt})
50
 
51
  # Display Image
52
  image = Image.open(io.BytesIO(image_bytes))