File size: 3,061 Bytes
9d0e55d
 
bb267e7
9d0e55d
bb267e7
d3a5644
9d0e55d
bb267e7
 
9d0e55d
 
 
 
bb267e7
9d0e55d
bb267e7
9d0e55d
 
 
 
bb267e7
9d0e55d
 
bb267e7
 
9d0e55d
 
 
bb267e7
9d0e55d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb267e7
 
 
 
 
 
 
 
9d0e55d
 
 
 
bb267e7
9d0e55d
bb267e7
 
 
b038116
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# AUTO_PATCH_DONE=0

from pathlib import Path
import os
import sys

# Gradio'nun node template index.js yolu
FILE = Path("/usr/local/lib/python3.10/site-packages/gradio/templates/node/build/index.js")

def patch_gradio():
    if not FILE.exists():
        print("[PATCH] index.js bulunamadı! Yol doğru mu kontrol et.")
        return False

    text = FILE.read_text(encoding="utf-8")

    # Eğer daha önce patchlendiyse tekrar dokunma
    if "// ===== CUSTOM NODE PROXY =====" in text:
        print("[PATCH] index.js zaten patch'lenmiş durumda.")
        return False

    # Arayacağımız orijinal satır (marker)
    marker = """\tif (route === "python") {\t\tproxy.web(req, res, { target: pythonTarget });\t\treturn;\t}
"""

    # Yerine koyacağımız proxy kurallı patch
    patch = """\tif (route === "python") {\t\tproxy.web(req, res, { target: pythonTarget });\t\treturn;\t}\t// ===== CUSTOM NODE PROXY =====\tif (path === "/node" || path.startsWith("/node/")) {\t\treq.url = req.url.replace(/^\\/node/, "") || "/";\t\tproxy.web(req, res, {\t\t\ttarget: "http://127.0.0.1:8888",\t\t\tchangeOrigin: true\t\t});\t\treturn;\t}
"""

    if marker not in text:
        print("[PATCH] index.js içinde hedef marker bulunamadı!")
        return False

    # Patch'i uygula ve kaydet
    FILE.write_text(text.replace(marker, patch), encoding="utf-8")
    print("[PATCH] index.js başarıyla patchlendi.")
    return True

# 1. AŞAMA: Dosyanın başındaki flag değerini oku
current_file = Path(__file__)
file_content = current_file.read_text(encoding="utf-8")

# Eğer henüz patch aşaması tamamlanmadıysa (AUTO_PATCH_DONE=0 ise)
if "# AUTO_PATCH_DONE=0" in file_content:
    print("[SÜREÇ] İlk çalıştırma algılandı. İşlemler başlıyor...")
    
    # Önce index.js dosyasını patch'le
    patched = patch_gradio()
    
    # Şimdi app.py'yi güncelle (AUTO_PATCH_DONE=1 yap)
    updated_content = file_content.replace("# AUTO_PATCH_DONE=0", "# AUTO_PATCH_DONE=1")
    current_file.write_text(updated_content, encoding="utf-8")
    print("[SÜREÇ] app.py güncellendi (# AUTO_PATCH_DONE=1 yapıldı).")
    
    # Python process'ini sıfırdan yeniden başlat
    print("[SÜREÇ] Process yeniden başlatılıyor (os.execv)...")
    os.execv(sys.executable, [sys.executable] + sys.argv)

else:
    # Eğer AUTO_PATCH_DONE=1 ise bu blok çalışır ve patch adımları doğrudan es geçilir.
    print("[SÜREÇ] İkinci aşama/Restart sonrası açılış. Patch adımları atlandı.")

# ==========================================
# 2. AŞAMA: NORMAL GRADIO + NODE ÇALIŞMASI
# ==========================================

import gradio as gr
import spaces

@spaces.GPU
def dummy():
    return "ok"

with gr.Blocks(title="Patch Test") as demo:
    gr.Markdown("# Patch Test")
    gr.Markdown("Gradio başarıyla patch'lendi ve restart atıldı.")
    gr.Markdown("Eğer bu sayfayı görüyorsan, startup sorunsuz tamamlanmış demektir!")

# Gradio'yu ayağa kaldır
demo.launch(
    server_name="0.0.0.0",
    server_port=7860,
)