Spaces:
Sleeping
Sleeping
Update routers/scraping_router.py
Browse files- routers/scraping_router.py +17 -5
routers/scraping_router.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
# routers/scraping_router.py
|
| 2 |
-
from fastapi import APIRouter, HTTPException, Header
|
| 3 |
from fastapi.responses import JSONResponse
|
|
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import Optional
|
| 6 |
import logging
|
| 7 |
import re
|
|
|
|
| 8 |
from helpers.ai_client import AIClient
|
| 9 |
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -16,6 +18,18 @@ router = APIRouter(
|
|
| 16 |
)
|
| 17 |
|
| 18 |
ai_client = AIClient()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
class CheerioScriptRequest(BaseModel):
|
| 21 |
html: str
|
|
@@ -30,7 +44,7 @@ class CheerioScriptResponse(BaseModel):
|
|
| 30 |
@router.post("/generate-cheerio-script", response_model=CheerioScriptResponse)
|
| 31 |
async def generate_cheerio_script(
|
| 32 |
request: CheerioScriptRequest,
|
| 33 |
-
|
| 34 |
):
|
| 35 |
try:
|
| 36 |
example = """
|
|
@@ -91,9 +105,7 @@ async def generate_cheerio_script(
|
|
| 91 |
|
| 92 |
response = ai_client.chat(
|
| 93 |
prompt=user_prompt,
|
| 94 |
-
system_message=system_prompt
|
| 95 |
-
model_id="openai/gpt-4o-mini",
|
| 96 |
-
api_key=x_api_key
|
| 97 |
)
|
| 98 |
|
| 99 |
cheerio_pattern = r'<cheerio_script>(.*?)</cheerio_script>'
|
|
|
|
| 1 |
# routers/scraping_router.py
|
| 2 |
+
from fastapi import APIRouter, HTTPException, Header, Depends
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
+
from fastapi.security import APIKeyHeader
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from typing import Optional
|
| 7 |
import logging
|
| 8 |
import re
|
| 9 |
+
import os
|
| 10 |
from helpers.ai_client import AIClient
|
| 11 |
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
ai_client = AIClient()
|
| 21 |
+
AI_SCRAPER_API_KEY = os.getenv("AI_SCRAPER_API_KEY")
|
| 22 |
+
|
| 23 |
+
# API Key security scheme
|
| 24 |
+
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=True)
|
| 25 |
+
|
| 26 |
+
async def verify_api_key(api_key: str = Depends(api_key_header)):
|
| 27 |
+
if api_key != AI_SCRAPER_API_KEY:
|
| 28 |
+
raise HTTPException(
|
| 29 |
+
status_code=401,
|
| 30 |
+
detail="Invalid API key"
|
| 31 |
+
)
|
| 32 |
+
return api_key
|
| 33 |
|
| 34 |
class CheerioScriptRequest(BaseModel):
|
| 35 |
html: str
|
|
|
|
| 44 |
@router.post("/generate-cheerio-script", response_model=CheerioScriptResponse)
|
| 45 |
async def generate_cheerio_script(
|
| 46 |
request: CheerioScriptRequest,
|
| 47 |
+
api_key: str = Depends(verify_api_key)
|
| 48 |
):
|
| 49 |
try:
|
| 50 |
example = """
|
|
|
|
| 105 |
|
| 106 |
response = ai_client.chat(
|
| 107 |
prompt=user_prompt,
|
| 108 |
+
system_message=system_prompt
|
|
|
|
|
|
|
| 109 |
)
|
| 110 |
|
| 111 |
cheerio_pattern = r'<cheerio_script>(.*?)</cheerio_script>'
|