Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
import json
|
|
@@ -986,59 +987,60 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Text-to-SQL 智能助手 (HF Space
|
|
| 986 |
btn.click(process_query, inputs=[inp, prompt_override], outputs=[sql_out, status, logs], api_name="/predict")
|
| 987 |
inp.submit(process_query, inputs=[inp, prompt_override], outputs=[sql_out, status, logs])
|
| 988 |
|
| 989 |
-
|
| 990 |
-
|
| 991 |
-
def health_endpoint():
|
| 992 |
-
endpoints = []
|
| 993 |
-
try:
|
| 994 |
-
cfg = getattr(demo, "config", None)
|
| 995 |
-
if isinstance(cfg, dict):
|
| 996 |
-
deps = cfg.get("dependencies") or []
|
| 997 |
-
for dep in deps:
|
| 998 |
-
endpoints.append({
|
| 999 |
-
"api_name": dep.get("api_name"),
|
| 1000 |
-
"fn_index": dep.get("fn_index"),
|
| 1001 |
-
"inputs_count": len(dep.get("inputs") or []),
|
| 1002 |
-
"outputs_count": len(dep.get("outputs") or []),
|
| 1003 |
-
})
|
| 1004 |
-
except Exception:
|
| 1005 |
-
pass
|
| 1006 |
-
|
| 1007 |
-
if not endpoints:
|
| 1008 |
-
endpoints.append({
|
| 1009 |
-
"api_name": "/predict",
|
| 1010 |
-
"fn_index": None,
|
| 1011 |
-
"inputs_count": 2,
|
| 1012 |
-
"outputs_count": 3,
|
| 1013 |
-
})
|
| 1014 |
-
|
| 1015 |
-
env_info = {
|
| 1016 |
-
"USE_GPU": USE_GPU,
|
| 1017 |
-
"DEVICE": DEVICE,
|
| 1018 |
-
"N_GPU_LAYERS": N_GPU_LAYERS,
|
| 1019 |
-
"THREADS": THREADS,
|
| 1020 |
-
"CTX": CTX,
|
| 1021 |
-
"MAX_TOKENS": MAX_TOKENS,
|
| 1022 |
-
"FEW_SHOT_EXAMPLES_COUNT": FEW_SHOT_EXAMPLES_COUNT,
|
| 1023 |
-
"ENABLE_INDEX": ENABLE_INDEX,
|
| 1024 |
-
"EMBED_BATCH": EMBED_BATCH,
|
| 1025 |
-
"N_BATCH": N_BATCH,
|
| 1026 |
-
"GGUF_REPO_ID": GGUF_REPO_ID,
|
| 1027 |
-
"GGUF_FILENAME": GGUF_FILENAME,
|
| 1028 |
-
}
|
| 1029 |
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
|
| 1034 |
-
|
| 1035 |
-
|
| 1036 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1037 |
|
| 1038 |
if __name__ == "__main__":
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
server_port=7860,
|
| 1042 |
-
share=True,
|
| 1043 |
-
show_error=True
|
| 1044 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
import json
|
|
|
|
| 987 |
btn.click(process_query, inputs=[inp, prompt_override], outputs=[sql_out, status, logs], api_name="/predict")
|
| 988 |
inp.submit(process_query, inputs=[inp, prompt_override], outputs=[sql_out, status, logs])
|
| 989 |
|
| 990 |
+
# ========== 使用 FastAPI 掛載,提供 /health ==========
|
| 991 |
+
_fastapi_app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 992 |
|
| 993 |
+
@_fastapi_app.get("/health")
|
| 994 |
+
def health_endpoint():
|
| 995 |
+
endpoints = []
|
| 996 |
+
try:
|
| 997 |
+
cfg = getattr(demo, "config", None)
|
| 998 |
+
if isinstance(cfg, dict):
|
| 999 |
+
deps = cfg.get("dependencies") or []
|
| 1000 |
+
for dep in deps:
|
| 1001 |
+
endpoints.append({
|
| 1002 |
+
"api_name": dep.get("api_name"),
|
| 1003 |
+
"fn_index": dep.get("fn_index"),
|
| 1004 |
+
"inputs_count": len(dep.get("inputs") or []),
|
| 1005 |
+
"outputs_count": len(dep.get("outputs") or []),
|
| 1006 |
+
})
|
| 1007 |
+
except Exception:
|
| 1008 |
+
pass
|
| 1009 |
+
|
| 1010 |
+
if not endpoints:
|
| 1011 |
+
endpoints.append({
|
| 1012 |
+
"api_name": "/predict",
|
| 1013 |
+
"fn_index": None,
|
| 1014 |
+
"inputs_count": 2,
|
| 1015 |
+
"outputs_count": 3,
|
| 1016 |
+
})
|
| 1017 |
+
|
| 1018 |
+
env_info = {
|
| 1019 |
+
"USE_GPU": USE_GPU,
|
| 1020 |
+
"DEVICE": DEVICE,
|
| 1021 |
+
"N_GPU_LAYERS": N_GPU_LAYERS,
|
| 1022 |
+
"THREADS": THREADS,
|
| 1023 |
+
"CTX": CTX,
|
| 1024 |
+
"MAX_TOKENS": MAX_TOKENS,
|
| 1025 |
+
"FEW_SHOT_EXAMPLES_COUNT": FEW_SHOT_EXAMPLES_COUNT,
|
| 1026 |
+
"ENABLE_INDEX": ENABLE_INDEX,
|
| 1027 |
+
"EMBED_BATCH": EMBED_BATCH,
|
| 1028 |
+
"N_BATCH": N_BATCH,
|
| 1029 |
+
"GGUF_REPO_ID": GGUF_REPO_ID,
|
| 1030 |
+
"GGUF_FILENAME": GGUF_FILENAME,
|
| 1031 |
+
}
|
| 1032 |
+
|
| 1033 |
+
server_info = {
|
| 1034 |
+
"time": get_current_time(),
|
| 1035 |
+
"gradio_version": getattr(gr, "__version__", "unknown"),
|
| 1036 |
+
"pid": os.getpid(),
|
| 1037 |
+
}
|
| 1038 |
+
|
| 1039 |
+
return {"status": "ok", "endpoints": endpoints, "env": env_info, "server": server_info}
|
| 1040 |
+
|
| 1041 |
+
# 將 Gradio Blocks 掛載到 FastAPI 的根路徑
|
| 1042 |
+
app = gr.mount_gradio_app(_fastapi_app, demo, path="/")
|
| 1043 |
|
| 1044 |
if __name__ == "__main__":
|
| 1045 |
+
import uvicorn
|
| 1046 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|