Update myapp.py
Browse files
myapp.py
CHANGED
|
@@ -1,82 +1,17 @@
|
|
| 1 |
-
from flask import Flask,
|
| 2 |
from flask_cors import CORS
|
| 3 |
-
import
|
| 4 |
-
import tempfile
|
| 5 |
-
import os
|
| 6 |
-
from threading import RLock
|
| 7 |
-
from huggingface_hub import InferenceClient
|
| 8 |
-
from all_models import models # Importing models from all_models
|
| 9 |
|
|
|
|
| 10 |
myapp = Flask(__name__)
|
| 11 |
-
CORS(myapp) # Enable CORS
|
| 12 |
-
|
| 13 |
-
lock = RLock()
|
| 14 |
-
HF_TOKEN = os.environ.get("HF_TOKEN") # Hugging Face token
|
| 15 |
-
|
| 16 |
-
inference_timeout = 600 # Set timeout for inference
|
| 17 |
|
| 18 |
@myapp.route('/')
|
| 19 |
def home():
|
| 20 |
-
return "Welcome to the
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Function to dynamically load models from the "models" list
|
| 24 |
-
def get_model_from_name(model_name):
|
| 25 |
-
return model_name if model_name in models else None
|
| 26 |
-
|
| 27 |
-
# Asynchronous function to perform inference
|
| 28 |
-
async def infer(client, prompt, seed=1, timeout=inference_timeout, model="prompthero/openjourney-v4"):
|
| 29 |
-
task = asyncio.create_task(
|
| 30 |
-
asyncio.to_thread(client.text_to_image, prompt=prompt, seed=seed, model=model)
|
| 31 |
-
)
|
| 32 |
-
await asyncio.sleep(0)
|
| 33 |
-
try:
|
| 34 |
-
result = await asyncio.wait_for(task, timeout=timeout)
|
| 35 |
-
except (Exception, asyncio.TimeoutError) as e:
|
| 36 |
-
print(e)
|
| 37 |
-
print(f"Task timed out for model: {model}")
|
| 38 |
-
if not task.done():
|
| 39 |
-
task.cancel()
|
| 40 |
-
result = None
|
| 41 |
-
|
| 42 |
-
if task.done() and result is not None:
|
| 43 |
-
with lock:
|
| 44 |
-
temp_image = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
| 45 |
-
with open(temp_image.name, "wb") as f:
|
| 46 |
-
f.write(result) # Save the result image as a temporary file
|
| 47 |
-
return temp_image.name # Return the path to the saved image
|
| 48 |
-
return None
|
| 49 |
-
|
| 50 |
-
# Flask route for the API endpoint
|
| 51 |
-
@myapp.route('/generate_api', methods=['POST'])
|
| 52 |
-
def generate_api():
|
| 53 |
-
data = request.get_json()
|
| 54 |
-
|
| 55 |
-
# Extract required fields from the request
|
| 56 |
-
prompt = data.get('prompt', '')
|
| 57 |
-
seed = data.get('seed', 1)
|
| 58 |
-
model_name = data.get('model', 'prompthero/openjourney-v4') # Default to "prompthero/openjourney-v4" if not provided
|
| 59 |
-
|
| 60 |
-
if not prompt:
|
| 61 |
-
return jsonify({"error": "Prompt is required"}), 400
|
| 62 |
-
|
| 63 |
-
# Get the model from all_models
|
| 64 |
-
model = get_model_from_name(model_name)
|
| 65 |
-
if not model:
|
| 66 |
-
return jsonify({"error": f"Model '{model_name}' not found in available models"}), 400
|
| 67 |
|
| 68 |
-
try:
|
| 69 |
-
# Create a generic InferenceClient for the model
|
| 70 |
-
client = InferenceClient(token=HF_TOKEN) # Pass Hugging Face token if needed
|
| 71 |
|
| 72 |
-
# Call the async inference function
|
| 73 |
-
result_path = asyncio.run(infer(client, prompt, seed, model=model))
|
| 74 |
-
if result_path:
|
| 75 |
-
return send_file(result_path, mimetype='image/png') # Send back the generated image file
|
| 76 |
-
else:
|
| 77 |
-
return jsonify({"error": "Failed to generate image"}), 500
|
| 78 |
-
except Exception as e:
|
| 79 |
-
return jsonify({"error": str(e)}), 500
|
| 80 |
|
|
|
|
| 81 |
if __name__ == "__main__":
|
| 82 |
myapp.run(host='0.0.0.0', port=5000) # Run directly if needed for testing
|
|
|
|
| 1 |
+
from flask import Flask, jsonify, request, send_file
|
| 2 |
from flask_cors import CORS
|
| 3 |
+
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Initialize the Flask app
|
| 6 |
myapp = Flask(__name__)
|
| 7 |
+
CORS(myapp) # Enable CORS if needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
@myapp.route('/')
|
| 10 |
def home():
|
| 11 |
+
return "Welcome to the Image Background Remover!" # Basic home response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Add this block to make sure your app runs when called
|
| 16 |
if __name__ == "__main__":
|
| 17 |
myapp.run(host='0.0.0.0', port=5000) # Run directly if needed for testing
|