shiue2000 commited on
Commit
540fae5
·
verified ·
1 Parent(s): 5161bf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -13,7 +13,7 @@ OUTPUT_DIR = "outputs"
13
  os.makedirs(OUTPUT_DIR, exist_ok=True)
14
 
15
  # --- DEVICE SETUP ---
16
- device = "cpu" # Force CPU for safety
17
 
18
  # --- LOAD STABLE DIFFUSION PIPELINE ---
19
  MODEL_NAME = "Lykon/anything-cartoon" # original model
@@ -38,9 +38,13 @@ pipe.to(device)
38
 
39
  # --- HELPER FUNCTIONS ---
40
  def load_image(file):
41
- ext = os.path.splitext(file.name)[1].lower()
42
  if ext in [".heic", ".heif"]:
43
- heif_file = pyheif.read_heif(file.read())
 
 
 
 
44
  image = Image.frombytes(
45
  heif_file.mode,
46
  heif_file.size,
@@ -51,8 +55,11 @@ def load_image(file):
51
  )
52
  return image
53
  else:
54
- file.seek(0)
55
- return Image.open(file)
 
 
 
56
 
57
  def apply_filters(file, mode):
58
  image = load_image(file)
@@ -94,7 +101,7 @@ def apply_filters(file, mode):
94
  elif mode == "AI Cartoon":
95
  pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
96
  prompt = "cartoon, anime style, vibrant colors, detailed, smooth"
97
- # CPU only, no GPU/autocast
98
  cartoon_img = pipe(prompt=prompt, image=pil_img, num_inference_steps=30).images[0]
99
  processed = np.array(cartoon_img)
100
  elif mode == "Sepia":
@@ -124,7 +131,7 @@ def apply_filters(file, mode):
124
  demo = gr.Interface(
125
  fn=apply_filters,
126
  inputs=[
127
- gr.File(label="Upload Image (PNG, JPG, HEIC)", type="file"),
128
  gr.Radio(
129
  choices=["Gray", "Scratch", "Pencil Sketch", "Cartoon", "AI Cartoon",
130
  "Sepia", "Edge Detection", "RGB", "HSV"],
 
13
  os.makedirs(OUTPUT_DIR, exist_ok=True)
14
 
15
  # --- DEVICE SETUP ---
16
+ device = "cpu" # Force CPU
17
 
18
  # --- LOAD STABLE DIFFUSION PIPELINE ---
19
  MODEL_NAME = "Lykon/anything-cartoon" # original model
 
38
 
39
  # --- HELPER FUNCTIONS ---
40
  def load_image(file):
41
+ ext = os.path.splitext(file.name)[1].lower() if hasattr(file, "name") else os.path.splitext(file)[1].lower()
42
  if ext in [".heic", ".heif"]:
43
+ if hasattr(file, "read"):
44
+ heif_file = pyheif.read_heif(file.read())
45
+ else:
46
+ with open(file, "rb") as f:
47
+ heif_file = pyheif.read_heif(f.read())
48
  image = Image.frombytes(
49
  heif_file.mode,
50
  heif_file.size,
 
55
  )
56
  return image
57
  else:
58
+ if hasattr(file, "seek"):
59
+ file.seek(0)
60
+ return Image.open(file)
61
+ else:
62
+ return Image.open(file)
63
 
64
  def apply_filters(file, mode):
65
  image = load_image(file)
 
101
  elif mode == "AI Cartoon":
102
  pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
103
  prompt = "cartoon, anime style, vibrant colors, detailed, smooth"
104
+ # CPU only
105
  cartoon_img = pipe(prompt=prompt, image=pil_img, num_inference_steps=30).images[0]
106
  processed = np.array(cartoon_img)
107
  elif mode == "Sepia":
 
131
  demo = gr.Interface(
132
  fn=apply_filters,
133
  inputs=[
134
+ gr.File(label="Upload Image (PNG, JPG, HEIC)", type="filepath"), # FIXED
135
  gr.Radio(
136
  choices=["Gray", "Scratch", "Pencil Sketch", "Cartoon", "AI Cartoon",
137
  "Sepia", "Edge Detection", "RGB", "HSV"],