Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +37 -29
src/streamlit_app.py
CHANGED
|
@@ -1,41 +1,49 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
from fastapi import FastAPI
|
| 4 |
-
from pydantic import BaseModel
|
| 5 |
-
from
|
| 6 |
-
from firecrawl import FirecrawlApp
|
| 7 |
|
| 8 |
-
# Initialize FastAPI app
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
class ScrapeRequest(BaseModel):
|
| 12 |
url: str
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
if not
|
| 18 |
-
|
| 19 |
-
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
@app.post("/scrape_url")
|
| 36 |
-
def scrape_url(request: ScrapeRequest):
|
| 37 |
-
return firecrawl_scrape_tool(request.url)
|
| 38 |
|
| 39 |
@app.get("/")
|
| 40 |
def root():
|
| 41 |
-
return {"message": "Firecrawl MCP Server is running."}
|
|
|
|
| 1 |
import os
|
| 2 |
+
from typing import List, Optional, Union, Dict, Any
|
| 3 |
+
from fastapi import FastAPI, HTTPException
|
| 4 |
+
from pydantic import BaseModel, Field
|
| 5 |
+
from firecrawl import Firecrawl
|
|
|
|
| 6 |
|
|
|
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
class JsonFormatSpec(BaseModel):
|
| 10 |
+
type: str = Field("json", const=True)
|
| 11 |
+
# One of these may be supplied; schema is recommended for structured output.
|
| 12 |
+
schema: Optional[type] = None # Pydantic model class if used server-side
|
| 13 |
+
prompt: Optional[str] = None # Optional prompt to guide extraction
|
| 14 |
+
|
| 15 |
+
FormatsType = List[Union[str, Dict[str, Any]]]
|
| 16 |
+
|
| 17 |
class ScrapeRequest(BaseModel):
|
| 18 |
url: str
|
| 19 |
+
formats: Optional[FormatsType] = Field(default_factory=lambda: ["markdown"])
|
| 20 |
+
only_main_content: Optional[bool] = False
|
| 21 |
+
timeout_ms: Optional[int] = 120000 # matches SDK examples in ms
|
| 22 |
|
| 23 |
+
@app.post("/scrape_url")
|
| 24 |
+
def scrape_url(req: ScrapeRequest):
|
| 25 |
+
api_key = os.getenv("FIRECRAWL_API_KEY")
|
| 26 |
+
if not api_key:
|
| 27 |
+
raise HTTPException(status_code=500, detail="FIRECRAWL_API_KEY not set")
|
|
|
|
| 28 |
try:
|
| 29 |
+
client = Firecrawl(api_key=api_key)
|
| 30 |
+
# Call scrape; pass optional flags when provided
|
| 31 |
+
result = client.scrape(
|
| 32 |
+
req.url,
|
| 33 |
+
formats=req.formats, # e.g., ["markdown", "html"] or [{"type":"json","prompt":"..."}]
|
| 34 |
+
only_main_content=req.only_main_content,
|
| 35 |
+
timeout=req.timeout_ms,
|
| 36 |
+
)
|
| 37 |
+
# Normalize common shapes: mapping with success/data is typical
|
| 38 |
+
if isinstance(result, dict):
|
| 39 |
+
# Prefer returning the "data" block which includes formats + metadata
|
| 40 |
+
data = result.get("data", result)
|
| 41 |
+
return {"success": True, "data": data}
|
| 42 |
+
# Some examples show object-like returns; expose best-effort mapping
|
| 43 |
+
return {"success": True, "data": getattr(result, "__dict__", str(result))}
|
| 44 |
except Exception as e:
|
| 45 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
@app.get("/")
|
| 48 |
def root():
|
| 49 |
+
return {"message": "Firecrawl MCP Server is running."}
|