Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,9 @@
|
|
| 2 |
from flask import Flask, request, send_file, jsonify
|
| 3 |
from flask_cors import CORS
|
| 4 |
from rembg import remove
|
| 5 |
-
from PIL import Image
|
| 6 |
import io
|
| 7 |
import os
|
| 8 |
-
import gradio as gr
|
| 9 |
|
| 10 |
# --- 2. יצירת אפליקציית Flask ---
|
| 11 |
app = Flask(__name__)
|
|
@@ -29,26 +28,22 @@ def remove_background_api():
|
|
| 29 |
if file.filename == '':
|
| 30 |
return jsonify({"error": "No file selected for uploading"}), 400
|
| 31 |
|
| 32 |
-
# קריאת הבייטים חשובה כדי שנוכל להשתמש בהם בכמה מקומות
|
| 33 |
input_bytes = file.read()
|
| 34 |
|
| 35 |
try:
|
| 36 |
-
#
|
| 37 |
-
# 1. פתח את התמונה באמצעות Pillow
|
| 38 |
input_image = Image.open(io.BytesIO(input_bytes))
|
| 39 |
|
| 40 |
-
#
|
| 41 |
input_image_rgba = input_image.convert("RGBA")
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
# rembg יודעת לעבוד גם עם אובייקטים של Pillow, לא רק עם בייטים.
|
| 45 |
output_image = remove(input_image_rgba, model='modnet_photographic')
|
| 46 |
|
| 47 |
-
#
|
| 48 |
output_bytes_stream = io.BytesIO()
|
| 49 |
output_image.save(output_bytes_stream, format="PNG")
|
| 50 |
output_bytes = output_bytes_stream.getvalue()
|
| 51 |
-
# <<< התיקון מסתיים כאן >>>
|
| 52 |
|
| 53 |
return send_file(
|
| 54 |
io.BytesIO(output_bytes),
|
|
@@ -61,20 +56,8 @@ def remove_background_api():
|
|
| 61 |
print(f"Error processing image: {e}")
|
| 62 |
return jsonify({"error": f"Failed to process image: {str(e)}"}), 500
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
return processed_image
|
| 70 |
-
|
| 71 |
-
gradio_interface = gr.Interface(
|
| 72 |
-
fn=remove_background_gradio,
|
| 73 |
-
inputs=gr.Image(type="pil", label="העלה תמונה"),
|
| 74 |
-
outputs=gr.Image(type="pil", label="תמונה ללא רקע"),
|
| 75 |
-
title="בודק הסרת רקעים",
|
| 76 |
-
description="ממשק בדיקה פשוט עבור שרת הסרת הרקעים."
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
# --- 7. הרכבת האפליקציות (ללא שינוי, נשאר כפי שהוא) ---
|
| 80 |
-
app = gr.mount_gradio_app(app, gradio_interface, path="/gradio")
|
|
|
|
| 2 |
from flask import Flask, request, send_file, jsonify
|
| 3 |
from flask_cors import CORS
|
| 4 |
from rembg import remove
|
| 5 |
+
from PIL import Image
|
| 6 |
import io
|
| 7 |
import os
|
|
|
|
| 8 |
|
| 9 |
# --- 2. יצירת אפליקציית Flask ---
|
| 10 |
app = Flask(__name__)
|
|
|
|
| 28 |
if file.filename == '':
|
| 29 |
return jsonify({"error": "No file selected for uploading"}), 400
|
| 30 |
|
|
|
|
| 31 |
input_bytes = file.read()
|
| 32 |
|
| 33 |
try:
|
| 34 |
+
# פתח את התמונה באמצעות Pillow
|
|
|
|
| 35 |
input_image = Image.open(io.BytesIO(input_bytes))
|
| 36 |
|
| 37 |
+
# המר את התמונה למצב RGBA כדי להבטיח ערוץ שקיפות
|
| 38 |
input_image_rgba = input_image.convert("RGBA")
|
| 39 |
|
| 40 |
+
# העבר את אובייקט התמונה של Pillow לפונקציה `remove`
|
|
|
|
| 41 |
output_image = remove(input_image_rgba, model='modnet_photographic')
|
| 42 |
|
| 43 |
+
# שמור את התוצאה לבייטים בפורמט PNG
|
| 44 |
output_bytes_stream = io.BytesIO()
|
| 45 |
output_image.save(output_bytes_stream, format="PNG")
|
| 46 |
output_bytes = output_bytes_stream.getvalue()
|
|
|
|
| 47 |
|
| 48 |
return send_file(
|
| 49 |
io.BytesIO(output_bytes),
|
|
|
|
| 56 |
print(f"Error processing image: {e}")
|
| 57 |
return jsonify({"error": f"Failed to process image: {str(e)}"}), 500
|
| 58 |
|
| 59 |
+
# --- 6. הרצת השרת (רלוונטי רק לפיתוח מקומי) ---
|
| 60 |
+
if __name__ == '__main__':
|
| 61 |
+
# Hugging Face Spaces מצפה שהאפליקציה תרוץ על פורט 7860
|
| 62 |
+
port = int(os.environ.get('PORT', 7860))
|
| 63 |
+
app.run(host='0.0.0.0', port=port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|