Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 12 |
-
inv = 255 - np.array(
|
| 13 |
blur = cv2.GaussianBlur(inv, (21, 21), 0)
|
| 14 |
-
sketch = cv2.divide(np.array(
|
| 15 |
return Image.fromarray(sketch)
|
| 16 |
|
| 17 |
def generate_sketch(prompt):
|
| 18 |
-
full_prompt = prompt + ", pencil sketch, line art, black and white
|
| 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 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
gr.Interface(
|
| 38 |
fn=generate_sketch,
|
| 39 |
-
inputs=gr.Textbox(placeholder="
|
| 40 |
outputs="image",
|
| 41 |
title="Text to Sketch AI",
|
| 42 |
-
description="
|
| 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()
|