Kevin Wu commited on
Commit
87afcae
·
1 Parent(s): 9875dc5

put in API

Browse files
Files changed (1) hide show
  1. reviewer/api.py +44 -14
reviewer/api.py CHANGED
@@ -6,6 +6,7 @@ from typing import Optional
6
 
7
  from pathlib import Path
8
  import tempfile
 
9
 
10
  from .reviewer import PDFReviewer
11
 
@@ -17,11 +18,15 @@ reviewer = PDFReviewer()
17
  @app.post("/review")
18
  async def review_pdf(
19
  file: Optional[UploadFile] = File(None),
 
20
  test: bool = Query(False, description="Return mock data for testing")
21
  ):
22
  """Upload a PDF file and return the structured review as JSON.
23
 
24
- If test=true is passed as a query parameter, returns mock data without requiring a file.
 
 
 
25
  """
26
  # Return mock data if test mode is enabled
27
  if test:
@@ -35,19 +40,44 @@ async def review_pdf(
35
  "audience_interest": "yes"
36
  })
37
 
38
- # Normal operation - file is required
39
- if not file:
40
- raise HTTPException(status_code=400, detail="File is required when not in test mode")
41
-
42
- if file.content_type != "application/pdf":
43
- raise HTTPException(status_code=400, detail="Only PDF files are supported")
44
-
45
- # Save to temporary location
46
- suffix = Path(file.filename).suffix or ".pdf"
47
- with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
48
- content = await file.read()
49
- tmp.write(content)
50
- tmp_path = Path(tmp.name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  try:
53
  review_result = reviewer.review_pdf(tmp_path)
 
6
 
7
  from pathlib import Path
8
  import tempfile
9
+ import requests
10
 
11
  from .reviewer import PDFReviewer
12
 
 
18
  @app.post("/review")
19
  async def review_pdf(
20
  file: Optional[UploadFile] = File(None),
21
+ url: Optional[str] = Query(None, description="URL of PDF to review"),
22
  test: bool = Query(False, description="Return mock data for testing")
23
  ):
24
  """Upload a PDF file and return the structured review as JSON.
25
 
26
+ You can either:
27
+ - Upload a file directly
28
+ - Provide a URL to a PDF
29
+ - Use test=true for mock data
30
  """
31
  # Return mock data if test mode is enabled
32
  if test:
 
40
  "audience_interest": "yes"
41
  })
42
 
43
+ # Check if either file or URL is provided
44
+ if not file and not url:
45
+ raise HTTPException(status_code=400, detail="Either file upload or URL is required")
46
+
47
+ if file and url:
48
+ raise HTTPException(status_code=400, detail="Please provide either file or URL, not both")
49
+
50
+ # Handle URL download
51
+ if url:
52
+ try:
53
+ # Download PDF from URL
54
+ response = requests.get(url, timeout=30)
55
+ response.raise_for_status()
56
+
57
+ # Verify it's a PDF
58
+ content_type = response.headers.get('content-type', '')
59
+ if 'application/pdf' not in content_type.lower():
60
+ raise HTTPException(status_code=400, detail=f"URL does not point to a PDF. Content-Type: {content_type}")
61
+
62
+ # Save to temporary file
63
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
64
+ tmp.write(response.content)
65
+ tmp_path = Path(tmp.name)
66
+
67
+ except requests.RequestException as e:
68
+ raise HTTPException(status_code=400, detail=f"Failed to download PDF from URL: {str(e)}")
69
+
70
+ # Handle file upload
71
+ else:
72
+ if file.content_type != "application/pdf":
73
+ raise HTTPException(status_code=400, detail="Only PDF files are supported")
74
+
75
+ # Save to temporary location
76
+ suffix = Path(file.filename).suffix or ".pdf"
77
+ with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
78
+ content = await file.read()
79
+ tmp.write(content)
80
+ tmp_path = Path(tmp.name)
81
 
82
  try:
83
  review_result = reviewer.review_pdf(tmp_path)