osamabyc86 commited on
Commit
1e8fa1d
·
verified ·
1 Parent(s): b2952b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -2,7 +2,7 @@ import subprocess
2
  import sys
3
  import os
4
  import socket
5
- from flask import Flask
6
 
7
  # تحديد المسار الحالي
8
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
@@ -16,8 +16,8 @@ def is_port_in_use(port):
16
  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
17
  return s.connect_ex(("127.0.0.1", port)) == 0
18
 
19
- def find_free_port(start_port=7520, max_port=7600):
20
- """إيجاد بورت متاح يبدأ من start_port"""
21
  port = start_port
22
  while port <= max_port:
23
  if not is_port_in_use(port):
@@ -25,16 +25,25 @@ def find_free_port(start_port=7520, max_port=7600):
25
  port += 1
26
  raise RuntimeError("❌ لم يتم العثور على أي بورت فارغ في النطاق المحدد")
27
 
 
 
 
 
 
 
 
 
 
28
  if __name__ == "__main__":
29
  try:
30
- # تشغيل main.py باستخدام نفس نسخة البايثون
31
  subprocess.run([sys.executable, MAIN_FILE], check=True)
32
  except subprocess.CalledProcessError as e:
33
  print(f"❌ خطأ أثناء تشغيل main.py: {e}")
34
 
35
- # البحث عن بورت متاح
36
- port_to_use = find_free_port(7520, 7600)
37
- print(f"✅ سيتم تشغيل السيرفر على البورت: {port_to_use}")
38
 
39
- # تشغيل Flask على البورت المتاح
40
- app.run(host="0.0.0.0", port=port_to_use)
 
2
  import sys
3
  import os
4
  import socket
5
+ from flask import Flask, request, redirect
6
 
7
  # تحديد المسار الحالي
8
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
 
16
  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
17
  return s.connect_ex(("127.0.0.1", port)) == 0
18
 
19
+ def find_free_port(start_port=7600, max_port=7900):
20
+ """إيجاد بورت متاح لبروكسي Hugging Face"""
21
  port = start_port
22
  while port <= max_port:
23
  if not is_port_in_use(port):
 
25
  port += 1
26
  raise RuntimeError("❌ لم يتم العثور على أي بورت فارغ في النطاق المحدد")
27
 
28
+ @app.route("/")
29
+ def index():
30
+ return "✅ السيرفر يعمل على استقبال 7520"
31
+
32
+ @app.route("/forward", methods=["GET", "POST"])
33
+ def forward_request():
34
+ """تحويل الطلب إلى بورت Hugging Face"""
35
+ return redirect(f"http://127.0.0.1:{huggingface_port}", code=307)
36
+
37
  if __name__ == "__main__":
38
  try:
39
+ # تشغيل main.py
40
  subprocess.run([sys.executable, MAIN_FILE], check=True)
41
  except subprocess.CalledProcessError as e:
42
  print(f"❌ خطأ أثناء تشغيل main.py: {e}")
43
 
44
+ # تحديد بورت Hugging Face
45
+ huggingface_port = find_free_port(7860, 7900)
46
+ print(f"✅ Hugging Face سيعمل على البورت: {huggingface_port}")
47
 
48
+ # تشغيل استقبال على 7520
49
+ app.run(host="0.0.0.0", port=7520)