Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
-
import torch
|
3 |
-
from transformers import DalleBartProcessor, DalleBartForConditionalGeneration
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
import cv2
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
model = DalleBartForConditionalGeneration.from_pretrained("dalle-mini/dalle-mini/mega-1-fp16")
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
def image_to_sketch(pil_img):
|
16 |
-
img = np.array(pil_img.convert("L"))
|
17 |
-
inv = 255 - img
|
18 |
blur = cv2.GaussianBlur(inv, (21, 21), 0)
|
19 |
-
sketch = cv2.divide(
|
20 |
return Image.fromarray(sketch)
|
21 |
|
22 |
def generate_sketch(prompt):
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
|
|
|
|
28 |
|
|
|
29 |
sketch = image_to_sketch(image)
|
30 |
return sketch
|
31 |
|
32 |
gr.Interface(
|
33 |
fn=generate_sketch,
|
34 |
-
inputs=gr.Textbox(placeholder="Enter a description
|
35 |
outputs="image",
|
36 |
-
title="Text to Sketch
|
|
|
37 |
).launch()
|
|
|
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()
|