Spaces:
Runtime error
Runtime error
Update app/main.py
Browse files- app/main.py +24 -16
app/main.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
-
# main.py
|
2 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException
|
3 |
-
from fastapi.responses import JSONResponse
|
|
|
|
|
4 |
from typing import Dict, Any
|
5 |
import io
|
6 |
import base64
|
@@ -17,20 +19,24 @@ app = FastAPI(
|
|
17 |
version="1.0.0"
|
18 |
)
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
"""
|
23 |
-
|
24 |
"""
|
25 |
logger.info("Root endpoint accessed.")
|
26 |
-
return {"
|
27 |
|
28 |
-
@app.post("/analyze", summary="Analyze PDF for Language Issues")
|
29 |
-
async def analyze_pdf_endpoint(file: UploadFile = File(...)):
|
30 |
"""
|
31 |
-
Analyze an uploaded PDF file for language issues.
|
32 |
-
|
33 |
-
- **file**: PDF file to be analyzed.
|
34 |
"""
|
35 |
if file.content_type != "application/pdf":
|
36 |
logger.error("Invalid file type uploaded: %s", file.content_type)
|
@@ -41,18 +47,20 @@ async def analyze_pdf_endpoint(file: UploadFile = File(...)):
|
|
41 |
file_stream = io.BytesIO(contents)
|
42 |
language_issues, annotated_pdf = analyze_pdf(file_stream)
|
43 |
|
44 |
-
|
|
|
45 |
"language_issues": language_issues
|
46 |
}
|
47 |
|
48 |
if annotated_pdf:
|
49 |
-
#
|
50 |
encoded_pdf = base64.b64encode(annotated_pdf).decode('utf-8')
|
51 |
-
|
|
|
52 |
logger.info("Annotated PDF generated and encoded.")
|
53 |
|
54 |
logger.info("PDF analysis completed successfully.")
|
55 |
-
return
|
56 |
except Exception as e:
|
57 |
logger.exception("Error occurred during PDF analysis.")
|
58 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
1 |
+
# app/main.py
|
2 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Request, Form
|
3 |
+
from fastapi.responses import JSONResponse, StreamingResponse, HTMLResponse, RedirectResponse
|
4 |
+
from fastapi.staticfiles import StaticFiles
|
5 |
+
from fastapi.templating import Jinja2Templates
|
6 |
from typing import Dict, Any
|
7 |
import io
|
8 |
import base64
|
|
|
19 |
version="1.0.0"
|
20 |
)
|
21 |
|
22 |
+
# Mount static files if any (optional)
|
23 |
+
# app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
24 |
+
|
25 |
+
# Set up templates
|
26 |
+
templates = Jinja2Templates(directory="app/templates")
|
27 |
+
|
28 |
+
@app.get("/", response_class=HTMLResponse, summary="Root Endpoint")
|
29 |
+
def read_root(request: Request):
|
30 |
"""
|
31 |
+
Render the main page with upload form.
|
32 |
"""
|
33 |
logger.info("Root endpoint accessed.")
|
34 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
35 |
|
36 |
+
@app.post("/analyze", response_class=HTMLResponse, summary="Analyze PDF for Language Issues")
|
37 |
+
async def analyze_pdf_endpoint(request: Request, file: UploadFile = File(...)):
|
38 |
"""
|
39 |
+
Analyze an uploaded PDF file for language issues and render the results on the frontend.
|
|
|
|
|
40 |
"""
|
41 |
if file.content_type != "application/pdf":
|
42 |
logger.error("Invalid file type uploaded: %s", file.content_type)
|
|
|
47 |
file_stream = io.BytesIO(contents)
|
48 |
language_issues, annotated_pdf = analyze_pdf(file_stream)
|
49 |
|
50 |
+
response_context = {
|
51 |
+
"request": request,
|
52 |
"language_issues": language_issues
|
53 |
}
|
54 |
|
55 |
if annotated_pdf:
|
56 |
+
# Encode the annotated PDF in Base64
|
57 |
encoded_pdf = base64.b64encode(annotated_pdf).decode('utf-8')
|
58 |
+
annotated_pdf_data_url = f"data:application/pdf;base64,{encoded_pdf}"
|
59 |
+
response_context["annotated_pdf"] = annotated_pdf_data_url
|
60 |
logger.info("Annotated PDF generated and encoded.")
|
61 |
|
62 |
logger.info("PDF analysis completed successfully.")
|
63 |
+
return templates.TemplateResponse("index.html", response_context)
|
64 |
except Exception as e:
|
65 |
logger.exception("Error occurred during PDF analysis.")
|
66 |
raise HTTPException(status_code=500, detail=str(e))
|