MINEOGO commited on
Commit
40924fd
verified
1 Parent(s): e04d49e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -23
app.py CHANGED
@@ -1,43 +1,42 @@
1
  import gradio as gr
 
2
  from PIL import Image
 
3
  import numpy as np
4
  import cv2
5
- import requests
6
 
7
- # Use the Hugging Face inference API for DALL路E Mini (public access, no token required)
8
  API_URL = "https://api-inference.huggingface.co/models/dalle-mini/dalle-mini/mega-1-fp16"
9
 
10
  def image_to_sketch(image):
11
- image = image.convert("L") # convert to grayscale
12
- inv = 255 - np.array(image)
13
  blur = cv2.GaussianBlur(inv, (21, 21), 0)
14
- sketch = cv2.divide(np.array(image), 255 - blur, scale=256)
15
  return Image.fromarray(sketch)
16
 
17
  def generate_sketch(prompt):
18
- full_prompt = prompt + ", pencil sketch, line art, black and white, minimal"
19
  response = requests.post(API_URL, json={"inputs": full_prompt})
20
-
21
- if response.status_code != 200:
22
- return f"Error generating image: {response.status_code}"
23
-
24
- output = response.json()
25
- if isinstance(output, dict) and output.get("error"):
26
- return f"API Error: {output['error']}"
27
-
28
- # Grab image URL
29
- image_url = output[0]['generated_image']
30
- image_response = requests.get(image_url)
31
- image = Image.open(BytesIO(image_response.content))
32
 
33
- # Convert to sketch
34
- sketch = image_to_sketch(image)
35
- return sketch
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  gr.Interface(
38
  fn=generate_sketch,
39
- inputs=gr.Textbox(placeholder="Enter a description like 'a robot on a bike'"),
40
  outputs="image",
41
  title="Text to Sketch AI",
42
- description="This app turns your text prompts into pencil sketch-style images using DALL路E Mini + sketch filter."
43
  ).launch()
 
1
  import gradio as gr
2
+ import requests
3
  from PIL import Image
4
+ from io import BytesIO
5
  import numpy as np
6
  import cv2
 
7
 
 
8
  API_URL = "https://api-inference.huggingface.co/models/dalle-mini/dalle-mini/mega-1-fp16"
9
 
10
  def image_to_sketch(image):
11
+ gray = image.convert("L")
12
+ inv = 255 - np.array(gray)
13
  blur = cv2.GaussianBlur(inv, (21, 21), 0)
14
+ sketch = cv2.divide(np.array(gray), 255 - blur, scale=256)
15
  return Image.fromarray(sketch)
16
 
17
  def generate_sketch(prompt):
18
+ full_prompt = prompt.strip() + ", pencil sketch, line art, black and white"
19
  response = requests.post(API_URL, json={"inputs": full_prompt})
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ try:
22
+ output = response.json()
23
+ if isinstance(output, dict) and output.get("error"):
24
+ return f"API Error: {output['error']}"
25
+ # Newer inference endpoints sometimes return `images` key
26
+ if isinstance(output, list) and "generated_image" in output[0]:
27
+ img_url = output[0]["generated_image"]
28
+ img_data = requests.get(img_url).content
29
+ image = Image.open(BytesIO(img_data))
30
+ return image_to_sketch(image)
31
+ else:
32
+ return "Unexpected response format. Try again later."
33
+ except Exception as e:
34
+ return f"Error: {str(e)}"
35
 
36
  gr.Interface(
37
  fn=generate_sketch,
38
+ inputs=gr.Textbox(placeholder="e.g. a wizard fighting a dragon"),
39
  outputs="image",
40
  title="Text to Sketch AI",
41
+ description="Type a description, and get a sketch-style image using DALL路E Mini + OpenCV."
42
  ).launch()