Update myapp.py
Browse files
myapp.py
CHANGED
|
@@ -2,8 +2,8 @@ from flask import Flask, request, jsonify, send_file
|
|
| 2 |
from flask_cors import CORS
|
| 3 |
import os
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
-
from io import BytesIO
|
| 6 |
-
from PIL import Image #
|
| 7 |
|
| 8 |
# Initialize the Flask app
|
| 9 |
myapp = Flask(__name__)
|
|
@@ -13,14 +13,10 @@ CORS(myapp) # Enable CORS for all routes
|
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN") # Ensure to set your Hugging Face token in the environment
|
| 14 |
client = InferenceClient(token=HF_TOKEN)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
def
|
| 18 |
-
return "Welcome to the Image Background Remover!"
|
| 19 |
-
|
| 20 |
-
# Function to generate an image from a prompt
|
| 21 |
-
def generate_image(prompt, seed=1, model="prompthero/openjourney-v4"):
|
| 22 |
try:
|
| 23 |
-
# Generate the image using Hugging Face's inference API
|
| 24 |
result_image = client.text_to_image(prompt=prompt, seed=seed, model=model)
|
| 25 |
return result_image
|
| 26 |
except Exception as e:
|
|
@@ -31,27 +27,32 @@ def generate_image(prompt, seed=1, model="prompthero/openjourney-v4"):
|
|
| 31 |
@myapp.route('/generate_image', methods=['POST'])
|
| 32 |
def generate_api():
|
| 33 |
data = request.get_json()
|
| 34 |
-
|
| 35 |
# Extract required fields from the request
|
| 36 |
prompt = data.get('prompt', '')
|
| 37 |
seed = data.get('seed', 1)
|
| 38 |
-
model_name = data.get('model', 'prompthero/openjourney-v4') #
|
| 39 |
|
| 40 |
if not prompt:
|
| 41 |
return jsonify({"error": "Prompt is required"}), 400
|
| 42 |
|
| 43 |
try:
|
| 44 |
-
# Call the generate_image function with the
|
| 45 |
image = generate_image(prompt, seed, model_name)
|
| 46 |
-
|
| 47 |
if image:
|
| 48 |
-
# Save the image to a BytesIO object
|
| 49 |
image_bytes = BytesIO()
|
| 50 |
image.save(image_bytes, format='PNG')
|
| 51 |
image_bytes.seek(0) # Go to the start of the byte stream
|
| 52 |
|
| 53 |
-
# Send the generated image as
|
| 54 |
-
return send_file(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
else:
|
| 56 |
return jsonify({"error": "Failed to generate image"}), 500
|
| 57 |
except Exception as e:
|
|
|
|
| 2 |
from flask_cors import CORS
|
| 3 |
import os
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from PIL import Image # Importing Pillow for image processing
|
| 7 |
|
| 8 |
# Initialize the Flask app
|
| 9 |
myapp = Flask(__name__)
|
|
|
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN") # Ensure to set your Hugging Face token in the environment
|
| 14 |
client = InferenceClient(token=HF_TOKEN)
|
| 15 |
|
| 16 |
+
# Function to generate an image from a prompt using the specified model
|
| 17 |
+
def generate_image(prompt, seed=1, model="prompthero/openjourney-v4"): # Default model if none provided
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
+
# Generate the image using Hugging Face's inference API
|
| 20 |
result_image = client.text_to_image(prompt=prompt, seed=seed, model=model)
|
| 21 |
return result_image
|
| 22 |
except Exception as e:
|
|
|
|
| 27 |
@myapp.route('/generate_image', methods=['POST'])
|
| 28 |
def generate_api():
|
| 29 |
data = request.get_json()
|
| 30 |
+
|
| 31 |
# Extract required fields from the request
|
| 32 |
prompt = data.get('prompt', '')
|
| 33 |
seed = data.get('seed', 1)
|
| 34 |
+
model_name = data.get('model', 'prompthero/openjourney-v4') # Use the provided model name or a default one
|
| 35 |
|
| 36 |
if not prompt:
|
| 37 |
return jsonify({"error": "Prompt is required"}), 400
|
| 38 |
|
| 39 |
try:
|
| 40 |
+
# Call the generate_image function with the dynamically provided model name
|
| 41 |
image = generate_image(prompt, seed, model_name)
|
| 42 |
+
|
| 43 |
if image:
|
| 44 |
+
# Save the image to a BytesIO object
|
| 45 |
image_bytes = BytesIO()
|
| 46 |
image.save(image_bytes, format='PNG')
|
| 47 |
image_bytes.seek(0) # Go to the start of the byte stream
|
| 48 |
|
| 49 |
+
# Send the generated image as an attachment
|
| 50 |
+
return send_file(
|
| 51 |
+
image_bytes,
|
| 52 |
+
mimetype='image/png',
|
| 53 |
+
as_attachment=True, # Send the file as an attachment
|
| 54 |
+
download_name='generated_image.png' # The file name for download
|
| 55 |
+
)
|
| 56 |
else:
|
| 57 |
return jsonify({"error": "Failed to generate image"}), 500
|
| 58 |
except Exception as e:
|