coycs commited on
Commit
0da9ad7
1 Parent(s): bef22ed

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +28 -0
  2. app.py +194 -0
  3. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python 3.9 image
2
+ FROM python:3.8.6
3
+
4
+ # Set the working directory to /code
5
+ WORKDIR /code
6
+
7
+ # Copy the current directory contents into the container at /code
8
+ COPY ./requirements.txt /code/requirements.txt
9
+
10
+ # Install requirements.txt
11
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
12
+
13
+ # Set up a new user named "user" with user ID 1000
14
+ RUN useradd -m -u 1000 user
15
+ # Switch to the "user" user
16
+ USER user
17
+ # Set home to the user's home directory
18
+ ENV HOME=/home/user \\
19
+ PATH=/home/user/.local/bin:$PATH
20
+
21
+ # Set the working directory to the user's home directory
22
+ WORKDIR $HOME/app
23
+
24
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
25
+ COPY --chown=user . $HOME/app
26
+
27
+ # Start the FastAPI app on port 7860, the default port expected by Spaces
28
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 返回图片测试
2
+ from sklearn.linear_model import LinearRegression
3
+ from sklearn.neural_network import MLPRegressor
4
+ import lightgbm as lgb
5
+ from xgboost import XGBRegressor
6
+ from sklearn.ensemble import RandomForestRegressor
7
+ from sklearn.preprocessing import StandardScaler
8
+ from sklearn.model_selection import train_test_split
9
+ import pandas as pd
10
+ from fastapi.middleware.cors import CORSMiddleware # 跨域
11
+ from fastapi import FastAPI, Response, BackgroundTasks
12
+ import json
13
+ import matplotlib.pyplot as plt
14
+ import io
15
+ import matplotlib
16
+ matplotlib.use('AGG')
17
+
18
+ app = FastAPI()
19
+
20
+ # 配置跨域白名单
21
+ origins = [
22
+ "http://127.0.0.1:5500"
23
+ ]
24
+
25
+ # 图片测试
26
+ app.add_middleware(
27
+ CORSMiddleware,
28
+ allow_origins=origins,
29
+ allow_credentials=True,
30
+ allow_methods=["POST"],
31
+ allow_headers=["*"],
32
+ )
33
+
34
+ def create_img():
35
+ plt.rcParams['figure.figsize'] = [7.50, 3.50]
36
+ plt.rcParams['figure.autolayout'] = True
37
+ plt.plot([1, 2])
38
+ img_buf = io.BytesIO()
39
+ plt.savefig(img_buf, format='png')
40
+ plt.close()
41
+ return img_buf
42
+
43
+ @app.get('/png')
44
+ async def get_img(background_tasks: BackgroundTasks):
45
+ img_buf = create_img()
46
+ # get the entire buffer content
47
+ # because of the async, this will await the loading of all content
48
+ bufContents: bytes = img_buf.getvalue()
49
+ background_tasks.add_task(img_buf.close)
50
+ headers = {'Content-Disposition': 'inline; filename="out.png"'}
51
+ return Response(bufContents, headers=headers, media_type='image/png')
52
+
53
+
54
+ # 机器学习测试
55
+ # 多元线性回归
56
+ @app.post("/mlr")
57
+ async def mlr():
58
+ # 引入excel数据
59
+ # 可能存在空行问题,dropna(axis=0)删除空行
60
+ df = pd.read_csv("./1.csv").dropna(axis=0)
61
+ # 取xy
62
+ x = df.iloc[:, :12]
63
+ # y = df.loc[:, "BSR"]
64
+ y = df.loc[:, "SBR"]
65
+ # 划分数据集
66
+ x_train, x_test, y_train, y_test = train_test_split(
67
+ x, y, test_size=0.3, random_state=0)
68
+
69
+ # 标准化
70
+ standardscaler = StandardScaler()
71
+ standardscaler.fit(x_train)
72
+ x_train = standardscaler.transform(x_train)
73
+ x_test = standardscaler.transform(x_test)
74
+
75
+ # 多元线性回归
76
+ model = LinearRegression()
77
+ model.fit(x_train, y_train)
78
+
79
+ # 测试样本预测
80
+ y_pred = model.predict(x_test)
81
+ return json.dumps(y_pred.tolist())
82
+
83
+ # 随机森林
84
+ @app.post("/rf")
85
+ async def rf():
86
+ # 引入excel数据
87
+ # 可能存在空行问题,dropna(axis=0)删除空行
88
+ df = pd.read_csv("./1.csv").dropna(axis=0)
89
+ # 取xy
90
+ x = df.iloc[:, :12]
91
+ # y = df.loc[:, "BSR"]
92
+ y = df.loc[:, "SBR"]
93
+ # 划分数据集
94
+ x_train, x_test, y_train, y_test = train_test_split(
95
+ x, y, test_size=0.3, random_state=0)
96
+
97
+ # 标准化
98
+ standardscaler = StandardScaler()
99
+ standardscaler.fit(x_train)
100
+ x_train = standardscaler.transform(x_train)
101
+ x_test = standardscaler.transform(x_test)
102
+
103
+ # 随机森林
104
+ model = RandomForestRegressor()
105
+ model.fit(x_train, y_train)
106
+
107
+ # 测试样本预测
108
+ y_pred = model.predict(x_test)
109
+ return json.dumps(y_pred.tolist())
110
+
111
+ # BP神经网络
112
+ @app.post("/bpn")
113
+ async def rf():
114
+ # 引入excel数据
115
+ # 可能存在空行问题,dropna(axis=0)删除空行
116
+ df = pd.read_csv("./1.csv").dropna(axis=0)
117
+ # 取xy
118
+ x = df.iloc[:, :12]
119
+ # y = df.loc[:, "BSR"]
120
+ y = df.loc[:, "SBR"]
121
+ # 划分数据集
122
+ x_train, x_test, y_train, y_test = train_test_split(
123
+ x, y, test_size=0.3, random_state=0)
124
+
125
+ # 标准化
126
+ standardscaler = StandardScaler()
127
+ standardscaler.fit(x_train)
128
+ x_train = standardscaler.transform(x_train)
129
+ x_test = standardscaler.transform(x_test)
130
+
131
+ # BP神经网络
132
+ model = MLPRegressor(hidden_layer_sizes=(10,), random_state=10,learning_rate_init=0.1) # BP神经网络回归模型
133
+ model.fit(x_train,y_train) # 训练模型
134
+
135
+ # 测试样本预测
136
+ y_pred = model.predict(x_test)
137
+ return json.dumps(y_pred.tolist())
138
+
139
+ # XGBoost
140
+ @app.post("/xgboost")
141
+ async def rf():
142
+ # 引入excel数据
143
+ # 可能存在空行问题,dropna(axis=0)删除空行
144
+ df = pd.read_csv("./1.csv").dropna(axis=0)
145
+ # 取xy
146
+ x = df.iloc[:, :12]
147
+ # y = df.loc[:, "BSR"]
148
+ y = df.loc[:, "SBR"]
149
+ # 划分数据集
150
+ x_train, x_test, y_train, y_test = train_test_split(
151
+ x, y, test_size=0.3, random_state=0)
152
+
153
+ # 标准化
154
+ standardscaler = StandardScaler()
155
+ standardscaler.fit(x_train)
156
+ x_train = standardscaler.transform(x_train)
157
+ x_test = standardscaler.transform(x_test)
158
+
159
+ # XGBoost
160
+ model = XGBRegressor(max_depth=5, learning_rate=0.1, n_estimators=160, objective='reg:gamma')
161
+ model.fit(x_train, y_train)
162
+
163
+ # 测试样本预测
164
+ y_pred = model.predict(x_test)
165
+ return json.dumps(y_pred.tolist())
166
+
167
+ # LightGBM
168
+ @app.post("/lightgbm")
169
+ async def rf():
170
+ # 引入excel数据
171
+ # 可能存在空行问题,dropna(axis=0)删除空行
172
+ df = pd.read_csv("./1.csv").dropna(axis=0)
173
+ # 取xy
174
+ x = df.iloc[:, :12]
175
+ # y = df.loc[:, "BSR"]
176
+ y = df.loc[:, "SBR"]
177
+ # 划分数据集
178
+ x_train, x_test, y_train, y_test = train_test_split(
179
+ x, y, test_size=0.3, random_state=0)
180
+
181
+ # 标准化
182
+ standardscaler = StandardScaler()
183
+ standardscaler.fit(x_train)
184
+ x_train = standardscaler.transform(x_train)
185
+ x_test = standardscaler.transform(x_test)
186
+
187
+ # LightGBM
188
+ model = lgb.LGBMRegressor(objective='regression',num_leaves=31,learning_rate=0.05,n_estimators=20)
189
+ model.fit(x_train, y_train)
190
+
191
+ # 测试样本预测
192
+ y_pred = model.predict(x_test)
193
+ return json.dumps(y_pred.tolist())
194
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi==0.95.1
2
+ lightgbm==3.3.5
3
+ matplotlib==3.5.1
4
+ pandas==1.5.3
5
+ scikit_learn==1.1.1
6
+ xgboost==1.7.5