Update app/main.py
Browse files- app/main.py +11 -19
app/main.py
CHANGED
|
@@ -4,12 +4,9 @@ from fastapi.responses import JSONResponse
|
|
| 4 |
from transformers import pipeline
|
| 5 |
from newspaper import Article
|
| 6 |
import pdfplumber
|
| 7 |
-
import io
|
| 8 |
import requests
|
| 9 |
from bs4 import BeautifulSoup
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
app.add_middleware(
|
|
@@ -22,6 +19,7 @@ app.add_middleware(
|
|
| 22 |
# Load summarizer from local model dir
|
| 23 |
summarizer = pipeline("summarization", model="/app/model/t5-base")
|
| 24 |
|
|
|
|
| 25 |
def extract_text_from_pdf(file):
|
| 26 |
text = ""
|
| 27 |
with pdfplumber.open(file) as pdf:
|
|
@@ -31,20 +29,15 @@ def extract_text_from_pdf(file):
|
|
| 31 |
text += page_text + "\n"
|
| 32 |
return text.strip()
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
def extract_text_from_url(url):
|
| 39 |
try:
|
| 40 |
if "wikipedia.org" in url:
|
| 41 |
-
res = requests.get(url, timeout=10)
|
| 42 |
-
res.raise_for_status()
|
| 43 |
soup = BeautifulSoup(res.content, "html.parser")
|
| 44 |
paragraphs = soup.find_all("p")
|
| 45 |
text_content = "\n".join(p.text.strip() for p in paragraphs if p.text.strip())
|
| 46 |
-
|
| 47 |
-
# Optional: limit to first 50 paragraphs to avoid huge texts
|
| 48 |
text_parts = text_content.split("\n")
|
| 49 |
limited_text = "\n".join(text_parts[:50])
|
| 50 |
return limited_text.strip()
|
|
@@ -57,8 +50,7 @@ def extract_text_from_url(url):
|
|
| 57 |
print(f"URL extraction failed: {e}")
|
| 58 |
return ""
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
def generate_summary(text, mode="paragraph", length="medium"):
|
| 63 |
length_map = {
|
| 64 |
"short": 50,
|
|
@@ -76,14 +68,16 @@ def generate_summary(text, mode="paragraph", length="medium"):
|
|
| 76 |
)
|
| 77 |
return summary_result[0]['summary_text']
|
| 78 |
|
|
|
|
| 79 |
@app.get("/")
|
| 80 |
def home():
|
| 81 |
return {"message": "📄 AI Summarizer API Running!"}
|
| 82 |
|
|
|
|
| 83 |
@app.post("/summarize")
|
| 84 |
async def summarize(
|
| 85 |
text: str = Form(None),
|
| 86 |
-
pdf_file: UploadFile
|
| 87 |
url: str = Form(None),
|
| 88 |
mode: str = Form("paragraph"),
|
| 89 |
length: str = Form("medium")
|
|
@@ -94,10 +88,9 @@ async def summarize(
|
|
| 94 |
content = text.strip()
|
| 95 |
|
| 96 |
elif pdf_file is not None and pdf_file.filename:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
|
| 102 |
elif url:
|
| 103 |
content = extract_text_from_url(url)
|
|
@@ -118,4 +111,3 @@ async def summarize(
|
|
| 118 |
"summaryLength": len(summary),
|
| 119 |
"mode": mode
|
| 120 |
}
|
| 121 |
-
|
|
|
|
| 4 |
from transformers import pipeline
|
| 5 |
from newspaper import Article
|
| 6 |
import pdfplumber
|
|
|
|
| 7 |
import requests
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
|
|
|
|
|
|
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
app.add_middleware(
|
|
|
|
| 19 |
# Load summarizer from local model dir
|
| 20 |
summarizer = pipeline("summarization", model="/app/model/t5-base")
|
| 21 |
|
| 22 |
+
# PDF Text Extractor
|
| 23 |
def extract_text_from_pdf(file):
|
| 24 |
text = ""
|
| 25 |
with pdfplumber.open(file) as pdf:
|
|
|
|
| 29 |
text += page_text + "\n"
|
| 30 |
return text.strip()
|
| 31 |
|
| 32 |
+
# URL Text Extractor
|
|
|
|
|
|
|
|
|
|
| 33 |
def extract_text_from_url(url):
|
| 34 |
try:
|
| 35 |
if "wikipedia.org" in url:
|
| 36 |
+
res = requests.get(url, timeout=10)
|
| 37 |
+
res.raise_for_status()
|
| 38 |
soup = BeautifulSoup(res.content, "html.parser")
|
| 39 |
paragraphs = soup.find_all("p")
|
| 40 |
text_content = "\n".join(p.text.strip() for p in paragraphs if p.text.strip())
|
|
|
|
|
|
|
| 41 |
text_parts = text_content.split("\n")
|
| 42 |
limited_text = "\n".join(text_parts[:50])
|
| 43 |
return limited_text.strip()
|
|
|
|
| 50 |
print(f"URL extraction failed: {e}")
|
| 51 |
return ""
|
| 52 |
|
| 53 |
+
# Summarizer
|
|
|
|
| 54 |
def generate_summary(text, mode="paragraph", length="medium"):
|
| 55 |
length_map = {
|
| 56 |
"short": 50,
|
|
|
|
| 68 |
)
|
| 69 |
return summary_result[0]['summary_text']
|
| 70 |
|
| 71 |
+
# Health Check
|
| 72 |
@app.get("/")
|
| 73 |
def home():
|
| 74 |
return {"message": "📄 AI Summarizer API Running!"}
|
| 75 |
|
| 76 |
+
# Summarize Endpoint
|
| 77 |
@app.post("/summarize")
|
| 78 |
async def summarize(
|
| 79 |
text: str = Form(None),
|
| 80 |
+
pdf_file: UploadFile = File(None),
|
| 81 |
url: str = Form(None),
|
| 82 |
mode: str = Form("paragraph"),
|
| 83 |
length: str = Form("medium")
|
|
|
|
| 88 |
content = text.strip()
|
| 89 |
|
| 90 |
elif pdf_file is not None and pdf_file.filename:
|
| 91 |
+
content = extract_text_from_pdf(pdf_file.file)
|
| 92 |
+
if not content:
|
| 93 |
+
return JSONResponse({"error": "❌ No readable text extracted from the PDF."}, 400)
|
|
|
|
| 94 |
|
| 95 |
elif url:
|
| 96 |
content = extract_text_from_url(url)
|
|
|
|
| 111 |
"summaryLength": len(summary),
|
| 112 |
"mode": mode
|
| 113 |
}
|
|
|