yeonjin98 commited on
Commit
91a9be1
·
verified ·
1 Parent(s): fc15a05

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +140 -0
  2. requirements.txt +26 -0
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Gradio 인터페이스가 포함된 Hugging Face Spaces용 앱
3
+ FastAPI와 Gradio를 함께 실행
4
+ """
5
+
6
+ import gradio as gr
7
+ from PIL import Image
8
+ import numpy as np
9
+ import io
10
+ from server_birefnet import process_image, simple_background_removal, load_model
11
+ import torch
12
+
13
+ # 모델 로드
14
+ print("Loading BiRefNet model...")
15
+ model_loaded = load_model()
16
+
17
+ def remove_background_gradio(input_image):
18
+ """Gradio 인터페이스용 배경 제거 함수"""
19
+ if input_image is None:
20
+ return None
21
+
22
+ # PIL Image로 변환
23
+ if isinstance(input_image, np.ndarray):
24
+ image = Image.fromarray(input_image)
25
+ else:
26
+ image = input_image
27
+
28
+ # RGB로 변환
29
+ if image.mode != 'RGB':
30
+ image = image.convert('RGB')
31
+
32
+ # 배경 제거
33
+ try:
34
+ if model_loaded:
35
+ result = process_image(image)
36
+ else:
37
+ result = simple_background_removal(image)
38
+ return result
39
+ except Exception as e:
40
+ print(f"Error: {e}")
41
+ return image.convert("RGBA")
42
+
43
+ # Gradio 인터페이스 생성
44
+ with gr.Blocks(title="CleanCut - AI 배경 제거") as demo:
45
+ gr.Markdown(
46
+ """
47
+ # 🎨 CleanCut - AI 배경 제거
48
+
49
+ BiRefNet 모델을 사용한 고품질 배경 제거 서비스입니다.
50
+
51
+ ### 사용 방법:
52
+ 1. 이미지를 업로드하거나 드래그 앤 드롭하세요
53
+ 2. '배경 제거' 버튼을 클릭하세요
54
+ 3. 결과 이미지를 다운로드하세요
55
+
56
+ ### API 엔드포인트:
57
+ - POST `/remove-background` - 프로그래매틱 액세스용
58
+ """
59
+ )
60
+
61
+ with gr.Row():
62
+ with gr.Column():
63
+ input_image = gr.Image(
64
+ label="원본 이미지",
65
+ type="pil",
66
+ height=400
67
+ )
68
+ process_btn = gr.Button(
69
+ "🚀 배경 제거",
70
+ variant="primary",
71
+ size="lg"
72
+ )
73
+
74
+ with gr.Column():
75
+ output_image = gr.Image(
76
+ label="결과 이미지",
77
+ type="pil",
78
+ height=400
79
+ )
80
+ download_btn = gr.Button(
81
+ "💾 다운로드",
82
+ variant="secondary",
83
+ size="lg"
84
+ )
85
+
86
+ # 예제 이미지들 (파일이 있을 때만 활성화)
87
+ # gr.Examples(
88
+ # examples=[
89
+ # ["examples/person.jpg"],
90
+ # ["examples/product.jpg"],
91
+ # ["examples/pet.jpg"],
92
+ # ],
93
+ # inputs=input_image,
94
+ # label="예제 이미지"
95
+ # )
96
+
97
+ # 이벤트 연결
98
+ process_btn.click(
99
+ fn=remove_background_gradio,
100
+ inputs=input_image,
101
+ outputs=output_image
102
+ )
103
+
104
+ # Footer
105
+ gr.Markdown(
106
+ """
107
+ ---
108
+ 💡 **Tips**:
109
+ - 최상의 결과를 위해 고해상도 이미지를 사용하세요
110
+ - 복잡한 배경의 경우 처리 시간이 더 걸릴 수 있습니다
111
+
112
+ 🔗 [GitHub](https://github.com/yourusername/cleancut) |
113
+ 📱 [Flutter App](https://play.google.com/store/apps/details?id=com.cleancut)
114
+ """
115
+ )
116
+
117
+ # FastAPI 앱도 함께 실행 (선택사항)
118
+ from fastapi import FastAPI
119
+ from fastapi.middleware.cors import CORSMiddleware
120
+ import uvicorn
121
+ from threading import Thread
122
+
123
+ # FastAPI 설정
124
+ from server_birefnet import app as fastapi_app
125
+
126
+ def run_fastapi():
127
+ """FastAPI 서버를 별도 스레드에서 실행"""
128
+ uvicorn.run(fastapi_app, host="0.0.0.0", port=7861)
129
+
130
+ # FastAPI를 백그라운드에서 실행
131
+ # api_thread = Thread(target=run_fastapi, daemon=True)
132
+ # api_thread.start()
133
+
134
+ if __name__ == "__main__":
135
+ # Gradio 앱 실행
136
+ demo.launch(
137
+ server_name="0.0.0.0",
138
+ server_port=7860,
139
+ share=False
140
+ )
requirements.txt ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python server dependencies
2
+
3
+ # FastAPI web framework
4
+ fastapi==0.109.0
5
+ uvicorn[standard]==0.27.0
6
+ python-multipart==0.0.6
7
+
8
+ # Gradio (for Hugging Face Spaces)
9
+ gradio==4.16.0
10
+
11
+ # Image processing
12
+ Pillow==10.2.0
13
+ numpy==1.24.3
14
+
15
+ # BiRefNet model packages
16
+ torch==2.1.2
17
+ torchvision==0.16.2
18
+ transformers==4.36.2
19
+ accelerate==0.25.0
20
+ timm==0.9.12
21
+ einops==0.7.0
22
+ kornia==0.7.0
23
+
24
+ # ONNX conversion (optional)
25
+ # onnx==1.15.0
26
+ # onnxruntime==1.16.3