Thompson001 commited on
Commit
9840ff2
·
verified ·
1 Parent(s): a88c5e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -32
app.py CHANGED
@@ -1,15 +1,15 @@
1
  # app.py
2
- from fastapi import FastAPI, File, UploadFile
3
  from fastapi.middleware.cors import CORSMiddleware
4
- from ultralytics import YOLO
5
  import uvicorn
6
- import numpy as np
7
  from PIL import Image
8
  import io
 
9
 
10
  app = FastAPI()
11
 
12
- # CORS 활성화 (ConcreteAI 웹과 연결)
13
  app.add_middleware(
14
  CORSMiddleware,
15
  allow_origins=["*"],
@@ -18,46 +18,47 @@ app.add_middleware(
18
  allow_headers=["*"],
19
  )
20
 
21
- # ---- YOLOv8 모델 로드 ----
22
- print("🔵 Loading YOLOv8 crack model...")
23
- model = YOLO("keremberke/yolov8n-concrete-crack")
24
- print("✅ Model loaded!")
 
 
 
 
25
 
26
  @app.post("/predict")
27
  async def predict(img: UploadFile = File(...)):
28
  # 이미지 읽기
29
- image_bytes = await img.read()
30
- image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
31
- np_img = np.array(image)
32
-
33
- # YOLOv8 추론
34
- results = model(np_img)[0]
35
-
36
- # 박스 리스트
37
- detections = results.boxes
38
-
39
- if detections is None or len(detections) == 0:
40
- # 균열 없음
41
- return {
42
- "data": [
43
- {
44
- "label": "normal",
45
- "confidence": 1.0
46
- }
47
- ]
48
- }
49
-
50
- # 가장 높은 confidence 선택
51
- max_conf = float(max(d.conf[0].item() for d in detections))
52
 
53
  return {
54
  "data": [
55
  {
56
  "label": "crack",
57
- "confidence": max_conf
58
  }
59
  ]
60
  }
61
 
 
62
  if __name__ == "__main__":
63
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  # app.py
2
+ from fastapi import FastAPI, UploadFile, File
3
  from fastapi.middleware.cors import CORSMiddleware
4
+ import requests
5
  import uvicorn
 
6
  from PIL import Image
7
  import io
8
+ import base64
9
 
10
  app = FastAPI()
11
 
12
+ # CORS 허용
13
  app.add_middleware(
14
  CORSMiddleware,
15
  allow_origins=["*"],
 
18
  allow_headers=["*"],
19
  )
20
 
21
+ # HuggingFace 모델 엔드포인트
22
+ HF_API_URL = "https://api-inference.huggingface.co/models/keremberke/yolov8n-concrete-crack"
23
+ HF_TOKEN = "YOUR_HF_TOKEN" # 반드시 입력 필요
24
+
25
+ headers = {
26
+ "Authorization": f"Bearer {HF_TOKEN}"
27
+ }
28
+
29
 
30
  @app.post("/predict")
31
  async def predict(img: UploadFile = File(...)):
32
  # 이미지 읽기
33
+ bytes_data = await img.read()
34
+
35
+ response = requests.post(
36
+ HF_API_URL,
37
+ headers=headers,
38
+ data=bytes_data
39
+ )
40
+
41
+ try:
42
+ results = response.json()
43
+ except:
44
+ return {"data": [{"label": "normal", "confidence": 1.0}]}
45
+
46
+ # 결과가 bounding box 리스트
47
+ if not isinstance(results, list) or len(results) == 0:
48
+ return {"data": [{"label": "normal", "confidence": 1.0}]}
49
+
50
+ # confidence 최고값 찾기
51
+ max_conf = max(item.get("score", 0) for item in results)
 
 
 
 
52
 
53
  return {
54
  "data": [
55
  {
56
  "label": "crack",
57
+ "confidence": float(max_conf)
58
  }
59
  ]
60
  }
61
 
62
+
63
  if __name__ == "__main__":
64
  uvicorn.run(app, host="0.0.0.0", port=7860)