danicor commited on
Commit
e680cc7
·
verified ·
1 Parent(s): f2d2548

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -18
app.py CHANGED
@@ -1,25 +1,66 @@
1
  import os
 
2
  from huggingface_hub import hf_hub_download
 
 
 
 
3
 
4
  # تنظیم مسیرهای کش
5
  os.environ["HF_HOME"] = "/home/user/app/hf_cache"
6
 
7
- # مسیر CelebAMask-HQ
8
- celeb_path = "/home/user/app/huggingface_models/CelebAMask-HQ"
9
- face_parsing_path = os.path.join(celeb_path, "face_parsing")
10
-
11
- print("[Info] PYTHONPATH:", os.environ.get("PYTHONPATH"))
12
- print("[Info] CelebAMask-HQ path exists:", os.path.exists(celeb_path))
13
- print("[Info] face_parsing folder exists:", os.path.exists(face_parsing_path))
14
-
15
- # دانلود مدل از Hugging Face
16
- try:
17
- model_path = hf_hub_download(
18
- repo_id="public-data/CelebAMask-HQ-Face-Parsing",
19
- filename="models/model.pth",
20
- cache_dir="/home/user/app/hf_cache"
21
- )
22
- print("[Success] Model downloaded to:", model_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- except Exception as e:
25
- print("[Error] Failed to download model:", str(e))
 
 
1
  import os
2
+ import time
3
  from huggingface_hub import hf_hub_download
4
+ from flask import Flask, jsonify
5
+ import threading
6
+
7
+ app = Flask(__name__)
8
 
9
  # تنظیم مسیرهای کش
10
  os.environ["HF_HOME"] = "/home/user/app/hf_cache"
11
 
12
+ def initialize_application():
13
+ """Initialize the application and download models"""
14
+ print("===== Application Startup at {} =====".format(time.strftime("%Y-%m-%d %H:%M:%S")))
15
+
16
+ # مسیر CelebAMask-HQ
17
+ celeb_path = "/home/user/app/huggingface_models/CelebAMask-HQ"
18
+ face_parsing_path = os.path.join(celeb_path, "face_parsing")
19
+
20
+ print("[Info] PYTHONPATH:", os.environ.get("PYTHONPATH"))
21
+ print("[Info] CelebAMask-HQ path exists:", os.path.exists(celeb_path))
22
+ print("[Info] face_parsing folder exists:", os.path.exists(face_parsing_path))
23
+
24
+ # دانلود مدل از Hugging Face
25
+ try:
26
+ model_path = hf_hub_download(
27
+ repo_id="public-data/CelebAMask-HQ-Face-Parsing",
28
+ filename="models/model.pth",
29
+ cache_dir="/home/user/app/hf_cache"
30
+ )
31
+ print("[Success] Model downloaded to:", model_path)
32
+ return True
33
+ except Exception as e:
34
+ print("[Error] Failed to download model:", str(e))
35
+ return False
36
+
37
+ @app.route('/')
38
+ def home():
39
+ return jsonify({
40
+ "status": "running",
41
+ "message": "CelebAMask-HQ Face Parsing Application",
42
+ "timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
43
+ })
44
+
45
+ @app.route('/health')
46
+ def health():
47
+ return jsonify({"status": "healthy"})
48
+
49
+ @app.route('/info')
50
+ def info():
51
+ celeb_path = "/home/user/app/huggingface_models/CelebAMask-HQ"
52
+ return jsonify({
53
+ "celebamask_hq_exists": os.path.exists(celeb_path),
54
+ "face_parsing_exists": os.path.exists(os.path.join(celeb_path, "face_parsing")),
55
+ "cache_directory": os.environ.get("HF_HOME"),
56
+ "pythonpath": os.environ.get("PYTHONPATH")
57
+ })
58
+
59
+ if __name__ == '__main__':
60
+ # Initialize the application in a separate thread
61
+ init_thread = threading.Thread(target=initialize_application)
62
+ init_thread.start()
63
 
64
+ # Run Flask app
65
+ print("Starting Flask server on 0.0.0.0:7860...")
66
+ app.run(host='0.0.0.0', port=7860, debug=False)