• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / pipcook / 4088643824

pending completion
4088643824

Pull #951

github

GitHub
Merge 3588a38ba into c1acf7dc8
Pull Request #951: build(deps): bump http-cache-semantics from 4.1.0 to 4.1.1 in /packages/cli

436 of 530 branches covered (82.26%)

Branch coverage included in aggregate %.

692 of 736 relevant lines covered (94.02%)

4.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/packages/cli/src/utils/serve-predict.ts
1
import * as express from 'express';
10✔
2
import { Express, Request, Response } from 'express';
3
import * as multer from 'multer';
10✔
4
import * as path from 'path';
10✔
5
import * as http from 'http';
6
import { PipelineType } from '@pipcook/costa';
10✔
7

8
let server: http.Server;
9
const ServeMap = {
10✔
10
  [PipelineType.TextClassification]: serveText,
11
  [PipelineType.ImageClassification]: serveImage,
12
  [PipelineType.ObjectDetection]: serveImage
13
};
14

15
export type PredictCallBack
16
  = (input: Buffer[] | string[]) => Promise<Record<string, any>[]>;
17

18
/**
19
 * Serve model.
20
 * @param port listen port.
21
 * @param pipelineType pipeline type.
22
 * @param predictCallback callback for predict.
23
 */
24
export async function serve(
10✔
25
  port: number,
26
  pipelineType: PipelineType,
27
  predictCallback: PredictCallBack
28
): Promise<void> {
29
  if (!ServeMap[pipelineType]) {
2✔
30
    throw new TypeError(`Pipeline type is not supported: ${pipelineType}`);
1✔
31
  }
32

33
  const app = express();
1✔
34
  ServeMap[pipelineType](app, predictCallback);
1✔
35
  return new Promise<void>((resolve) => {
1✔
36
    server = app.listen(port, () => {
1✔
37
      resolve();
1✔
38
    });
39
  });
40
}
41

42
export async function stop(): Promise<void> {
10✔
43
  if (server) {
1!
44
    return new Promise<void>((resolve, reject) => {
1✔
45
      server.close((err?: Error) => {
1✔
46
        server = undefined;
1✔
47
        if (err) {
1!
48
          reject(err);
×
49
        } else {
50
          resolve();
1✔
51
        }
52
      });
53
    });
54
  }
55
}
56

57
export async function predictText(
10✔
58
  predictCallback: PredictCallBack,
59
  req: Request,
60
  res: Response
61
): Promise<void> {
62
  if (req.query && req.query['input']) {
2✔
63
    let inputs: string[];
64
    if (Array.isArray(req.query['input'])) {
1!
65
      inputs = req.query['input'] as string[];
1✔
66
    } else if (typeof req.query['input'] === 'string') {
×
67
      inputs = [ req.query['input'] ];
×
68
    }
69
    const result = await predictCallback(inputs);
1✔
70
    res.json({ success: true, data: result });
1✔
71
  } else {
72
    res.json({ success: false, message: 'no input available' });
1✔
73
  }
74
}
75

76
export function serveText(
10✔
77
  app: Express,
78
  predictCallback: PredictCallBack
79
): void {
80
  app.use(express.static(path.join(__dirname, '../../serve-resource/text')))
2✔
81
    .get('/predict', predictText.bind(this, predictCallback));
82
}
83

84
export async function predictImage(
10✔
85
  predictCallback: PredictCallBack,
86
  req: Request,
87
  res: Response
88
): Promise<void> {
89
  let buf: Buffer[];
90
  if (Array.isArray(req.files)) {
2✔
91
    buf = (req.files as Express.Multer.File[]).map((file) => file.buffer);
2✔
92
  }
93

94
  if (buf) {
2✔
95
    const result = await predictCallback(buf);
1✔
96
    res.json({ success: true, data: result });
1✔
97
  } else {
98
    res.json({ success: false, message: 'no file available' });
1✔
99
  }
100
}
101

102
export function serveImage(
10✔
103
  app: Express,
104
  predictCallback: PredictCallBack
105
): void {
106
  const upload = multer({ storage: multer.memoryStorage() });
1✔
107
  app.use(express.static(path.join(__dirname, '../../serve-resource/image')))
1✔
108
    .post('/predict', upload.array('image'), predictImage.bind(this, predictCallback));
109
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc