Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,51 @@
|
|
| 1 |
-
from flask import Flask, request,
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# פרונטאנד פשוט יותר - רק טופס, בלי אייפרעים או ג'אווהסקריפט מורכב
|
| 7 |
-
FRONTEND_HTML = """
|
| 8 |
-
<!DOCTYPE html>
|
| 9 |
-
<html lang="he" dir="rtl">
|
| 10 |
-
<head>
|
| 11 |
-
<meta charset="UTF-8">
|
| 12 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 13 |
-
<title>My Web Proxy</title>
|
| 14 |
-
<style>
|
| 15 |
-
body { font-family: Arial, sans-serif; background-color: #f0f2f5; margin: 0; padding: 20px; text-align: center; }
|
| 16 |
-
h1 { color: #333; }
|
| 17 |
-
.container { max-width: 800px; margin: 40px auto; padding: 20px; background-color: #fff; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
|
| 18 |
-
input[type="url"] { width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; }
|
| 19 |
-
button { padding: 10px 20px; border: none; background-color: #007bff; color: white; font-size: 16px; border-radius: 4px; cursor: pointer; margin-right: 10px;}
|
| 20 |
-
button:hover { background-color: #0056b3; }
|
| 21 |
-
</style>
|
| 22 |
-
</head>
|
| 23 |
-
<body>
|
| 24 |
-
<div class="container">
|
| 25 |
-
<h1>פרוקסי אישי</h1>
|
| 26 |
-
<form id="proxy-form" action="/proxy" method="GET">
|
| 27 |
-
<input type="url" id="url-input" name="url" placeholder="הכנס כתובת אתר..." required>
|
| 28 |
-
<button type="submit">טען</button>
|
| 29 |
-
</form>
|
| 30 |
-
</div>
|
| 31 |
-
</body>
|
| 32 |
-
</html>
|
| 33 |
-
"""
|
| 34 |
|
| 35 |
-
@app.route('/')
|
| 36 |
-
def home():
|
| 37 |
-
"""מגיש את דף הפרונטאנד."""
|
| 38 |
-
return render_template_string(FRONTEND_HTML)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
if not original_url:
|
| 45 |
-
return "שגיאה: פרמטר URL חסר.", 400
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
# שלב 2: בניית ה-URL של גוגל תרגום
|
| 53 |
-
google_translate_url = f"https://translate.google.com/translate?sl=auto&tl=en&u={original_url}"
|
| 54 |
|
| 55 |
-
# שלב 3: הפניה מחדש (Redirect) של הדפדפן לכתובת של גוגל
|
| 56 |
-
return redirect(google_translate_url)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, send_file, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
from rembg import remove
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
+
CORS(app, resources={r"/api/*": {"origins": "*"}})
|
| 10 |
+
@app.route('/')
|
| 11 |
+
def index():
|
| 12 |
+
return jsonify({"message": "Background removal server is running!"})
|
| 13 |
+
|
| 14 |
+
@app.route('/api/remove-background', methods=['POST'])
|
| 15 |
+
def remove_background_api():
|
| 16 |
+
if 'file' not in request.files:
|
| 17 |
+
return jsonify({"error": "No file part in the request"}), 400
|
| 18 |
+
file = request.files['file']
|
| 19 |
+
if file.filename == '':
|
| 20 |
+
return jsonify({"error": "No file selected for uploading"}), 400
|
| 21 |
+
|
| 22 |
+
input_bytes = file.read()
|
| 23 |
+
try:
|
| 24 |
+
input_image = Image.open(io.BytesIO(input_bytes)).convert("RGBA")
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
|
| 29 |
+
# --- !!! השינוי המרכזי כאן !!! ---
|
| 30 |
+
# הפעם אנחנו מבקשים במפורש את המודל הכבד והאיכותי ביותר - u2net.
|
| 31 |
+
output_image = remove(input_image, model='u2net')
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
output_bytes_stream = io.BytesIO()
|
| 34 |
+
output_image.save(output_bytes_stream, format="PNG")
|
| 35 |
+
output_bytes = output_bytes_stream.getvalue()
|
| 36 |
+
|
| 37 |
+
return send_file(
|
| 38 |
+
io.BytesIO(output_bytes),
|
| 39 |
+
mimetype='image/png',
|
| 40 |
+
as_attachment=False,
|
| 41 |
+
download_name='processed.png'
|
| 42 |
+
)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Error processing image: {e}")
|
| 45 |
+
return jsonify({"error": f"Failed to process image: {str(e}", )})500
|
| 46 |
|
|
|
|
|
|
|
| 47 |
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
if __name__ == '__main__':
|
| 50 |
+
port = int(os.environ.get('PORT', 7860))
|
| 51 |
+
app.run(host='0.0.0.0', port=port)
|