Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -298,7 +298,6 @@ class UpscalerESRGAN:
|
|
298 |
output = output[:, :, ::-1]
|
299 |
return Image.fromarray(output, "RGB")
|
300 |
|
301 |
-
# https://github.com/philz1337x/clarity-upscaler/blob/e0cd797198d1e0e745400c04d8d1b98ae508c73b/modules/esrgan_model.py#L208
|
302 |
def upscale_with_tiling(self, img: Image.Image) -> Image.Image:
|
303 |
img = img.convert("RGB")
|
304 |
grid = split_grid(img)
|
@@ -521,7 +520,6 @@ def upscaler(
|
|
521 |
log(f'CALL upscaler')
|
522 |
|
523 |
manual_seed(seed)
|
524 |
-
|
525 |
solver_type: type[Solver] = getattr(solvers, solver)
|
526 |
|
527 |
log(f'DBG upscaler 1')
|
@@ -548,7 +546,7 @@ def get_tensor_length(tensor):
|
|
548 |
nums = list(tensor.size())
|
549 |
ret = 1
|
550 |
for num in nums:
|
551 |
-
ret
|
552 |
return ret
|
553 |
|
554 |
def _summarize(text):
|
@@ -596,7 +594,6 @@ def generate_random_string(length):
|
|
596 |
return ''.join(random.choice(characters) for _ in range(length))
|
597 |
|
598 |
def add_text_above_image(img,top_title=None,bottom_title=None):
|
599 |
-
|
600 |
w, h = img.size
|
601 |
draw = ImageDraw.Draw(img,mode="RGBA")
|
602 |
|
@@ -1125,10 +1122,6 @@ class GoogleTranslator(BaseTranslator):
|
|
1125 |
proxies: Optional[dict] = None,
|
1126 |
**kwargs
|
1127 |
):
|
1128 |
-
"""
|
1129 |
-
@param source: source language to translate from
|
1130 |
-
@param target: target language to translate to
|
1131 |
-
"""
|
1132 |
self.proxies = proxies
|
1133 |
super().__init__(
|
1134 |
base_url=google_translate_endpoint,
|
@@ -1136,18 +1129,13 @@ class GoogleTranslator(BaseTranslator):
|
|
1136 |
target=target,
|
1137 |
element_tag="div",
|
1138 |
element_query={"class": "t0"},
|
1139 |
-
payload_key="q",
|
1140 |
**kwargs
|
1141 |
)
|
1142 |
|
1143 |
self._alt_element_query = {"class": "result-container"}
|
1144 |
|
1145 |
def translate(self, text: str, **kwargs) -> str:
|
1146 |
-
"""
|
1147 |
-
function to translate a text
|
1148 |
-
@param text: desired text to translate
|
1149 |
-
@return: str: translated text
|
1150 |
-
"""
|
1151 |
if is_input_valid(text, max_chars=1000):
|
1152 |
text = text.strip()
|
1153 |
if self._same_source_target() or is_empty(text):
|
@@ -1178,12 +1166,8 @@ class GoogleTranslator(BaseTranslator):
|
|
1178 |
raise TranslationNotFound(text)
|
1179 |
|
1180 |
if element.get_text(strip=True) == text.strip():
|
1181 |
-
to_translate_alpha = "".join(
|
1182 |
-
|
1183 |
-
)
|
1184 |
-
translated_alpha = "".join(
|
1185 |
-
ch for ch in element.get_text(strip=True) if ch.isalnum()
|
1186 |
-
)
|
1187 |
if (
|
1188 |
to_translate_alpha
|
1189 |
and translated_alpha
|
@@ -1194,29 +1178,15 @@ class GoogleTranslator(BaseTranslator):
|
|
1194 |
return text.strip()
|
1195 |
del self._url_params["hl"]
|
1196 |
return self.translate(text)
|
1197 |
-
|
1198 |
else:
|
1199 |
return element.get_text(strip=True)
|
1200 |
|
1201 |
def translate_file(self, path: str, **kwargs) -> str:
|
1202 |
-
"""
|
1203 |
-
translate directly from file
|
1204 |
-
@param path: path to the target file
|
1205 |
-
@type path: str
|
1206 |
-
@param kwargs: additional args
|
1207 |
-
@return: str
|
1208 |
-
"""
|
1209 |
return self._translate_file(path, **kwargs)
|
1210 |
|
1211 |
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
1212 |
-
"""
|
1213 |
-
translate a list of texts
|
1214 |
-
@param batch: list of texts you want to translate
|
1215 |
-
@return: list of translations
|
1216 |
-
"""
|
1217 |
return self._translate_batch(batch, **kwargs)
|
1218 |
|
1219 |
-
# End of "Modified parts from https://github.com/nidhaloff/deep-translator"
|
1220 |
|
1221 |
def translate(txt,to_lang="en",from_lang="auto"):
|
1222 |
log(f'CALL translate')
|
@@ -1289,7 +1259,7 @@ def generation(h,w,d):
|
|
1289 |
for line_index in range(len(d_lines)):
|
1290 |
d_lines[line_index] = d_lines[line_index].strip()
|
1291 |
if d_lines[line_index] != "" and re.sub(r'[\.]$', '', d_lines[line_index]) == d_lines[line_index]:
|
1292 |
-
d_lines[line_index]
|
1293 |
d = " ".join(d_lines)
|
1294 |
|
1295 |
d = re.sub(r"([ \t]){1,}", " ", d).lower().strip()
|
@@ -1323,7 +1293,7 @@ def generation(h,w,d):
|
|
1323 |
# entry
|
1324 |
|
1325 |
if __name__ == "__main__":
|
1326 |
-
# Changed the theme to a more colorful one and updated the title
|
1327 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="lime"), css=css) as demo:
|
1328 |
gr.Markdown(f"""
|
1329 |
# Multilingual Images
|
@@ -1352,20 +1322,20 @@ if __name__ == "__main__":
|
|
1352 |
run = gr.Button("Generate", elem_classes="btn")
|
1353 |
|
1354 |
top = gr.Textbox(
|
1355 |
-
placeholder="Top
|
1356 |
value="",
|
1357 |
container=False,
|
1358 |
max_lines=1
|
1359 |
)
|
1360 |
bottom = gr.Textbox(
|
1361 |
-
placeholder="Bottom
|
1362 |
value="",
|
1363 |
container=False,
|
1364 |
max_lines=1
|
1365 |
)
|
1366 |
|
1367 |
data = gr.Textbox(
|
1368 |
-
placeholder="
|
1369 |
value="",
|
1370 |
container=False,
|
1371 |
max_lines=100
|
@@ -1386,23 +1356,23 @@ if __name__ == "__main__":
|
|
1386 |
|
1387 |
gr.Markdown("---")
|
1388 |
|
1389 |
-
# Bottom row explanation or details
|
1390 |
gr.Markdown("""
|
1391 |
-
##
|
1392 |
-
1.
|
1393 |
-
2.
|
1394 |
-
3.
|
1395 |
-
4.
|
1396 |
-
5.
|
1397 |
-
---
|
1398 |
""")
|
1399 |
|
1400 |
gr.Markdown("""
|
1401 |
-
###
|
1402 |
-
1.
|
1403 |
-
2.
|
1404 |
-
3. **Add title(s)**
|
1405 |
-
4.
|
1406 |
""")
|
1407 |
|
1408 |
# Event wiring
|
|
|
298 |
output = output[:, :, ::-1]
|
299 |
return Image.fromarray(output, "RGB")
|
300 |
|
|
|
301 |
def upscale_with_tiling(self, img: Image.Image) -> Image.Image:
|
302 |
img = img.convert("RGB")
|
303 |
grid = split_grid(img)
|
|
|
520 |
log(f'CALL upscaler')
|
521 |
|
522 |
manual_seed(seed)
|
|
|
523 |
solver_type: type[Solver] = getattr(solvers, solver)
|
524 |
|
525 |
log(f'DBG upscaler 1')
|
|
|
546 |
nums = list(tensor.size())
|
547 |
ret = 1
|
548 |
for num in nums:
|
549 |
+
ret *= num
|
550 |
return ret
|
551 |
|
552 |
def _summarize(text):
|
|
|
594 |
return ''.join(random.choice(characters) for _ in range(length))
|
595 |
|
596 |
def add_text_above_image(img,top_title=None,bottom_title=None):
|
|
|
597 |
w, h = img.size
|
598 |
draw = ImageDraw.Draw(img,mode="RGBA")
|
599 |
|
|
|
1122 |
proxies: Optional[dict] = None,
|
1123 |
**kwargs
|
1124 |
):
|
|
|
|
|
|
|
|
|
1125 |
self.proxies = proxies
|
1126 |
super().__init__(
|
1127 |
base_url=google_translate_endpoint,
|
|
|
1129 |
target=target,
|
1130 |
element_tag="div",
|
1131 |
element_query={"class": "t0"},
|
1132 |
+
payload_key="q",
|
1133 |
**kwargs
|
1134 |
)
|
1135 |
|
1136 |
self._alt_element_query = {"class": "result-container"}
|
1137 |
|
1138 |
def translate(self, text: str, **kwargs) -> str:
|
|
|
|
|
|
|
|
|
|
|
1139 |
if is_input_valid(text, max_chars=1000):
|
1140 |
text = text.strip()
|
1141 |
if self._same_source_target() or is_empty(text):
|
|
|
1166 |
raise TranslationNotFound(text)
|
1167 |
|
1168 |
if element.get_text(strip=True) == text.strip():
|
1169 |
+
to_translate_alpha = "".join(ch for ch in text.strip() if ch.isalnum())
|
1170 |
+
translated_alpha = "".join(ch for ch in element.get_text(strip=True) if ch.isalnum())
|
|
|
|
|
|
|
|
|
1171 |
if (
|
1172 |
to_translate_alpha
|
1173 |
and translated_alpha
|
|
|
1178 |
return text.strip()
|
1179 |
del self._url_params["hl"]
|
1180 |
return self.translate(text)
|
|
|
1181 |
else:
|
1182 |
return element.get_text(strip=True)
|
1183 |
|
1184 |
def translate_file(self, path: str, **kwargs) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1185 |
return self._translate_file(path, **kwargs)
|
1186 |
|
1187 |
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
|
|
|
|
|
|
|
|
|
|
1188 |
return self._translate_batch(batch, **kwargs)
|
1189 |
|
|
|
1190 |
|
1191 |
def translate(txt,to_lang="en",from_lang="auto"):
|
1192 |
log(f'CALL translate')
|
|
|
1259 |
for line_index in range(len(d_lines)):
|
1260 |
d_lines[line_index] = d_lines[line_index].strip()
|
1261 |
if d_lines[line_index] != "" and re.sub(r'[\.]$', '', d_lines[line_index]) == d_lines[line_index]:
|
1262 |
+
d_lines[line_index] += "."
|
1263 |
d = " ".join(d_lines)
|
1264 |
|
1265 |
d = re.sub(r"([ \t]){1,}", " ", d).lower().strip()
|
|
|
1293 |
# entry
|
1294 |
|
1295 |
if __name__ == "__main__":
|
1296 |
+
# Changed the theme to a more colorful one and updated the title to English
|
1297 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="lime"), css=css) as demo:
|
1298 |
gr.Markdown(f"""
|
1299 |
# Multilingual Images
|
|
|
1322 |
run = gr.Button("Generate", elem_classes="btn")
|
1323 |
|
1324 |
top = gr.Textbox(
|
1325 |
+
placeholder="Top Title",
|
1326 |
value="",
|
1327 |
container=False,
|
1328 |
max_lines=1
|
1329 |
)
|
1330 |
bottom = gr.Textbox(
|
1331 |
+
placeholder="Bottom Title",
|
1332 |
value="",
|
1333 |
container=False,
|
1334 |
max_lines=1
|
1335 |
)
|
1336 |
|
1337 |
data = gr.Textbox(
|
1338 |
+
placeholder="Enter your text/prompt (multiple languages allowed)",
|
1339 |
value="",
|
1340 |
container=False,
|
1341 |
max_lines=100
|
|
|
1356 |
|
1357 |
gr.Markdown("---")
|
1358 |
|
1359 |
+
# Bottom row explanation or details in English
|
1360 |
gr.Markdown("""
|
1361 |
+
## Features
|
1362 |
+
1. **Text Input**: You can input text in various languages; it will be automatically translated and summarized before generating an image.
|
1363 |
+
2. **Image Size Adjustment**: Use sliders to specify the width and height of the output image.
|
1364 |
+
3. **Overlay Text**: Easily add top/bottom titles to the generated image with a simple button click.
|
1365 |
+
4. **High-Quality Upscaling**: Increase the resolution with the "Upscale x2" feature.
|
1366 |
+
5. **Automatic GPU Resource Management**: The system automatically adjusts GPU usage time depending on input text length and image size.
|
1367 |
+
---
|
1368 |
""")
|
1369 |
|
1370 |
gr.Markdown("""
|
1371 |
+
### Usage Guide
|
1372 |
+
1. Set the desired image dimensions and text prompt, then click **Generate**.
|
1373 |
+
2. After viewing the generated image, you can select **Upscale x2** to improve its resolution.
|
1374 |
+
3. Use **Add title(s)** to place custom titles at the top or bottom of the image.
|
1375 |
+
4. You can test all available features in the interface above.
|
1376 |
""")
|
1377 |
|
1378 |
# Event wiring
|