Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,193 +1,165 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
import
|
7 |
-
import
|
8 |
-
import
|
9 |
-
import
|
10 |
-
import
|
11 |
-
|
12 |
-
# Load environment variables
|
13 |
-
load_dotenv()
|
14 |
-
|
15 |
-
# Set up logging
|
16 |
-
|
17 |
-
logging.
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
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 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
st.
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
st.
|
157 |
-
|
158 |
-
|
159 |
-
if
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
text_to_remove = st.text_input("Text to Remove")
|
166 |
-
new_text = st.text_input("New Text")
|
167 |
-
font_size = st.number_input("Font Size", min_value=1, max_value=100, value=40)
|
168 |
-
font_color = st.color_picker("Font Color", "#FFFFFF")
|
169 |
-
|
170 |
-
if st.button("Apply Changes"):
|
171 |
-
position = None
|
172 |
-
size = None
|
173 |
-
if text_to_remove:
|
174 |
-
img, position, size = remove_text_from_image(img, text_to_remove)
|
175 |
-
|
176 |
-
if new_text:
|
177 |
-
img = add_text_to_image(img, new_text, font_size, font_color, position, size)
|
178 |
-
|
179 |
-
st.image(img, caption="Edited Image", use_column_width=True)
|
180 |
-
|
181 |
-
# Save edited image for download
|
182 |
-
img_byte_arr = io.BytesIO()
|
183 |
-
img.save(img_byte_arr, format='PNG')
|
184 |
-
img_byte_arr = img_byte_arr.getvalue()
|
185 |
-
st.download_button(
|
186 |
-
label="Download Edited Image",
|
187 |
-
data=img_byte_arr,
|
188 |
-
file_name="edited_poster.png",
|
189 |
-
mime="image/png"
|
190 |
-
)
|
191 |
-
|
192 |
-
if __name__ == "__main__":
|
193 |
main()
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image, ImageDraw, ImageFont
|
3 |
+
import io
|
4 |
+
import cv2
|
5 |
+
import numpy as np
|
6 |
+
import easyocr
|
7 |
+
import os
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
import requests
|
10 |
+
import logging
|
11 |
+
|
12 |
+
# Load environment variables
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
# Set up logging
|
16 |
+
logging.basicConfig(level=logging.DEBUG)
|
17 |
+
logger = logging.getLogger(__name__)
|
18 |
+
|
19 |
+
# Hugging Face API setup
|
20 |
+
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
21 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
22 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
23 |
+
|
24 |
+
def load_image(image_file):
|
25 |
+
img = Image.open(image_file)
|
26 |
+
return img
|
27 |
+
|
28 |
+
def detect_text(image):
|
29 |
+
reader = easyocr.Reader(['en'])
|
30 |
+
img_array = np.array(image)
|
31 |
+
results = reader.readtext(img_array)
|
32 |
+
return [(text, box) for (box, text, _) in results]
|
33 |
+
|
34 |
+
def replace_text_in_image(image, text_to_replace, new_text):
|
35 |
+
img_array = np.array(image)
|
36 |
+
for (text, box) in detect_text(image):
|
37 |
+
if text == text_to_replace:
|
38 |
+
x, y, w, h = int(box[0][0]), int(box[0][1]), int(box[2][0] - box[0][0]), int(box[2][1] - box[0][1])
|
39 |
+
mask = np.zeros(img_array.shape[:2], dtype=np.uint8)
|
40 |
+
cv2.rectangle(mask, (x, y), (x+w, y+h), 255, -1)
|
41 |
+
img_array = cv2.inpaint(img_array, mask, 3, cv2.INPAINT_TELEA)
|
42 |
+
image = Image.fromarray(img_array)
|
43 |
+
draw = ImageDraw.Draw(image)
|
44 |
+
font = ImageFont.truetype("arial.ttf", 40)
|
45 |
+
draw.text((x, y), new_text, font=font, fill="#000000")
|
46 |
+
return image
|
47 |
+
return Image.fromarray(img_array)
|
48 |
+
|
49 |
+
def query(payload):
|
50 |
+
try:
|
51 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
52 |
+
response.raise_for_status()
|
53 |
+
|
54 |
+
logger.debug(f"API response status code: {response.status_code}")
|
55 |
+
logger.debug(f"API response headers: {response.headers}")
|
56 |
+
|
57 |
+
content_type = response.headers.get('Content-Type', '')
|
58 |
+
if 'application/json' in content_type:
|
59 |
+
return response.json()
|
60 |
+
elif 'image' in content_type:
|
61 |
+
return response.content
|
62 |
+
else:
|
63 |
+
logger.error(f"Unexpected content type: {content_type}")
|
64 |
+
st.error(f"Unexpected content type: {content_type}")
|
65 |
+
return None
|
66 |
+
except requests.exceptions.RequestException as e:
|
67 |
+
logger.error(f"Request failed: {str(e)}")
|
68 |
+
st.error(f"Request failed: {str(e)}")
|
69 |
+
return None
|
70 |
+
|
71 |
+
def increase_image_quality(image, scale_factor):
|
72 |
+
width, height = image.size
|
73 |
+
new_size = (width * scale_factor, height * scale_factor)
|
74 |
+
return image.resize(new_size, Image.LANCZOS)
|
75 |
+
|
76 |
+
def image_text_replacer():
|
77 |
+
st.header("Image Text Replacer")
|
78 |
+
|
79 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
80 |
+
if uploaded_file is not None:
|
81 |
+
image = load_image(uploaded_file)
|
82 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
83 |
+
|
84 |
+
text_results = detect_text(image)
|
85 |
+
|
86 |
+
st.subheader("Detected Text:")
|
87 |
+
|
88 |
+
edited_image = image.copy()
|
89 |
+
|
90 |
+
for i, (text, box) in enumerate(text_results):
|
91 |
+
if text.strip(): # Only process non-empty text
|
92 |
+
st.text(f"{i+1}. {text}")
|
93 |
+
|
94 |
+
new_text = st.text_input(f"Enter new text for '{text}':", value=text, key=f"new_text_{i}")
|
95 |
+
|
96 |
+
if st.button(f"Replace '{text}'", key=f"replace_{i}"):
|
97 |
+
edited_image = replace_text_in_image(edited_image, text, new_text)
|
98 |
+
st.image(edited_image, caption='Edited Image', use_column_width=True)
|
99 |
+
|
100 |
+
# Provide download option for the edited image
|
101 |
+
buf = io.BytesIO()
|
102 |
+
edited_image.save(buf, format="PNG")
|
103 |
+
byte_im = buf.getvalue()
|
104 |
+
st.download_button(
|
105 |
+
label="Download edited image",
|
106 |
+
data=byte_im,
|
107 |
+
file_name="edited_image.png",
|
108 |
+
mime="image/png"
|
109 |
+
)
|
110 |
+
|
111 |
+
def poster_generator():
|
112 |
+
st.header("Generate Poster")
|
113 |
+
poster_type = st.selectbox("Poster Type", ["Fashion", "Movie", "Event", "Advertisement", "Other"])
|
114 |
+
prompt = st.text_area("Prompt")
|
115 |
+
num_images = st.number_input("Number of Images", min_value=1, max_value=5, value=1)
|
116 |
+
quality_factor = st.number_input("Quality Factor", min_value=1, max_value=4, value=1)
|
117 |
+
|
118 |
+
if st.button("Generate Images"):
|
119 |
+
if poster_type == "Other":
|
120 |
+
full_prompt = f"A colorful poster with the following elements: {prompt}"
|
121 |
+
else:
|
122 |
+
full_prompt = f"A colorful {poster_type.lower()} poster with the following elements: {prompt}"
|
123 |
+
|
124 |
+
generated_images = []
|
125 |
+
for i in range(num_images):
|
126 |
+
with st.spinner(f"Generating image {i+1}..."):
|
127 |
+
logger.info(f"Generating image {i+1} with prompt: {full_prompt}")
|
128 |
+
response = query({"inputs": full_prompt})
|
129 |
+
|
130 |
+
if isinstance(response, bytes):
|
131 |
+
image = Image.open(io.BytesIO(response))
|
132 |
+
if quality_factor > 1:
|
133 |
+
image = increase_image_quality(image, quality_factor)
|
134 |
+
generated_images.append(image)
|
135 |
+
else:
|
136 |
+
st.error("Failed to generate image")
|
137 |
+
|
138 |
+
# Display generated images
|
139 |
+
for i, img in enumerate(generated_images):
|
140 |
+
st.image(img, caption=f"Generated Poster {i+1}", use_column_width=True)
|
141 |
+
|
142 |
+
# Provide download option for the generated image
|
143 |
+
buf = io.BytesIO()
|
144 |
+
img.save(buf, format="PNG")
|
145 |
+
byte_im = buf.getvalue()
|
146 |
+
st.download_button(
|
147 |
+
label=f"Download generated poster {i+1}",
|
148 |
+
data=byte_im,
|
149 |
+
file_name=f"generated_poster_{i+1}.png",
|
150 |
+
mime="image/png"
|
151 |
+
)
|
152 |
+
|
153 |
+
def main():
|
154 |
+
st.title("Image Processing App")
|
155 |
+
|
156 |
+
app_mode = st.sidebar.selectbox("Choose the app mode",
|
157 |
+
["Image Text Replacer", "Poster Generator"])
|
158 |
+
|
159 |
+
if app_mode == "Image Text Replacer":
|
160 |
+
image_text_replacer()
|
161 |
+
elif app_mode == "Poster Generator":
|
162 |
+
poster_generator()
|
163 |
+
|
164 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
main()
|