Spaces:
Sleeping
Sleeping
Rivalcoder
commited on
Commit
·
09c633c
1
Parent(s):
8575323
Add application file
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
|
|
2 |
import fitz # PyMuPDF
|
3 |
import pytesseract
|
4 |
from PIL import Image
|
@@ -6,6 +7,40 @@ import io
|
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
@app.post("/extract-text")
|
10 |
async def extract_text(file: UploadFile = File(...)):
|
11 |
try:
|
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from fastapi.responses import HTMLResponse
|
3 |
import fitz # PyMuPDF
|
4 |
import pytesseract
|
5 |
from PIL import Image
|
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
|
11 |
+
@app.get("/", response_class=HTMLResponse)
|
12 |
+
async def home():
|
13 |
+
html_content = """
|
14 |
+
<!DOCTYPE html>
|
15 |
+
<html>
|
16 |
+
<head>
|
17 |
+
<title>PDF Text Extraction API</title>
|
18 |
+
<style>
|
19 |
+
body { font-family: Arial, sans-serif; padding: 2rem; background: #f0f0f0; }
|
20 |
+
.container { max-width: 600px; margin: auto; background: white; padding: 2rem; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1);}
|
21 |
+
h1 { color: #333; }
|
22 |
+
p { color: #555; }
|
23 |
+
a { color: #1a73e8; text-decoration: none; }
|
24 |
+
a:hover { text-decoration: underline; }
|
25 |
+
</style>
|
26 |
+
</head>
|
27 |
+
<body>
|
28 |
+
<div class="container">
|
29 |
+
<h1>Welcome to PDF Text Extraction API</h1>
|
30 |
+
<p>This API allows you to upload PDFs and extract text — including optional OCR for images.</p>
|
31 |
+
<h2>Available endpoints:</h2>
|
32 |
+
<ul>
|
33 |
+
<li><b>POST /extract-text</b> - Extract text from PDF pages.</li>
|
34 |
+
<li><b>POST /extract-text-ocr</b> - Extract text including OCR from images inside PDFs.</li>
|
35 |
+
</ul>
|
36 |
+
<p>Use a tool like <a href="https://www.postman.com/" target="_blank">Postman</a> or write your own client to send PDF files to the endpoints.</p>
|
37 |
+
</div>
|
38 |
+
</body>
|
39 |
+
</html>
|
40 |
+
"""
|
41 |
+
return HTMLResponse(content=html_content, status_code=200)
|
42 |
+
|
43 |
+
|
44 |
@app.post("/extract-text")
|
45 |
async def extract_text(file: UploadFile = File(...)):
|
46 |
try:
|