-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
170 lines (130 loc) · 3.92 KB
/
main.py
File metadata and controls
170 lines (130 loc) · 3.92 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# app/main.py
import os
from collections import deque
from typing import Deque, Dict, Any
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from duel_core import (
APP_NAME,
HISTORY_MAX_LEN,
MODEL_POOL,
CompareRequest,
JudgeRequest,
run_compare,
run_judge,
export_history_csv,
)
from tool_router_core import (
ToolRouterAnalyzeRequest,
analyze_tools,
)
from prompt_eval_core import (
PromptEvalRequest,
PromptEvalResponse,
evaluate_prompt,
)
from context_viz_core import (
ContextVizRequest,
ContextVizResponse,
analyze_context,
)
# ------- App & filesystem setup -------
app = FastAPI(title=APP_NAME)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
static_dir = os.path.join(BASE_DIR, "static")
templates_dir = os.path.join(BASE_DIR, "templates")
os.makedirs(static_dir, exist_ok=True)
os.makedirs(templates_dir, exist_ok=True)
app.mount("/static", StaticFiles(directory=static_dir), name="static")
templates = Jinja2Templates(directory=templates_dir)
# In-memory history for duel
history: Deque[Dict[str, Any]] = deque(maxlen=HISTORY_MAX_LEN)
# ------- Pages -------
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
# Index = pestaña Duel por defecto
return templates.TemplateResponse(
"index.html",
{
"request": request,
"app_name": APP_NAME,
"active_tab": "duel",
},
)
@app.get("/duel", response_class=HTMLResponse)
async def duel_page(request: Request):
return templates.TemplateResponse(
"duel.html",
{
"request": request,
"app_name": APP_NAME,
"active_tab": "duel",
},
)
@app.get("/tool-router", response_class=HTMLResponse)
async def tool_router_page(request: Request):
return templates.TemplateResponse(
"router.html",
{
"request": request,
"app_name": APP_NAME,
"active_tab": "router",
},
)
@app.get("/prompt-eval", response_class=HTMLResponse)
async def prompt_eval_page(request: Request):
return templates.TemplateResponse(
"prompt.html",
{
"request": request,
"app_name": APP_NAME,
"active_tab": "prompt",
},
)
@app.get("/context-viz", response_class=HTMLResponse)
async def context_viz_page(request: Request):
return templates.TemplateResponse(
"context.html",
{
"request": request,
"app_name": APP_NAME,
"active_tab": "context",
},
)
# ------- Duel: models, compare, judge, history, export -------
@app.get("/models")
async def list_models():
return {"models": MODEL_POOL}
@app.post("/compare")
async def compare(req: CompareRequest):
result = await run_compare(req)
history.append(result.model_dump())
return result
@app.post("/judge")
async def judge(req: JudgeRequest):
data = await run_judge(req, history)
return data
@app.get("/history")
async def get_history():
return list(history)
@app.get("/export.csv")
async def export_csv():
return export_history_csv(history)
# ------- Tool Router endpoints -------
@app.post("/tool-router/analyze")
async def tool_router_analyze(req: ToolRouterAnalyzeRequest):
resp = await analyze_tools(req)
return resp
# ------- Prompt eval -------
@app.post("/prompt-eval/analyze", response_model=PromptEvalResponse)
async def prompt_eval_analyze(req: PromptEvalRequest):
return await evaluate_prompt(req)
# ------- Context viz -------
@app.post("/context-viz/analyze", response_model=ContextVizResponse)
async def context_viz_analyze(req: ContextVizRequest):
return await analyze_context(req)
if __name__ == "__main__":
import uvicorn
uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)