Update app/main.py
Browse files3 unmodified lines4567891011121314156 unmodified lines2223242526273 unmodified lines31323334353637383940414243444546474849506 unmodified lines575859606162636411 unmodified lines76777879808182838485868788894 unmodified lines94959697989910010110210314 unmodified lines1181191201213 unmodified linesfrom transformers import pipelinefrom newspaper import Articleimport pdfplumberimport ioimport requestsfrom bs4 import BeautifulSoupapp = FastAPI()app.add_middleware(6 unmodified lines# Load summarizer from local model dirsummarizer = pipeline("summarization", model="/app/model/t5-base")def extract_text_from_pdf(file):text = ""with pdfplumber.open(file) as pdf:3 unmodified linestext += page_text + "\n"return text.strip()def extract_text_from_url(url):try:if "wikipedia.org" in url:res = requests.get(url, timeout=10) # timeout addedres.raise_for_status() # check for HTTP errorssoup = BeautifulSoup(res.content, "html.parser")paragraphs = soup.find_all("p")text_content = "\n".join(p.text.strip() for p in paragraphs if p.text.strip())# Optional: limit to first 50 paragraphs to avoid huge textstext_parts = text_content.split("\n")limited_text = "\n".join(text_parts[:50])return limited_text.strip()6 unmodified linesprint(f"URL extraction failed: {e}")return ""def generate_summary(text, mode="paragraph", length="medium"):length_map = {"short": 50,11 unmodified lines)return summary_result[0]['summary_text']@app.get("/")def home():return {"message": "📄 AI Summarizer API Running!"}@app.post("/summarize")async def summarize(text: str = Form(None),pdf_file: UploadFile | None = File(None),url: str = Form(None),mode: str = Form("paragraph"),length: str = Form("medium")4 unmodified linescontent = text.strip()elif pdf_file is not None and pdf_file.filename:content = extract_text_from_pdf(pdf_file.file)if not content:return JSONResponse({"error": "❌ No readable text extracted from the PDF."}, 400)elif url:content = extract_text_from_url(url)14 unmodified lines"summaryLength": len(summary),"mode": mode}3 unmodified lines4567891011126 unmodified lines192021222324253 unmodified lines2930313233343536373839404142436 unmodified lines5051525354555611 unmodified lines686970717273747576777879808182834 unmodified lines88899091929394959614 unmodified lines1111121133 unmodified linesfrom transformers import pipelinefrom newspaper import Articleimport pdfplumberimport requestsfrom bs4 import BeautifulSoupapp = FastAPI()app.add_middleware(6 unmodified lines# Load summarizer from local model dirsummarizer = pipeline("summarization", model="/app/model/t5-base")# PDF Text Extractordef extract_text_from_pdf(file):text = ""with pdfplumber.open(file) as pdf:3 unmodified linestext += page_text + "\n"return text.strip()# URL Text Extractordef extract_text_from_url(url):try:if "wikipedia.org" in url:res = requests.get(url, timeout=10)res.raise_for_status()soup = BeautifulSoup(res.content, "html.parser")paragraphs = soup.find_all("p")text_content = "\n".join(p.text.strip() for p in paragraphs if p.text.strip())text_parts = text_content.split("\n")limited_text = "\n".join(text_parts[:50])return limited_text.strip()6 unmodified linesprint(f"URL extraction failed: {e}")return ""# Summarizerdef generate_summary(text, mode="paragraph", length="medium"):length_map = {"short": 50,11 unmodified lines)return summary_result[0]['summary_text']# Health Check@app.get("/")def home():return {"message": "📄 AI Summarizer API Running!"}# Summarize Endpoint@app.post("/summarize")async def summarize(text: str = Form(None),pdf_file: UploadFile = File(None),url: str = Form(None),mode: str = Form("paragraph"),length: str = Form("medium")4 unmodified linescontent = text.strip()elif pdf_file is not None and pdf_file.filename:content = extract_text_from_pdf(pdf_file.file)if not content:return JSONResponse({"error": "❌ No readable text extracted from the PDF."}, 400)elif url:content = extract_text_from_url(url)14 unmodified lines"summaryLength": len(summary),"mode": mode}
app/main.py+11-19