GexSay commited on
Commit
3bcbec3
·
verified ·
1 Parent(s): 9530cb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -64
app.py CHANGED
@@ -9,14 +9,12 @@ from typing import Optional
9
  from pydantic import BaseModel
10
  from rapidocr_onnxruntime import RapidOCR
11
 
12
- # Khởi tạo FastAPI và OCR engine
13
  app = FastAPI(title="OCR API", description="RapidOCR API Service")
14
  engine = RapidOCR()
15
 
16
  def process_ocr(image: np.ndarray, use_det: bool, use_cls: bool, use_rec: bool) -> list:
17
  """Xử lý OCR và trả về kết quả"""
18
  img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
19
- print(use_det, use_cls, use_rec)
20
  result, _ = engine(img_bgr, use_det=use_det, use_cls=use_cls, use_rec=use_rec)
21
 
22
  if not result:
@@ -41,22 +39,6 @@ def process_ocr(image: np.ndarray, use_det: bool, use_cls: bool, use_rec: bool)
41
 
42
  return texts
43
 
44
- @app.get("/")
45
- async def root():
46
- """Root endpoint - hướng dẫn sử dụng API"""
47
- return {
48
- "message": "OCR API is running",
49
- "endpoints": {
50
- "POST /ocr": "Upload image file for OCR",
51
- "POST /ocr/url": "Provide image URL for OCR",
52
- "GET /health": "Health check"
53
- },
54
- "usage": {
55
- "curl_example": 'curl -X POST https://your-space.hf.space/ocr -F "file=@image.jpg"',
56
- "python_example": "requests.post('https://your-space.hf.space/ocr', files={'file': open('image.jpg', 'rb')})"
57
- }
58
- }
59
-
60
  @app.post("/ocr")
61
  async def ocr_from_file(
62
  file: UploadFile = File(..., description="Image file to process"),
@@ -64,14 +46,10 @@ async def ocr_from_file(
64
  use_cls: bool = Form(True, description="Use classification"),
65
  use_rec: bool = Form(True, description="Use recognition")
66
  ):
67
- """OCR from uploaded image file"""
68
-
69
- # Validate file type
70
  if not file.content_type or not file.content_type.startswith('image/'):
71
  raise HTTPException(status_code=400, detail="File must be an image")
72
 
73
  try:
74
- # Read and process image
75
  contents = await file.read()
76
  image = Image.open(io.BytesIO(contents))
77
 
@@ -91,52 +69,11 @@ async def ocr_from_file(
91
  except Exception as e:
92
  raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
93
 
94
- class OCRUrlRequest(BaseModel):
95
- url: str
96
- use_det: Optional[bool] = True
97
- use_cls: Optional[bool] = True
98
- use_rec: Optional[bool] = True
99
-
100
- @app.post("/ocr/url")
101
- async def ocr_from_url(request: OCRUrlRequest):
102
- """OCR from image URL"""
103
-
104
- try:
105
- # Download image from URL
106
- response = requests.get(request.url, timeout=30)
107
- response.raise_for_status()
108
-
109
- image = Image.open(io.BytesIO(response.content))
110
-
111
- if image.mode != 'RGB':
112
- image = image.convert('RGB')
113
-
114
- img_np = np.array(image)
115
- results = process_ocr(
116
- img_np,
117
- request.use_det,
118
- request.use_cls,
119
- request.use_rec
120
- )
121
-
122
- return JSONResponse(content={
123
- "success": True,
124
- "texts": [item["text"] for item in results],
125
- "details": results,
126
- "num_texts": len(results)
127
- })
128
-
129
- except requests.RequestException as e:
130
- raise HTTPException(status_code=400, detail=f"Failed to fetch image: {str(e)}")
131
- except Exception as e:
132
- raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
133
-
134
  @app.get("/health")
135
  async def health_check():
136
  """Health check endpoint"""
137
- return {"status": "healthy", "service": "RapidOCR API"}
138
 
139
- # Chạy với uvicorn (Hugging Face sẽ tự gọi cái này)
140
  if __name__ == "__main__":
141
  import uvicorn
142
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
9
  from pydantic import BaseModel
10
  from rapidocr_onnxruntime import RapidOCR
11
 
 
12
  app = FastAPI(title="OCR API", description="RapidOCR API Service")
13
  engine = RapidOCR()
14
 
15
  def process_ocr(image: np.ndarray, use_det: bool, use_cls: bool, use_rec: bool) -> list:
16
  """Xử lý OCR và trả về kết quả"""
17
  img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
 
18
  result, _ = engine(img_bgr, use_det=use_det, use_cls=use_cls, use_rec=use_rec)
19
 
20
  if not result:
 
39
 
40
  return texts
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  @app.post("/ocr")
43
  async def ocr_from_file(
44
  file: UploadFile = File(..., description="Image file to process"),
 
46
  use_cls: bool = Form(True, description="Use classification"),
47
  use_rec: bool = Form(True, description="Use recognition")
48
  ):
 
 
 
49
  if not file.content_type or not file.content_type.startswith('image/'):
50
  raise HTTPException(status_code=400, detail="File must be an image")
51
 
52
  try:
 
53
  contents = await file.read()
54
  image = Image.open(io.BytesIO(contents))
55
 
 
69
  except Exception as e:
70
  raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  @app.get("/health")
73
  async def health_check():
74
  """Health check endpoint"""
75
+ return {"status": "healthy", "service": "XOCR AI"}
76
 
 
77
  if __name__ == "__main__":
78
  import uvicorn
79
  uvicorn.run(app, host="0.0.0.0", port=7860)