Skip to content

Commit

Permalink
--line option
Browse files Browse the repository at this point in the history
  • Loading branch information
haifengl committed Feb 7, 2025
1 parent 5af84f1 commit 29e52bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion scala/src/main/scala/smile/cas/Scalar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait Scalar extends Tensor {
/** Returns the derivative. */
def d(dx: Var): Scalar
/** Returns the gradient vector. */
def d(dx: Var*): Vector = Vars(dx.map(d)).simplify
def d(dx: Var*): Vector = Vars(dx.map(d)*).simplify
/** Returns the gradient vector. */
def d(dx: VectorVar): Vector

Expand Down
13 changes: 11 additions & 2 deletions shell/src/main/scala/smile/shell/Serve.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ import smile.model.SmileModel
* Serve command options.
* @param model the model file path.
* @param probability the flag if output posteriori probabilities for soft classifiers.
* @param line the flag if render multiple JSON objects in a line-by-line fashion.
* @param host the IP address to listen on (0.0.0.0 for all available addresses).
* @param port the port number.
*/
case class ServeConfig(model: String,
probability: Boolean = false,
line: Boolean = false,
host: String = "localhost",
port: Int = 8728)

Expand Down Expand Up @@ -81,6 +85,10 @@ object Serve extends LazyLogging {
.optional()
.action((_, c) => c.copy(probability = true))
.text("Output the posteriori probabilities for soft classifier"),
opt[Unit]("line")
.optional()
.action((_, c) => c.copy(line = true))
.text("Render multiple JSON objects in a line-by-line fashion"),
opt[String]("host")
.optional()
.action((x, c) => c.copy(host = x))
Expand Down Expand Up @@ -119,8 +127,9 @@ object Serve extends LazyLogging {
// Source rendering support trait
// Render multiple JSON objects in a line-by-line fashion
val newline = ByteString("\n")
implicit val jsonStreamingSupport = EntityStreamingSupport.json()
.withFramingRenderer(Flow[ByteString].map(bs => bs ++ newline))
val streaming = EntityStreamingSupport.json()
val jsonl = streaming.withFramingRenderer(Flow[ByteString].map(bs => bs ++ newline))
implicit val jsonStreamingSupport = if (config.line) jsonl else streaming

val route = path("v1" / "infer") {
post {
Expand Down

0 comments on commit 29e52bf

Please sign in to comment.