JerryCoder commited on
Commit
2ce5586
·
verified ·
1 Parent(s): 4218e9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -67
app.py CHANGED
@@ -1,12 +1,17 @@
1
- import io, requests, json
2
- import gradio as gr
 
 
3
  from rembg import remove
4
  from PIL import Image
 
5
 
6
- # -----------------
7
- # Core background remover
8
- # -----------------
9
- def process_image_bytes(img_bytes: bytes):
 
 
10
  # Remove background
11
  output_bytes = remove(
12
  img_bytes,
@@ -16,77 +21,43 @@ def process_image_bytes(img_bytes: bytes):
16
  alpha_matting_erode_size=10
17
  )
18
 
19
- # Upload to ar-media hosting
20
  files = {"file": ("output.png", output_bytes, "image/png")}
21
  r = requests.post("https://ar-hosting.pages.dev/upload", files=files)
22
  r.raise_for_status()
23
  data = r.json()
24
 
25
  if "url" in data:
26
- return output_bytes, data["url"]
27
-
28
- raise Exception("Upload failed")
29
-
30
- # -----------------
31
- # Gradio function (UI)
32
- # -----------------
33
- def remove_and_upload(image: Image.Image):
34
  try:
35
- with io.BytesIO() as buffered:
36
- image.save(buffered, format="PNG", optimize=True)
37
- img_bytes = buffered.getvalue()
38
-
39
- output_bytes, hosted_url = process_image_bytes(img_bytes)
40
-
41
- return (
42
- Image.open(io.BytesIO(output_bytes)).convert("RGBA"),
43
- json.dumps({"success": True, "url": hosted_url}, indent=2)
44
- )
45
-
46
  except Exception as e:
47
- return None, json.dumps({"error": str(e)}, indent=2)
48
 
49
- # -----------------
50
- # API-friendly function (?url=)
51
- # -----------------
52
- def api_json(url: str):
 
53
  try:
54
- resp = requests.get(url, timeout=10)
55
  resp.raise_for_status()
56
- img_bytes = resp.content
57
-
58
- _, hosted_url = process_image_bytes(img_bytes)
59
-
60
- return {"success": True, "url": hosted_url}
61
  except Exception as e:
62
- return {"error": str(e)}
63
-
64
- # -----------------
65
- # Gradio UI
66
- # -----------------
67
- examples = [
68
- ["https://upload.wikimedia.org/wikipedia/commons/1/15/Cat_August_2010-4.jpg"],
69
- ["https://upload.wikimedia.org/wikipedia/commons/4/4d/Cat_November_2010-1a.jpg"]
70
- ]
71
-
72
- with gr.Blocks(title="AI ClearCut - Background Remover") as demo:
73
- gr.Markdown("# <center>AI ClearCut</center>\n### <center>Background Removal + JSON API Output</center>")
74
-
75
- with gr.Row():
76
- with gr.Column():
77
- input_image = gr.Image(label="Upload Image", type="pil")
78
- submit_btn = gr.Button("Remove Background", variant="primary")
79
- gr.Examples(examples=examples, inputs=[input_image], label="Examples")
80
-
81
- with gr.Column():
82
- output_image = gr.Image(label="Result Image", type="pil")
83
- output_json = gr.Textbox(label="JSON Output", lines=6, interactive=False)
84
-
85
- # UI button → normal background removal
86
- submit_btn.click(fn=remove_and_upload, inputs=[input_image], outputs=[output_image, output_json])
87
-
88
- # API endpoint for ?url=
89
- demo.load(fn=lambda url: api_json(url), inputs=[gr.Textbox(label="Image URL")], outputs="json", api_name="json")
90
 
91
- # ✅ Launch for Hugging Face
92
- demo.launch()
 
 
 
 
1
+ import io
2
+ import requests
3
+ from fastapi import FastAPI, UploadFile, File, Query
4
+ from fastapi.responses import JSONResponse
5
  from rembg import remove
6
  from PIL import Image
7
+ import uvicorn
8
 
9
+ app = FastAPI(title="AI ClearCut - Background Remover")
10
+
11
+ # --------------------------
12
+ # Helper: process image bytes
13
+ # --------------------------
14
+ def process_image(img_bytes: bytes):
15
  # Remove background
16
  output_bytes = remove(
17
  img_bytes,
 
21
  alpha_matting_erode_size=10
22
  )
23
 
24
+ # Upload to ar-hosting
25
  files = {"file": ("output.png", output_bytes, "image/png")}
26
  r = requests.post("https://ar-hosting.pages.dev/upload", files=files)
27
  r.raise_for_status()
28
  data = r.json()
29
 
30
  if "url" in data:
31
+ return {"success": True, "url": data["url"]}
32
+ return {"error": "Upload failed"}
33
+
34
+ # --------------------------
35
+ # Endpoint 1: Upload file
36
+ # --------------------------
37
+ @app.post("/upload")
38
+ async def upload_file(file: UploadFile = File(...)):
39
  try:
40
+ img_bytes = await file.read()
41
+ result = process_image(img_bytes)
42
+ return JSONResponse(content=result)
 
 
 
 
 
 
 
 
43
  except Exception as e:
44
+ return JSONResponse(content={"error": str(e)})
45
 
46
+ # --------------------------
47
+ # Endpoint 2: From image URL
48
+ # --------------------------
49
+ @app.get("/json")
50
+ async def from_url(url: str = Query(..., description="Image URL")):
51
  try:
52
+ resp = requests.get(url, timeout=15)
53
  resp.raise_for_status()
54
+ result = process_image(resp.content)
55
+ return JSONResponse(content=result)
 
 
 
56
  except Exception as e:
57
+ return JSONResponse(content={"error": str(e)})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ # --------------------------
60
+ # HF Entry point
61
+ # --------------------------
62
+ if __name__ == "__main__":
63
+ uvicorn.run(app, host="0.0.0.0", port=7860)