samyak152002 commited on
Commit
514e4c2
·
verified ·
1 Parent(s): 68594d1

Update app/main.py

Browse files
Files changed (1) hide show
  1. 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
- @app.get("/", summary="Root Endpoint")
21
- def read_root():
 
 
 
 
 
 
22
  """
23
- Root endpoint to verify that the API is running.
24
  """
25
  logger.info("Root endpoint accessed.")
26
- return {"message": "PDF Language Issue Analyzer API is running."}
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
- response = {
 
45
  "language_issues": language_issues
46
  }
47
 
48
  if annotated_pdf:
49
- # Properly encode the annotated PDF in Base64
50
  encoded_pdf = base64.b64encode(annotated_pdf).decode('utf-8')
51
- response["annotated_pdf"] = f"data:application/pdf;base64,{encoded_pdf}"
 
52
  logger.info("Annotated PDF generated and encoded.")
53
 
54
  logger.info("PDF analysis completed successfully.")
55
- return JSONResponse(content=response)
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))