-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
36 lines (27 loc) · 840 Bytes
/
server.py
File metadata and controls
36 lines (27 loc) · 840 Bytes
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
from fastapi import FastAPI, Body
from typing import List
from rich.traceback import install
from pydantic import BaseModel
from .browser import WebScraper
from pydantic import create_model
install(show_locals=True)
app = FastAPI()
class ScrapeRequestModel(BaseModel):
start_url: str
task: str
schema: dict
@app.post("/scrape")
async def scrape(request: ScrapeRequestModel = Body(...)):
start_url = request.start_url
task = request.task
schema = request.schema
model = create_model(
"ResponseModel", **{key: (value, ...) for key, value in schema.items()}
)
class OutputModel(BaseModel):
results: List[model]
class Config:
arbitrary_types_allowed = True
scraper = WebScraper(task, start_url, OutputModel)
result = await scraper.run()
return result