Rehan12345678 commited on
Commit
b390cea
·
verified ·
1 Parent(s): 65bbd88

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +37 -29
src/streamlit_app.py CHANGED
@@ -1,41 +1,49 @@
1
  import os
2
- import json
3
- from fastapi import FastAPI
4
- from pydantic import BaseModel
5
- from langchain_community.document_loaders import FireCrawlLoader
6
- from firecrawl import FirecrawlApp
7
 
8
- # Initialize FastAPI app
9
  app = FastAPI()
10
 
 
 
 
 
 
 
 
 
11
  class ScrapeRequest(BaseModel):
12
  url: str
 
 
 
13
 
14
- def firecrawl_scrape_tool(url: str):
15
- """Scrapes a single URL using Firecrawl and returns the content."""
16
- firecrawl_api_key = os.getenv("FIRECRAWL_API_KEY")
17
- if not firecrawl_api_key:
18
- return {"error": "Firecrawl API key not found."}
19
-
20
  try:
21
- # Create a new instance of the FirecrawlApp for each request to avoid state issues.
22
- firecrawl_app = FirecrawlApp(api_key=firecrawl_api_key)
23
-
24
- # Use the updated 'scrape' method
25
- docs = firecrawl_app.scrape(url=url)
26
-
27
- # The fix: directly access the attributes from the returned dictionary.
28
- if docs and isinstance(docs, dict):
29
- return {"content": docs.get('content'), "metadata": docs.get('metadata')}
30
-
31
- return {"content": "No content found.", "metadata": {}}
 
 
 
 
32
  except Exception as e:
33
- return {"error": str(e)}
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."}