Update app.py
Browse files
app.py
CHANGED
@@ -13,37 +13,59 @@ def query(payload):
|
|
13 |
response = requests.post(API_URL, headers=headers, json=payload)
|
14 |
return response.content
|
15 |
|
16 |
-
#
|
17 |
-
st.
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
img_buffer
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
response = requests.post(API_URL, headers=headers, json=payload)
|
14 |
return response.content
|
15 |
|
16 |
+
# Check for 'text' parameter in URL
|
17 |
+
query_params = st.experimental_get_query_params()
|
18 |
+
prompt_from_url = query_params.get('text', None)
|
19 |
+
|
20 |
+
if prompt_from_url:
|
21 |
+
# If 'text' parameter is present, generate and display the image only
|
22 |
+
prompt = prompt_from_url[0]
|
23 |
+
image_bytes = query({"inputs": prompt})
|
24 |
+
image = Image.open(io.BytesIO(image_bytes))
|
25 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
26 |
+
|
27 |
+
# Provide download link
|
28 |
+
img_buffer = io.BytesIO()
|
29 |
+
image.save(img_buffer, format="PNG")
|
30 |
+
img_buffer.seek(0)
|
31 |
+
st.download_button(
|
32 |
+
label="Download Image π₯",
|
33 |
+
data=img_buffer,
|
34 |
+
file_name="generated_image.png",
|
35 |
+
mime="image/png"
|
36 |
+
)
|
37 |
+
else:
|
38 |
+
# Standard interface with prompt input
|
39 |
+
st.title("AI Image Generator π¨π")
|
40 |
+
st.write("Enter a prompt below and generate an AI-generated image using Hugging Face!")
|
41 |
+
|
42 |
+
# User Input
|
43 |
+
prompt = st.text_input("Enter your prompt:", "Astronaut riding a horse")
|
44 |
+
|
45 |
+
if st.button("Generate Image"):
|
46 |
+
if prompt:
|
47 |
+
st.write("Generating image... Please wait β³")
|
48 |
+
image_bytes = query({"inputs": prompt})
|
49 |
+
|
50 |
+
# Display Image
|
51 |
+
image = Image.open(io.BytesIO(image_bytes))
|
52 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
53 |
+
|
54 |
+
# Convert Image to Bytes for Download
|
55 |
+
img_buffer = io.BytesIO()
|
56 |
+
image.save(img_buffer, format="PNG")
|
57 |
+
img_buffer.seek(0)
|
58 |
+
|
59 |
+
# Download Button
|
60 |
+
st.download_button(
|
61 |
+
label="Download Image π₯",
|
62 |
+
data=img_buffer,
|
63 |
+
file_name="generated_image.png",
|
64 |
+
mime="image/png"
|
65 |
+
)
|
66 |
+
else:
|
67 |
+
st.warning("Please enter a prompt before generating an image.")
|
68 |
+
|
69 |
+
# Footer
|
70 |
+
st.write("---")
|
71 |
+
st.write("Powered by [Hugging Face](https://huggingface.co/) π")
|