student2222333051 commited on
Commit
9c830d7
·
verified ·
1 Parent(s): b6f911c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -27
main.py CHANGED
@@ -1,47 +1,36 @@
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}
 
 
 
 
1
  from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import FileResponse, JSONResponse
 
3
  from fastapi.staticfiles import StaticFiles
 
4
  from summarizer import generate_summary
5
+ from pdf_reader import extract_text_from_pdf # eğer pdf okuyorsan
6
+ import uvicorn
7
 
8
  app = FastAPI()
9
 
10
+ # serve static files
11
+ app.mount("/static", StaticFiles(directory="static"), name="static")
 
 
 
 
 
 
 
 
12
 
 
13
  @app.get("/")
14
+ def index():
15
+ return FileResponse("static/index.html")
16
 
 
17
  @app.post("/summarize/text")
18
+ async def summarize_text_endpoint(data: dict):
19
  text = data.get("text", "")
20
+ if not text:
21
+ return JSONResponse({"error": "No text provided"}, status_code=400)
 
22
  summary = generate_summary(text)
23
  return {"summary": summary}
24
 
25
+ # PDF endpoint (егер керек болса)
26
  @app.post("/summarize/pdf")
27
+ async def summarize_pdf_endpoint(file: UploadFile = File(...)):
28
  pdf_bytes = await file.read()
29
  text = extract_text_from_pdf(pdf_bytes)
 
30
  if not text.strip():
31
+ return JSONResponse({"error": "Could not extract text"}, status_code=400)
 
32
  summary = generate_summary(text)
33
  return {"summary": summary}
34
+
35
+ if __name__ == "__main__":
36
+ uvicorn.run("main:app", host="0.0.0.0", port=7860)