kawaiipeace commited on
Commit
3a171a3
·
1 Parent(s): a187f48

update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -24
app.py CHANGED
@@ -2,7 +2,6 @@ import os
2
  import tempfile
3
  from fastapi import FastAPI, UploadFile, File, HTTPException, Header
4
  from fastapi.middleware.cors import CORSMiddleware
5
- from fastapi.responses import FileResponse, JSONResponse
6
  from typhoon_ocr import ocr_document
7
  from PyPDF2 import PdfReader
8
  from docx import Document
@@ -69,10 +68,10 @@ async def ocr_endpoint(
69
  x_api_key: str | None = Header(None)
70
  ):
71
  if API_KEY and x_api_key != API_KEY:
72
- return JSONResponse(status_code=401, content={"detail": "API key ผิดพ่อง"})
73
 
74
  if page_num < 0:
75
- return JSONResponse(status_code=400, content={"detail": "เลขหน้าต้องเป็น 0 หรือบวกเท่านั้น"})
76
 
77
  suffix = os.path.splitext(file.filename)[-1].lower()
78
  content = await file.read()
@@ -83,33 +82,15 @@ async def ocr_endpoint(
83
  try:
84
  md, md_path, docx_path = process_file_with_typhoon(tmp_path, task_type, page_num)
85
  except Exception as e:
86
- return JSONResponse(status_code=400, content={"detail": str(e)})
87
 
88
- # ส่งข้อมูล Markdown + ลิงก์ดาวน์โหลดไฟล์
89
  return {
90
  "markdown": md,
91
- "md_download_url": f"/download/md/{os.path.basename(md_path)}",
92
- "docx_download_url": f"/download/docx/{os.path.basename(docx_path)}",
93
  "task_type": task_type,
94
  }
95
 
96
- @app.get("/download/md/{filename}")
97
- async def download_md(filename: str):
98
- tmp_dir = tempfile.gettempdir()
99
- file_path = os.path.join(tmp_dir, filename)
100
- if not os.path.exists(file_path):
101
- raise HTTPException(status_code=404, detail="ไฟล์ .md ไม่เจอ")
102
- return FileResponse(file_path, media_type="text/markdown", filename="ocr_extraction_peace.md")
103
-
104
- @app.get("/download/docx/{filename}")
105
- async def download_docx(filename: str):
106
- tmp_dir = tempfile.gettempdir()
107
- file_path = os.path.join(tmp_dir, filename)
108
- if not os.path.exists(file_path):
109
- raise HTTPException(status_code=404, detail="ไฟล์ .docx ไม่เจอ")
110
- return FileResponse(file_path, media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document", filename="ocr_extraction_peace.docx")
111
-
112
-
113
  def gradio_handler(file, task_type, page_num):
114
  if file is None:
115
  return "❌ อัปโหลดไฟล์ก่อน....", None, None
 
2
  import tempfile
3
  from fastapi import FastAPI, UploadFile, File, HTTPException, Header
4
  from fastapi.middleware.cors import CORSMiddleware
 
5
  from typhoon_ocr import ocr_document
6
  from PyPDF2 import PdfReader
7
  from docx import Document
 
68
  x_api_key: str | None = Header(None)
69
  ):
70
  if API_KEY and x_api_key != API_KEY:
71
+ raise HTTPException(status_code=401, detail="API key ผิดพ่อง")
72
 
73
  if page_num < 0:
74
+ raise HTTPException(status_code=400, detail="เลขหน้าต้องเป็น 0 หรือบวกเท่านั้น")
75
 
76
  suffix = os.path.splitext(file.filename)[-1].lower()
77
  content = await file.read()
 
82
  try:
83
  md, md_path, docx_path = process_file_with_typhoon(tmp_path, task_type, page_num)
84
  except Exception as e:
85
+ raise HTTPException(status_code=400, detail=str(e))
86
 
 
87
  return {
88
  "markdown": md,
89
+ "md_file": md_path,
90
+ "docx_file": docx_path,
91
  "task_type": task_type,
92
  }
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  def gradio_handler(file, task_type, page_num):
95
  if file is None:
96
  return "❌ อัปโหลดไฟล์ก่อน....", None, None