acecalisto3 commited on
Commit
66e3dee
·
verified ·
1 Parent(s): 7be82e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -24
app.py CHANGED
@@ -1,12 +1,13 @@
1
  import gradio as gr
2
  import requests
3
  import os
4
- from PIL import Image
5
  from io import BytesIO
6
  from tqdm import tqdm
7
  import time
8
  import numpy as np
9
  import potrace
 
10
 
11
  # Defining the repository information and the trigger word
12
  repo = "artificialguybr/LineAniRedmond-LinearMangaSDXL-V2"
@@ -27,7 +28,7 @@ constraints = (
27
  def generate_image(prompt):
28
  print("Generating image with prompt:", prompt)
29
  api_url = f"https://api-inference.huggingface.co/models/{repo}"
30
- token = os.getenv("API_TOKEN")
31
  headers = {
32
  "Authorization": f"Bearer {token}"
33
  }
@@ -54,19 +55,19 @@ def generate_image(prompt):
54
  if response.status_code == 200:
55
  print("Image generation successful!")
56
  # Open the image and convert it to grayscale
 
57
  # Convert the grayscale image to a binary image
58
  image = image.point(lambda p: 255 if p > 128 else 0, mode='1')
 
 
59
  # Convert the PIL image to a numpy array
60
  array = np.array(image)
61
- # Invert the array for potrace (black on white)
62
- array = 255 - array
63
  # Trace the bitmap to SVG
64
  bitmap = potrace.Bitmap(array)
65
  path = bitmap.trace()
66
  # Generate SVG data
67
  svg_data = path.to_svg()
68
  return svg_data
69
-
70
  elif response.status_code == 503:
71
  time.sleep(1)
72
  pbar.update(1)
@@ -77,23 +78,31 @@ def generate_image(prompt):
77
  print("API Error:", response.status_code)
78
  raise Exception(f"API Error: {response.status_code}")
79
 
80
- iface = gr.Interface(
81
- fn=generate_image,
82
- inputs=gr.Textbox(lines=2, placeholder="Describe the subject here..."),
83
- outputs="text", # SVG data as text
84
- title="LineArt XL Image Generator",
85
- description=(
86
- "Powered by the generous GPU time from Redmond.AI, this LORA, fine-tuned on SD XL 1.0, excels at creating Lineart-themed images across a wide range of subjects. "
87
- "Optimized for 1024x1024 resolution, it incorporates the specific tag 'lineart,LineAniAF' directly in the HF Space for ease of use. "
88
- "If you appreciate this model and wish to support, consider a donation via [Patreon](https://www.patreon.com/user?u=81570187) or [Ko-fi](https://ko-fi.com/artificialguybr). "
89
- "Stay updated on new models by following on [Twitter](https://twitter.com/artificialguybr)."
90
- ),
91
- examples=[
92
- ["a soaring eagle"],
93
- ["a running cheetah"],
94
- ["a majestic oak tree"]
95
- ]
96
- )
 
 
 
 
 
 
 
 
 
97
 
98
- print("Launching Gradio interface...")
99
- iface.launch()
 
1
  import gradio as gr
2
  import requests
3
  import os
4
+ from PIL import Image, ImageOps
5
  from io import BytesIO
6
  from tqdm import tqdm
7
  import time
8
  import numpy as np
9
  import potrace
10
+ import base64
11
 
12
  # Defining the repository information and the trigger word
13
  repo = "artificialguybr/LineAniRedmond-LinearMangaSDXL-V2"
 
28
  def generate_image(prompt):
29
  print("Generating image with prompt:", prompt)
30
  api_url = f"https://api-inference.huggingface.co/models/{repo}"
31
+ token = os.getenv("HF_TOKEN")
32
  headers = {
33
  "Authorization": f"Bearer {token}"
34
  }
 
55
  if response.status_code == 200:
56
  print("Image generation successful!")
57
  # Open the image and convert it to grayscale
58
+ image = Image.open(BytesIO(response.content)).convert("L")
59
  # Convert the grayscale image to a binary image
60
  image = image.point(lambda p: 255 if p > 128 else 0, mode='1')
61
+ # Invert the image for potrace (black on white)
62
+ image = ImageOps.invert(image)
63
  # Convert the PIL image to a numpy array
64
  array = np.array(image)
 
 
65
  # Trace the bitmap to SVG
66
  bitmap = potrace.Bitmap(array)
67
  path = bitmap.trace()
68
  # Generate SVG data
69
  svg_data = path.to_svg()
70
  return svg_data
 
71
  elif response.status_code == 503:
72
  time.sleep(1)
73
  pbar.update(1)
 
78
  print("API Error:", response.status_code)
79
  raise Exception(f"API Error: {response.status_code}")
80
 
81
+ with gr.Blocks() as demo:
82
+ gr.Markdown("# LineArt XL Image Generator")
83
+ prompt = gr.Textbox(lines=2, placeholder="Describe the subject here...")
84
+ generate_button = gr.Button("Generate Image")
85
+
86
+ with gr.Row():
87
+ svg_display = gr.HTML()
88
+ download_button = gr.Download()
89
+
90
+ def display_svg(svg_data):
91
+ # Encode SVG data in base64 for data URI
92
+ svg_bytes = svg_data.encode('utf-8')
93
+ svg_b64 = base64.b64encode(svg_bytes).decode('utf-8')
94
+ # Create HTML content with an iframe
95
+ html_content = f'<iframe src="data:image/svg+xml;base64,{svg_b64}" width="500" height="500"></iframe>'
96
+ return html_content, svg_bytes
97
+
98
+ generate_button.click(
99
+ generate_image,
100
+ inputs=prompt,
101
+ outputs=None
102
+ ).then(
103
+ display_svg,
104
+ inputs=generate_image,
105
+ outputs=[svg_display, download_button]
106
+ )
107
 
108
+ demo.launch()