Update app.py
Browse files
app.py
CHANGED
|
@@ -3,36 +3,32 @@ from flask_cors import CORS
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
CORS(app) # Enable CORS for all routes
|
| 9 |
client = InferenceClient()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
@app.route('/')
|
| 13 |
-
def home():
|
| 14 |
-
return "Welcome to the Image Background Remover!"
|
| 15 |
-
|
| 16 |
@app.route('/generate-image', methods=['POST'])
|
| 17 |
def generate_image():
|
| 18 |
-
# Get the
|
| 19 |
-
|
| 20 |
-
prompt =
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
# Generate image using the InferenceClient
|
| 26 |
generated_image = client.image_to_image(image, prompt=prompt)
|
| 27 |
|
| 28 |
# Save the generated image to a BytesIO object
|
| 29 |
img_byte_arr = io.BytesIO()
|
| 30 |
-
generated_image.save(img_byte_arr, format='
|
| 31 |
img_byte_arr.seek(0) # Move the cursor to the beginning of the BytesIO object
|
| 32 |
|
| 33 |
# Send the generated image back to the client
|
| 34 |
-
return send_file(img_byte_arr, mimetype='image/
|
| 35 |
-
|
| 36 |
|
| 37 |
if __name__ == "__main__":
|
| 38 |
-
app.run(
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
+
import base64
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
CORS(app) # Enable CORS for all routes
|
| 10 |
client = InferenceClient()
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@app.route('/generate-image', methods=['POST'])
|
| 13 |
def generate_image():
|
| 14 |
+
data = request.json # Get the JSON data from the request
|
| 15 |
+
base64_image = data['image'] # Get the base64 image string
|
| 16 |
+
prompt = data['prompt'] # Get the prompt
|
| 17 |
|
| 18 |
+
# Decode the base64 image
|
| 19 |
+
image_data = base64.b64decode(base64_image)
|
| 20 |
+
image = Image.open(io.BytesIO(image_data))
|
| 21 |
|
| 22 |
# Generate image using the InferenceClient
|
| 23 |
generated_image = client.image_to_image(image, prompt=prompt)
|
| 24 |
|
| 25 |
# Save the generated image to a BytesIO object
|
| 26 |
img_byte_arr = io.BytesIO()
|
| 27 |
+
generated_image.save(img_byte_arr, format='JPEG')
|
| 28 |
img_byte_arr.seek(0) # Move the cursor to the beginning of the BytesIO object
|
| 29 |
|
| 30 |
# Send the generated image back to the client
|
| 31 |
+
return send_file(img_byte_arr, mimetype='image/jpeg')
|
|
|
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
+
app.run(debug=True)
|