Nymbo commited on
Commit
63f8661
·
verified ·
1 Parent(s): 50d2220

Gradio 6.29; local BiRefNet-lite via rembg (remote ysharma Space is dead); fonts via packages.txt; lazy example cache

Browse files
Files changed (4) hide show
  1. README.md +2 -1
  2. app.py +8 -19
  3. packages.txt +1 -0
  4. requirements.txt +3 -0
README.md CHANGED
@@ -4,7 +4,8 @@ emoji: 📈
4
  colorFrom: gray
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 5.5.0
 
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: gray
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 6.29.0
8
+ python_version: "3.12"
9
  app_file: app.py
10
  pinned: false
11
  license: mit
app.py CHANGED
@@ -3,10 +3,11 @@ from PIL import Image, ImageDraw, ImageFont
3
  import numpy as np
4
  import os
5
  from PIL import Image
6
- from gradio_client import Client, handle_file
7
- import uuid
8
 
9
- client = Client("ysharma/BiRefNet_for_text_writing")
 
 
10
 
11
  def add_text_with_stroke(draw, text, x, y, font, text_color, stroke_width):
12
  """Helper function to draw text with stroke"""
@@ -16,12 +17,7 @@ def add_text_with_stroke(draw, text, x, y, font, text_color, stroke_width):
16
  draw.text((x + adj_x, y + adj_y), text, font=font, fill=text_color)
17
 
18
  def remove_background(image):
19
- # Save the image to a specific location
20
- filename = f"image_{uuid.uuid4()}.png" # Generates a universally unique identifier (UUID) for the filename
21
- image.save(filename)
22
- # Call gradio client for background removal
23
- result = client.predict(images=handle_file(filename), api_name="/image")
24
- return Image.open(result[0])
25
 
26
  def superimpose(image_with_text, overlay_image):
27
  # Open image as RGBA to handle transparency
@@ -49,7 +45,7 @@ def add_text_to_image(
49
  if input_image is None:
50
  return None
51
 
52
- image = Image.fromarray(input_image)
53
  # remove background
54
  overlay_image = remove_background(image)
55
 
@@ -214,21 +210,14 @@ def create_interface():
214
  outputs=output_image,
215
  fn=add_text_to_image,
216
  cache_examples=True,
 
217
  )
218
 
219
  return app
220
 
221
  # Launch the app
222
  if __name__ == "__main__":
223
- # Try to install required font
224
- try:
225
- import subprocess
226
- subprocess.run(['apt-get', 'update'])
227
- subprocess.run(['apt-get', 'install', '-y', 'fonts-dejavu'])
228
- print("Font installed successfully")
229
- except:
230
- print("Could not install font automatically. Please install DejaVu font manually.")
231
-
232
  # Create and launch the interface
233
  app = create_interface()
234
  app.launch()
 
3
  import numpy as np
4
  import os
5
  from PIL import Image
6
+ from rembg import new_session, remove
 
7
 
8
+ # The original called the Space ysharma/BiRefNet_for_text_writing, which no longer builds.
9
+ # Same model family (BiRefNet lite), run locally on CPU through rembg's ONNX session.
10
+ session = new_session("birefnet-general-lite")
11
 
12
  def add_text_with_stroke(draw, text, x, y, font, text_color, stroke_width):
13
  """Helper function to draw text with stroke"""
 
17
  draw.text((x + adj_x, y + adj_y), text, font=font, fill=text_color)
18
 
19
  def remove_background(image):
20
+ return remove(image.convert("RGB"), session=session)
 
 
 
 
 
21
 
22
  def superimpose(image_with_text, overlay_image):
23
  # Open image as RGBA to handle transparency
 
45
  if input_image is None:
46
  return None
47
 
48
+ image = Image.fromarray(input_image).convert("RGB")
49
  # remove background
50
  overlay_image = remove_background(image)
51
 
 
210
  outputs=output_image,
211
  fn=add_text_to_image,
212
  cache_examples=True,
213
+ cache_mode="lazy",
214
  )
215
 
216
  return app
217
 
218
  # Launch the app
219
  if __name__ == "__main__":
220
+ # DejaVu fonts come from packages.txt (fonts-dejavu-core); runtime apt-get can't run as non-root.
 
 
 
 
 
 
 
 
221
  # Create and launch the interface
222
  app = create_interface()
223
  app.launch()
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ fonts-dejavu-core
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ rembg[cpu]
2
+ pillow
3
+ numpy