File size: 1,821 Bytes
619ded7
 
826cabf
4f0cbc8
619ded7
 
 
 
 
 
 
 
 
 
 
 
55fd49a
 
e98edba
0f71dfc
55fd49a
8a327d8
55fd49a
e7de21b
 
 
 
55fd49a
e98edba
 
7814d1d
55fd49a
8a327d8
7b3699d
 
 
 
55fd49a
 
 
 
 
0f71dfc
55fd49a
 
c39bef7
99fdb5f
55fd49a
 
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
# 1. ZeroGPU MUST be imported before ANYTHING else
import spaces

# 2. Patch the asyncio -1 file descriptor cleanup bug
import asyncio
import asyncio.selector_events

if hasattr(asyncio.selector_events.BaseSelectorEventLoop, "_close_self_pipe"):
    _orig_close_self_pipe = asyncio.selector_events.BaseSelectorEventLoop._close_self_pipe
    def _patched_close_self_pipe(self):
        try:
            _orig_close_self_pipe(self)
        except (ValueError, OSError):
            pass
    asyncio.selector_events.BaseSelectorEventLoop._close_self_pipe = _patched_close_self_pipe

# 3. Imports
import os
import gradio as gr
from fastapi.staticfiles import StaticFiles
from main import app   # your real FastAPI app: routers + CORS already set up

# 4. ZeroGPU anchor
@spaces.GPU
def zero_gpu_keepalive():
    return "GPU Active"

# 5. Landing page
with gr.Blocks(title="Tools Backend API") as demo:
    gr.Markdown("# 🚀 CloudAiRambo Tools API Running")
    gr.Markdown("- **Frontend Site:** [freetoolshub.cloudairambo.com](https://freetoolshub.cloudairambo.com)")
    gr.Markdown("- **Swagger Docs:** [/docs](/docs)")

    # Hidden hook so ZeroGPU sees the GPU function used in a Gradio event
    _out = gr.Textbox(visible=False)
    _btn = gr.Button("ping", visible=False)
    _btn.click(zero_gpu_keepalive, inputs=None, outputs=_out, api_name="gpu_ping")
# 6. Static dirs (only if main.py hasn't already mounted them)
for name in ("static", "uploads"):
    os.makedirs(name, exist_ok=True)
    if not any(getattr(r, "name", None) == name for r in app.routes):
        app.mount(f"/{name}", StaticFiles(directory=name), name=name)

# 7. Gradio goes LAST so your API routes win
app = gr.mount_gradio_app(app, demo, path="/")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)