ThanaphonJoe commited on
Commit
b419378
·
verified ·
1 Parent(s): 64fa5e4

upload all files

Browse files
Files changed (4) hide show
  1. Dockerfile +21 -0
  2. iris_random_forest.joblib +3 -0
  3. main.py +63 -0
  4. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # 1. ใช้ base image ของ Python 3.9
3
+ FROM python:3.9-slim
4
+
5
+ # 2. ตั้งค่า working directory ใน container
6
+ WORKDIR /code
7
+
8
+ # 3. คัดลอกไฟล์ requirements.txt เข้าไปก่อน
9
+ COPY ./requirements.txt /code/requirements.txt
10
+
11
+ # 4. ติดตั้ง dependencies จากไฟล์ requirements.txt
12
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
13
+
14
+ # 5. คัดลอกไฟล์โค้ดและโมเดลทั้งหมดเข้าไป
15
+ COPY . /code/
16
+
17
+ # 6. บอกให้ Container เปิด Port 80 รอรับการเชื่อมต่อ
18
+ EXPOSE 80
19
+
20
+ # 7. คำสั่งสำหรับรันแอปพลิเคชันของเรา ใช้ PORT 7860 ซึ่งเป็น defaul port ของ Hugging face Space
21
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
iris_random_forest.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d6ed39853e9da1d351ef1580e5d9cb73c3dc177fad26a346488de9ba5cde359e
3
+ size 186753
main.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # main.api.py
3
+ # นี่คือไฟล์ที่จะใช้รันเป็น Web Server ของเรา
4
+
5
+ from fastapi import FastAPI
6
+ from pydantic import BaseModel
7
+ from joblib import load
8
+ import numpy as np
9
+
10
+ # สร้างแอป FastAPI พร้อมใส่ Metadata ที่สวยงาม
11
+ app = FastAPI(
12
+ title="Iris Species Prediction API",
13
+ description="An API to predict the species of Iris flowers. Created for educational purposes.",
14
+ version="1.0.0"
15
+ )
16
+
17
+ # โหลดโมเดลที่ฝึกไว้
18
+ # โค้ดนี้จะทำงานเมื่อ Server เริ่มต้นขึ้น
19
+ try:
20
+ model = load('iris_random_forest.joblib')
21
+ target_names = ['setosa', 'versicolor', 'virginica']
22
+ except FileNotFoundError:
23
+ model = None
24
+ target_names = []
25
+
26
+ # กำหนดโครงสร้างข้อมูล Input ที่จะรับเข้ามาผ่าน API
27
+ class IrisData(BaseModel):
28
+ sepal_length: float
29
+ sepal_width: float
30
+ petal_length: float
31
+ petal_width: float
32
+
33
+ # สร้าง Endpoint พื้นฐานสำหรับทดสอบ
34
+ @app.get("/")
35
+ def read_root():
36
+ return {"message": "Welcome to the Iris Prediction API! Go to /docs to see the documentation."}
37
+
38
+ # สร้าง Endpoint สำหรับการทำนาย (/predict)
39
+ # @app.post หมายถึงรับข้อมูลผ่าน HTTP POST method
40
+ @app.post("/predict")
41
+ def predict_iris(data: IrisData):
42
+ if model is None:
43
+ return {"error": "Model not found."}
44
+
45
+ # แปลงข้อมูลจาก API เป็น numpy array ที่โมเดลเข้าใจ
46
+ input_data = np.array([[
47
+ data.sepal_length,
48
+ data.sepal_width,
49
+ data.petal_length,
50
+ data.petal_width
51
+ ]])
52
+
53
+ # ทำนายผล
54
+ prediction_index = model.predict(input_data)[0]
55
+ predicted_class_name = target_names[prediction_index]
56
+
57
+ # ส่งผลลัพธ์กลับไปในรูปแบบ JSON
58
+ return {
59
+ "input": data.dict(),
60
+ "predicted_class_index": int(prediction_index),
61
+ "predicted_class_name": predicted_class_name,
62
+ "Joob": 1233
63
+ }
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi==0.111.0
2
+ uvicorn==0.30.1
3
+ scikit-learn==1.6.1
4
+ joblib==1.4.2
5
+ numpy==1.26.4
6
+ pydantic==2.7.4