Spaces:
Sleeping
Sleeping
Update hf_api.py
Browse files
hf_api.py
CHANGED
|
@@ -17,6 +17,9 @@ from fastapi.responses import JSONResponse
|
|
| 17 |
from response_formatter import ResponseFormatter
|
| 18 |
import traceback
|
| 19 |
from fastapi.security.api_key import APIKeyHeader, APIKey
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Load environment variables
|
| 22 |
load_dotenv()
|
|
@@ -300,7 +303,13 @@ class AgentProcessor:
|
|
| 300 |
return None
|
| 301 |
|
| 302 |
# Initialize FastAPI app
|
| 303 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
agent_processor = None
|
| 305 |
|
| 306 |
# Add CORS middleware
|
|
@@ -312,6 +321,30 @@ app.add_middleware(
|
|
| 312 |
allow_headers=["*"],
|
| 313 |
)
|
| 314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
@app.on_event("startup")
|
| 316 |
async def startup_event():
|
| 317 |
global agent_processor
|
|
@@ -342,10 +375,19 @@ async def get_api_key(
|
|
| 342 |
)
|
| 343 |
|
| 344 |
# Update your route to require API key
|
| 345 |
-
@app.post(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
async def process_agent_request(
|
| 347 |
request: AgentRequest,
|
| 348 |
-
api_key: APIKey =
|
| 349 |
):
|
| 350 |
try:
|
| 351 |
logger.info(f"Processing agent request: {request.query}")
|
|
@@ -375,4 +417,141 @@ if __name__ == "__main__":
|
|
| 375 |
host="0.0.0.0",
|
| 376 |
port=port,
|
| 377 |
reload=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
)
|
|
|
|
| 17 |
from response_formatter import ResponseFormatter
|
| 18 |
import traceback
|
| 19 |
from fastapi.security.api_key import APIKeyHeader, APIKey
|
| 20 |
+
from fastapi.staticfiles import StaticFiles
|
| 21 |
+
from fastapi.openapi.docs import get_swagger_ui_html
|
| 22 |
+
from fastapi.responses import HTMLResponse
|
| 23 |
|
| 24 |
# Load environment variables
|
| 25 |
load_dotenv()
|
|
|
|
| 303 |
return None
|
| 304 |
|
| 305 |
# Initialize FastAPI app
|
| 306 |
+
app = FastAPI(
|
| 307 |
+
title="Agent API",
|
| 308 |
+
description="API requiring X-API-Key authentication",
|
| 309 |
+
version="1.0.0",
|
| 310 |
+
docs_url=None, # Disable default docs
|
| 311 |
+
openapi_tags=[{"name": "agent", "description": "Agent endpoints"}],
|
| 312 |
+
)
|
| 313 |
agent_processor = None
|
| 314 |
|
| 315 |
# Add CORS middleware
|
|
|
|
| 321 |
allow_headers=["*"],
|
| 322 |
)
|
| 323 |
|
| 324 |
+
# Add security scheme
|
| 325 |
+
app.add_middleware(
|
| 326 |
+
CORSMiddleware,
|
| 327 |
+
allow_origins=["*"],
|
| 328 |
+
allow_credentials=True,
|
| 329 |
+
allow_methods=["*"],
|
| 330 |
+
allow_headers=["*"],
|
| 331 |
+
)
|
| 332 |
+
|
| 333 |
+
# Add security scheme
|
| 334 |
+
app.add_security_requirement({"ApiKeyAuth": []})
|
| 335 |
+
app.openapi_schema = None # Reset OpenAPI schema
|
| 336 |
+
|
| 337 |
+
# Define the security scheme
|
| 338 |
+
security_scheme = {
|
| 339 |
+
"ApiKeyAuth": {
|
| 340 |
+
"type": "apiKey",
|
| 341 |
+
"in": "header",
|
| 342 |
+
"name": "X-API-Key",
|
| 343 |
+
"description": "API key required for authentication"
|
| 344 |
+
}
|
| 345 |
+
}
|
| 346 |
+
app.openapi_components = {"securitySchemes": security_scheme}
|
| 347 |
+
|
| 348 |
@app.on_event("startup")
|
| 349 |
async def startup_event():
|
| 350 |
global agent_processor
|
|
|
|
| 375 |
)
|
| 376 |
|
| 377 |
# Update your route to require API key
|
| 378 |
+
@app.post(
|
| 379 |
+
"/v1/agent",
|
| 380 |
+
tags=["agent"],
|
| 381 |
+
summary="Process agent request",
|
| 382 |
+
response_class=StreamingResponse,
|
| 383 |
+
responses={
|
| 384 |
+
403: {"description": "Invalid or missing API key"},
|
| 385 |
+
500: {"description": "Internal server error"}
|
| 386 |
+
}
|
| 387 |
+
)
|
| 388 |
async def process_agent_request(
|
| 389 |
request: AgentRequest,
|
| 390 |
+
api_key: APIKey = Security(api_key_header, scopes=[])
|
| 391 |
):
|
| 392 |
try:
|
| 393 |
logger.info(f"Processing agent request: {request.query}")
|
|
|
|
| 417 |
host="0.0.0.0",
|
| 418 |
port=port,
|
| 419 |
reload=True
|
| 420 |
+
)
|
| 421 |
+
|
| 422 |
+
# Add custom docs endpoint
|
| 423 |
+
@app.get("/docs", include_in_schema=False)
|
| 424 |
+
async def custom_swagger_ui_html():
|
| 425 |
+
return get_swagger_ui_html(
|
| 426 |
+
openapi_url=app.openapi_url,
|
| 427 |
+
title=app.title + " - Swagger UI",
|
| 428 |
+
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
|
| 429 |
+
swagger_js_url="https://unpkg.com/swagger-ui-dist@5.9.0/swagger-ui-bundle.js",
|
| 430 |
+
swagger_css_url="https://unpkg.com/swagger-ui-dist@5.9.0/swagger-ui.css",
|
| 431 |
+
swagger_favicon_url="https://fastapi.tiangolo.com/img/favicon.png",
|
| 432 |
+
extra_html="""
|
| 433 |
+
<style>
|
| 434 |
+
/* Dark theme with cool colors */
|
| 435 |
+
:root {
|
| 436 |
+
--primary-color: #00b4d8;
|
| 437 |
+
--secondary-color: #90e0ef;
|
| 438 |
+
--background-color: #0d1117;
|
| 439 |
+
--text-color: #e6edf3;
|
| 440 |
+
--border-color: #30363d;
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
body {
|
| 444 |
+
background-color: var(--background-color);
|
| 445 |
+
color: var(--text-color);
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
.swagger-ui {
|
| 449 |
+
background-color: var(--background-color);
|
| 450 |
+
color: var(--text-color);
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
/* Headers and text */
|
| 454 |
+
.swagger-ui .info .title,
|
| 455 |
+
.swagger-ui .info .base-url,
|
| 456 |
+
.swagger-ui .info li,
|
| 457 |
+
.swagger-ui .info p,
|
| 458 |
+
.swagger-ui .info table {
|
| 459 |
+
color: var(--text-color);
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
/* Operation buttons */
|
| 463 |
+
.swagger-ui .opblock.opblock-post {
|
| 464 |
+
background: rgba(0, 180, 216, 0.1);
|
| 465 |
+
border-color: var(--primary-color);
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
.swagger-ui .opblock.opblock-post .opblock-summary-method {
|
| 469 |
+
background: var(--primary-color);
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
+
/* Authorize button */
|
| 473 |
+
.swagger-ui .btn.authorize {
|
| 474 |
+
background: var(--primary-color);
|
| 475 |
+
border-color: var(--primary-color);
|
| 476 |
+
color: white;
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
.swagger-ui .btn.authorize svg {
|
| 480 |
+
fill: white;
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
/* Schema sections */
|
| 484 |
+
.swagger-ui .model-box {
|
| 485 |
+
background: rgba(48, 54, 61, 0.4);
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
.swagger-ui .model {
|
| 489 |
+
color: var(--text-color);
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
/* Try it out section */
|
| 493 |
+
.swagger-ui textarea,
|
| 494 |
+
.swagger-ui input[type=text] {
|
| 495 |
+
background: var(--background-color);
|
| 496 |
+
color: var(--text-color);
|
| 497 |
+
border-color: var(--border-color);
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
/* Response section */
|
| 501 |
+
.swagger-ui .responses-table th,
|
| 502 |
+
.swagger-ui .responses-table td {
|
| 503 |
+
color: var(--text-color);
|
| 504 |
+
border-color: var(--border-color);
|
| 505 |
+
}
|
| 506 |
+
|
| 507 |
+
/* Scrollbar */
|
| 508 |
+
::-webkit-scrollbar {
|
| 509 |
+
width: 8px;
|
| 510 |
+
height: 8px;
|
| 511 |
+
}
|
| 512 |
+
|
| 513 |
+
::-webkit-scrollbar-track {
|
| 514 |
+
background: var(--background-color);
|
| 515 |
+
}
|
| 516 |
+
|
| 517 |
+
::-webkit-scrollbar-thumb {
|
| 518 |
+
background: var(--primary-color);
|
| 519 |
+
border-radius: 4px;
|
| 520 |
+
}
|
| 521 |
+
|
| 522 |
+
/* Code blocks */
|
| 523 |
+
.swagger-ui .highlight-code {
|
| 524 |
+
background-color: #1b1f24;
|
| 525 |
+
}
|
| 526 |
+
|
| 527 |
+
/* Modal dialogs */
|
| 528 |
+
.swagger-ui .dialog-ux .modal-ux {
|
| 529 |
+
background: var(--background-color);
|
| 530 |
+
border-color: var(--border-color);
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
.swagger-ui .dialog-ux .modal-ux-header h3 {
|
| 534 |
+
color: var(--text-color);
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
/* Tables */
|
| 538 |
+
.swagger-ui table thead tr td,
|
| 539 |
+
.swagger-ui table thead tr th {
|
| 540 |
+
color: var(--text-color);
|
| 541 |
+
border-color: var(--border-color);
|
| 542 |
+
}
|
| 543 |
+
|
| 544 |
+
/* Links */
|
| 545 |
+
.swagger-ui a {
|
| 546 |
+
color: var(--primary-color);
|
| 547 |
+
}
|
| 548 |
+
|
| 549 |
+
/* Schema dropdowns */
|
| 550 |
+
.swagger-ui select {
|
| 551 |
+
background: var(--background-color);
|
| 552 |
+
color: var(--text-color);
|
| 553 |
+
border-color: var(--border-color);
|
| 554 |
+
}
|
| 555 |
+
</style>
|
| 556 |
+
"""
|
| 557 |
)
|