Skydata001 commited on
Commit
d12af8e
·
verified ·
1 Parent(s): 7414510

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +42 -0
  2. index.html +139 -0
  3. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ from fastapi import FastAPI, Request, File, UploadFile
3
+ from fastapi.responses import HTMLResponse, StreamingResponse
4
+ from rembg import remove # هذه المكتبة هي التي تزيل الخلفية
5
+ from PIL import Image
6
+
7
+ app = FastAPI()
8
+
9
+ # 1. عند فتح الرابط، اعرض الواجهة
10
+ @app.get("/", response_class=HTMLResponse)
11
+ async def read_root():
12
+ try:
13
+ with open("index.html", "r", encoding="utf-8") as f:
14
+ return f.read()
15
+ except FileNotFoundError:
16
+ return "<h1>خطأ: ملف index.html غير موجود</h1>"
17
+
18
+ # 2. عند طلب إزالة الخلفية
19
+ @app.post("/remove-bg")
20
+ async def remove_background(file: UploadFile = File(...)):
21
+ try:
22
+ # قراءة الصورة القادمة من الواجهة
23
+ input_bytes = await file.read()
24
+ input_image = Image.open(io.BytesIO(input_bytes))
25
+
26
+ # استخدام rembg للمعالجة
27
+ output_image = remove(input_image)
28
+
29
+ # حفظ النتيجة كصورة PNG في الذاكرة
30
+ output_bytes_io = io.BytesIO()
31
+ output_image.save(output_bytes_io, format="PNG")
32
+ output_bytes_io.seek(0)
33
+
34
+ # إرسال الصورة النظيفة إلى الواجهة
35
+ return StreamingResponse(output_bytes_io, media_type="image/png")
36
+
37
+ except Exception as e:
38
+ return {"error": f"حدث خطأ: {str(e)}"}
39
+
40
+ if __name__ == "__main__":
41
+ import uvicorn
42
+ uvicorn.run(app, host="0.0.0.0", port=7860)
index.html ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="ar" dir="rtl">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>أداة إزالة الخلفية</title>
7
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/js/all.min.js"></script>
8
+ <style>
9
+ @import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700&display=swap');
10
+ body {
11
+ font-family: 'Cairo', sans-serif;
12
+ background-color: #0d0d1a;
13
+ color: #ffffff;
14
+ display: flex;
15
+ justify-content: center;
16
+ align-items: center;
17
+ flex-direction: column;
18
+ min-height: 100vh;
19
+ padding: 20px;
20
+ box-sizing: border-box;
21
+ }
22
+ h1 { color: #6366f1; }
23
+ .container {
24
+ background: #1e1e2e;
25
+ padding: 25px;
26
+ border-radius: 15px;
27
+ border: 1px solid #333;
28
+ text-align: center;
29
+ width: 90%;
30
+ max-width: 500px;
31
+ box-shadow: 0 0 30px rgba(99, 102, 241, 0.2);
32
+ }
33
+ input[type="file"] { display: none; }
34
+ .upload-label {
35
+ display: inline-block;
36
+ padding: 12px 25px;
37
+ background: #6366f1;
38
+ color: white;
39
+ border-radius: 8px;
40
+ cursor: pointer;
41
+ transition: 0.3s;
42
+ }
43
+ .upload-label:hover { background: #4f52c2; }
44
+ #submit-btn {
45
+ display: none;
46
+ margin-top: 15px;
47
+ padding: 12px 30px;
48
+ background: #28a745;
49
+ color: white;
50
+ border: none;
51
+ border-radius: 8px;
52
+ font-size: 16px;
53
+ font-family: 'Cairo', sans-serif;
54
+ cursor: pointer;
55
+ }
56
+ #status {
57
+ margin-top: 20px;
58
+ font-size: 15px;
59
+ color: #aaa;
60
+ height: 20px;
61
+ }
62
+ #result-area {
63
+ display:none;
64
+ margin-top: 25px;
65
+ border-top: 1px solid #444;
66
+ padding-top: 25px;
67
+ }
68
+ #result-img { max-width: 100%; border-radius: 8px; }
69
+ </style>
70
+ </head>
71
+ <body>
72
+ <div class="container">
73
+ <h1><i class="fas fa-magic"></i> أداة إزالة الخلفية</h1>
74
+ <p style="color:#bbb; margin-bottom:25px;">ارفع صورتك واحصل عليها بخلفية شفافة.</p>
75
+
76
+ <input type="file" id="file-input" accept="image/png, image/jpeg">
77
+ <label for="file-input" class="upload-label">
78
+ <i class="fas fa-upload"></i> اختر صورة
79
+ </label>
80
+
81
+ <div id="status"></div>
82
+ <button id="submit-btn"><i class="fas fa-rocket"></i> ابدأ المعالجة</button>
83
+
84
+ <div id="result-area">
85
+ <h3>النتيجة (اضغط مطولاً للحفظ):</h3>
86
+ <img id="result-img" alt="الصورة بعد إزالة الخلفية">
87
+ </div>
88
+ </div>
89
+
90
+ <script>
91
+ const fileInput = document.getElementById('file-input');
92
+ const submitBtn = document.getElementById('submit-btn');
93
+ const statusEl = document.getElementById('status');
94
+ const resultArea = document.getElementById('result-area');
95
+ const resultImg = document.getElementById('result-img');
96
+
97
+ fileInput.addEventListener('change', () => {
98
+ if (fileInput.files.length > 0) {
99
+ statusEl.textContent = `تم اختيار: ${fileInput.files[0].name}`;
100
+ submitBtn.style.display = 'inline-block';
101
+ resultArea.style.display = 'none';
102
+ }
103
+ });
104
+
105
+ submitBtn.addEventListener('click', async () => {
106
+ if (fileInput.files.length === 0) return;
107
+
108
+ statusEl.innerHTML = '<i class="fas fa-spinner fa-spin"></i> جاري المعالجة...';
109
+ submitBtn.disabled = true;
110
+
111
+ const formData = new FormData();
112
+ formData.append('file', fileInput.files[0]);
113
+
114
+ try {
115
+ // إرسال الصورة إلى /remove-bg في app.py
116
+ const response = await fetch('/remove-bg', {
117
+ method: 'POST',
118
+ body: formData
119
+ });
120
+
121
+ if (!response.ok) throw new Error('فشل المعالجة');
122
+
123
+ // استقبال الصورة النظيفة
124
+ const imageBlob = await response.blob();
125
+ const imageUrl = URL.createObjectURL(imageBlob);
126
+
127
+ resultImg.src = imageUrl;
128
+ resultArea.style.display = 'block';
129
+ statusEl.textContent = 'اكتملت المعالجة!';
130
+
131
+ } catch (error) {
132
+ statusEl.textContent = '⚠️ حدث خطأ. حاول بصورة أخرى.';
133
+ } finally {
134
+ submitBtn.disabled = false;
135
+ }
136
+ });
137
+ </script>
138
+ </body>
139
+ </html>
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ rembg
4
+ Pillow
5
+ onnxruntime