samyak152002 commited on
Commit
12d2529
1 Parent(s): e5592bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -39
app.py CHANGED
@@ -1,42 +1,58 @@
1
- from fastapi import FastAPI, File, UploadFile
2
- from fastapi.responses import JSONResponse, StreamingResponse
3
  from annotations import analyze_pdf
 
4
  import io
5
 
6
- app = FastAPI(
7
- title="PDF Language Issue Analyzer",
8
- description="API endpoint to analyze PDFs for language issues, their locations, and provide an annotated PDF.",
9
- version="1.0.0"
10
- )
11
-
12
- @app.post("/analyze-pdf/")
13
- async def analyze_pdf_endpoint(file: UploadFile = File(...)):
14
- """
15
- Endpoint to analyze a PDF file for language issues.
16
-
17
- - **file**: PDF file to be analyzed.
18
-
19
- **Returns**:
20
- - **issues**: Dictionary containing total issues and detailed information.
21
- - **annotated_pdf**: Annotated PDF file with highlighted issues.
22
- """
23
- if file.content_type != "application/pdf":
24
- return JSONResponse(status_code=400, content={"error": "Invalid file type. Please upload a PDF file."})
25
-
26
- try:
27
- # Analyze the PDF
28
- issues, annotated_pdf = analyze_pdf(file.file)
29
-
30
- response = {
31
- "issues": issues
32
- }
33
-
34
- if annotated_pdf:
35
- # Encode the annotated PDF in base64 for transmission
36
- annotated_pdf_io = io.BytesIO(annotated_pdf)
37
- response["annotated_pdf"] = "data:application/pdf;base64," + io.base64.b64encode(annotated_pdf).decode('utf-8')
38
-
39
- return JSONResponse(content=response)
40
-
41
- except Exception as e:
42
- return JSONResponse(status_code=500, content={"error": str(e)})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
 
2
  from annotations import analyze_pdf
3
+ import base64
4
  import io
5
 
6
+ def process_pdf(file):
7
+ if file is None:
8
+ return {"error": "No file uploaded."}, None
9
+
10
+ # Analyze the PDF
11
+ issues, annotated_pdf = analyze_pdf(file)
12
+
13
+ if "error" in issues:
14
+ return issues, None
15
+
16
+ # Prepare issues for display
17
+ issues_display = f"Total Issues Found: {issues['total_issues']}\n\n"
18
+ for idx, issue in enumerate(issues['issues'], start=1):
19
+ issues_display += f"Issue {idx}:\n"
20
+ issues_display += f"Message: {issue['message']}\n"
21
+ issues_display += f"Context: {issue['context']}\n"
22
+ issues_display += f"Suggestions: {', '.join(issue['suggestions']) if issue['suggestions'] else 'None'}\n"
23
+ issues_display += f"Category: {issue['category']}\n"
24
+ issues_display += f"Rule ID: {issue['rule_id']}\n"
25
+ issues_display += f"Offset: {issue['offset']}\n"
26
+ issues_display += f"Length: {issue['length']}\n\n"
27
+
28
+ # Prepare annotated PDF for download
29
+ if annotated_pdf:
30
+ annotated_pdf_base64 = base64.b64encode(annotated_pdf).decode('utf-8')
31
+ annotated_pdf_link = f"data:application/pdf;base64,{annotated_pdf_base64}"
32
+ else:
33
+ annotated_pdf_link = None
34
+
35
+ return issues_display, annotated_pdf_link
36
+
37
+ def download_annotated_pdf(annotated_pdf_link):
38
+ return annotated_pdf_link
39
+
40
+ with gr.Blocks() as demo:
41
+ gr.Markdown("# PDF Language Issue Analyzer")
42
+ gr.Markdown("Upload a PDF to analyze language issues and receive an annotated PDF.")
43
+
44
+ with gr.Row():
45
+ with gr.Column():
46
+ pdf_input = gr.File(label="Upload PDF", type="file")
47
+ analyze_button = gr.Button("Analyze PDF")
48
+ with gr.Column():
49
+ issues_output = gr.Textbox(label="Language Issues", lines=20)
50
+ annotated_pdf_output = gr.File(label="Download Annotated PDF")
51
+
52
+ analyze_button.click(
53
+ fn=process_pdf,
54
+ inputs=pdf_input,
55
+ outputs=[issues_output, annotated_pdf_output]
56
+ )
57
+
58
+ demo.launch()