Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import os, json, traceback
|
|
| 2 |
from typing import List
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
-
from fastapi.responses import RedirectResponse, PlainTextResponse
|
| 6 |
import gradio as gr
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
|
@@ -82,7 +82,7 @@ def ui_company_score_and_proposal(company_name: str,
|
|
| 82 |
outputs.append("### 🧩 インデックス更新ログ\n" + index_report)
|
| 83 |
return "\n\n".join(outputs)
|
| 84 |
|
| 85 |
-
# --- Gradio UI(/
|
| 86 |
with gr.Blocks(title="営業自動化 Agent Studio") as demo:
|
| 87 |
gr.Markdown("# 営業自動化 Agent Studio(Hugging Face 完結)")
|
| 88 |
with gr.Row():
|
|
@@ -109,23 +109,24 @@ with gr.Blocks(title="営業自動化 Agent Studio") as demo:
|
|
| 109 |
outputs=[out]
|
| 110 |
)
|
| 111 |
|
| 112 |
-
# --- FastAPI(/
|
| 113 |
-
app = FastAPI(title="Agent Studio - Docker (
|
| 114 |
app.add_middleware(
|
| 115 |
CORSMiddleware,
|
| 116 |
allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],
|
| 117 |
)
|
| 118 |
app.add_exception_handler(Exception, _exception_printer)
|
| 119 |
|
| 120 |
-
# ルートに来たら UI へ自動リダイレクト
|
| 121 |
-
@app.get("/")
|
| 122 |
-
def root():
|
| 123 |
-
return RedirectResponse(url="/ui")
|
| 124 |
-
|
| 125 |
@app.get("/health")
|
| 126 |
def health():
|
| 127 |
return {"ok": True}
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
@app.get("/t/{token}")
|
| 130 |
def track_click(token: str, request: Request):
|
| 131 |
try:
|
|
@@ -136,11 +137,11 @@ def track_click(token: str, request: Request):
|
|
| 136 |
ip = request.client.host if request.client else "unknown"
|
| 137 |
ua = request.headers.get("User-Agent", "") if request.headers else ""
|
| 138 |
log_event("click", payload, {"ip": ip, "ua": ua})
|
| 139 |
-
redirect_to = payload.get("redirect") or os.getenv("PUBLIC_BASE_URL", "/
|
| 140 |
return RedirectResponse(url=redirect_to)
|
| 141 |
except Exception as e:
|
| 142 |
tb = "".join(traceback.format_exception(type(e), e, e.__traceback__))
|
| 143 |
return PlainTextResponse(f"tracking error\n\n{tb}", status_code=500)
|
| 144 |
|
| 145 |
-
# /
|
| 146 |
-
app = gr.mount_gradio_app(app, demo, path="/
|
|
|
|
| 2 |
from typing import List
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from fastapi.responses import RedirectResponse, PlainTextResponse
|
| 6 |
import gradio as gr
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
|
|
|
| 82 |
outputs.append("### 🧩 インデックス更新ログ\n" + index_report)
|
| 83 |
return "\n\n".join(outputs)
|
| 84 |
|
| 85 |
+
# --- Gradio UI(ルート / に設置) ---
|
| 86 |
with gr.Blocks(title="営業自動化 Agent Studio") as demo:
|
| 87 |
gr.Markdown("# 営業自動化 Agent Studio(Hugging Face 完結)")
|
| 88 |
with gr.Row():
|
|
|
|
| 109 |
outputs=[out]
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# --- FastAPI(/ = UI、/ui も / へ転送、/t と /health はAPI) ---
|
| 113 |
+
app = FastAPI(title="Agent Studio - Docker (root UI)")
|
| 114 |
app.add_middleware(
|
| 115 |
CORSMiddleware,
|
| 116 |
allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],
|
| 117 |
)
|
| 118 |
app.add_exception_handler(Exception, _exception_printer)
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
@app.get("/health")
|
| 121 |
def health():
|
| 122 |
return {"ok": True}
|
| 123 |
|
| 124 |
+
# /ui に来た人も UI(/)へ転送しておく
|
| 125 |
+
@app.get("/ui")
|
| 126 |
+
def old_ui():
|
| 127 |
+
return RedirectResponse(url="/")
|
| 128 |
+
|
| 129 |
+
# クリック計測
|
| 130 |
@app.get("/t/{token}")
|
| 131 |
def track_click(token: str, request: Request):
|
| 132 |
try:
|
|
|
|
| 137 |
ip = request.client.host if request.client else "unknown"
|
| 138 |
ua = request.headers.get("User-Agent", "") if request.headers else ""
|
| 139 |
log_event("click", payload, {"ip": ip, "ua": ua})
|
| 140 |
+
redirect_to = payload.get("redirect") or os.getenv("PUBLIC_BASE_URL", "/")
|
| 141 |
return RedirectResponse(url=redirect_to)
|
| 142 |
except Exception as e:
|
| 143 |
tb = "".join(traceback.format_exception(type(e), e, e.__traceback__))
|
| 144 |
return PlainTextResponse(f"tracking error\n\n{tb}", status_code=500)
|
| 145 |
|
| 146 |
+
# ルート(/)で Gradio を表示
|
| 147 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|