Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -20,10 +20,10 @@ os.environ['HF_HOME'] = '/tmp/.cache/huggingface'
|
|
20 |
# Create cache directory if it doesn't exist
|
21 |
os.makedirs('/tmp/.cache/huggingface', exist_ok=True)
|
22 |
|
23 |
-
|
|
|
24 |
|
25 |
-
# Initialize the model
|
26 |
-
@app.before_first_request
|
27 |
def load_model():
|
28 |
global pipe
|
29 |
try:
|
@@ -38,6 +38,11 @@ def load_model():
|
|
38 |
logger.error(f"Error loading model: {str(e)}")
|
39 |
raise
|
40 |
|
|
|
|
|
|
|
|
|
|
|
41 |
@app.route("/", methods=["GET"])
|
42 |
def index():
|
43 |
return jsonify({"message": "Zero123Plus API is running."})
|
@@ -45,6 +50,15 @@ def index():
|
|
45 |
@app.route("/generate", methods=["POST"])
|
46 |
def generate():
|
47 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
if 'image' not in request.files:
|
49 |
logger.warning("No image uploaded")
|
50 |
return jsonify({"error": "No image uploaded"}), 400
|
|
|
20 |
# Create cache directory if it doesn't exist
|
21 |
os.makedirs('/tmp/.cache/huggingface', exist_ok=True)
|
22 |
|
23 |
+
# Global variable for the model
|
24 |
+
pipe = None
|
25 |
|
26 |
+
# Initialize the model at startup
|
|
|
27 |
def load_model():
|
28 |
global pipe
|
29 |
try:
|
|
|
38 |
logger.error(f"Error loading model: {str(e)}")
|
39 |
raise
|
40 |
|
41 |
+
# Load the model immediately
|
42 |
+
load_model()
|
43 |
+
|
44 |
+
app = Flask(__name__)
|
45 |
+
|
46 |
@app.route("/", methods=["GET"])
|
47 |
def index():
|
48 |
return jsonify({"message": "Zero123Plus API is running."})
|
|
|
50 |
@app.route("/generate", methods=["POST"])
|
51 |
def generate():
|
52 |
try:
|
53 |
+
global pipe
|
54 |
+
# Check if model is loaded
|
55 |
+
if pipe is None:
|
56 |
+
try:
|
57 |
+
load_model()
|
58 |
+
except Exception as e:
|
59 |
+
logger.error(f"Failed to load model: {str(e)}")
|
60 |
+
return jsonify({"error": "Failed to initialize model"}), 500
|
61 |
+
|
62 |
if 'image' not in request.files:
|
63 |
logger.warning("No image uploaded")
|
64 |
return jsonify({"error": "No image uploaded"}), 400
|