Asanaly
commited on
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,61 +1,47 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from fastapi import
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
app = FastAPI(
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
model_ru = MBartForConditionalGeneration.from_pretrained(MODEL_NAME_RU)
|
| 24 |
-
|
| 25 |
-
# ==========================
|
| 26 |
-
# API Request схемасы
|
| 27 |
-
# ==========================
|
| 28 |
-
class TextRequest(BaseModel):
|
| 29 |
-
text: str
|
| 30 |
-
lang: str # "en" немесе "ru"
|
| 31 |
-
|
| 32 |
-
# ==========================
|
| 33 |
-
# Routes
|
| 34 |
-
# ==========================
|
| 35 |
@app.get("/")
|
| 36 |
-
def root():
|
| 37 |
-
return
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 61 |
return {"summary": summary}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import FileResponse
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
|
| 6 |
+
from summarizer import generate_summary
|
| 7 |
+
from pdf_reader import extract_text_from_pdf
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
# CORS (frontend ↔ backend)
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Барлық файлдар бір папкада → static ретінде қосамыз
|
| 20 |
+
app.mount("/static", StaticFiles(directory="."), name="static")
|
| 21 |
+
|
| 22 |
+
# Frontend main page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
@app.get("/")
|
| 24 |
+
async def root():
|
| 25 |
+
return FileResponse("index.html")
|
| 26 |
+
|
| 27 |
+
# TEXT summarization
|
| 28 |
+
@app.post("/summarize/text")
|
| 29 |
+
async def summarize_text(data: dict):
|
| 30 |
+
text = data.get("text", "")
|
| 31 |
+
if not text.strip():
|
| 32 |
+
return {"summary": "No text provided."}
|
| 33 |
+
|
| 34 |
+
summary = generate_summary(text)
|
| 35 |
+
return {"summary": summary}
|
| 36 |
+
|
| 37 |
+
# PDF summarization
|
| 38 |
+
@app.post("/summarize/pdf")
|
| 39 |
+
async def summarize_pdf(file: UploadFile = File(...)):
|
| 40 |
+
pdf_bytes = await file.read()
|
| 41 |
+
text = extract_text_from_pdf(pdf_bytes)
|
| 42 |
+
|
| 43 |
+
if not text.strip():
|
| 44 |
+
return {"summary": "PDF is empty or could not be processed."}
|
| 45 |
+
|
| 46 |
+
summary = generate_summary(text)
|
|
|
|
|
|
|
| 47 |
return {"summary": summary}
|