Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
c7f4ae7b
Unverified
Commit
c7f4ae7b
authored
May 12, 2025
by
Jeffrey Morgan
Committed by
GitHub
May 12, 2025
Browse files
server: add webp image input support (#10653)
parent
526b2ed1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
7 deletions
+24
-7
cmd/interactive.go
cmd/interactive.go
+3
-3
cmd/interactive_test.go
cmd/interactive_test.go
+15
-4
server/routes.go
server/routes.go
+6
-0
No files found.
cmd/interactive.go
View file @
c7f4ae7b
...
@@ -44,7 +44,7 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error {
...
@@ -44,7 +44,7 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error {
fmt
.
Fprintln
(
os
.
Stderr
,
"Use
\"\"\"
to begin a multi-line message."
)
fmt
.
Fprintln
(
os
.
Stderr
,
"Use
\"\"\"
to begin a multi-line message."
)
if
opts
.
MultiModal
{
if
opts
.
MultiModal
{
fmt
.
Fprintf
(
os
.
Stderr
,
"Use %s to include .jpg or .
png
images.
\n
"
,
filepath
.
FromSlash
(
"/path/to/file"
))
fmt
.
Fprintf
(
os
.
Stderr
,
"Use %s to include .jpg
, .png,
or .
webp
images.
\n
"
,
filepath
.
FromSlash
(
"/path/to/file"
))
}
}
fmt
.
Fprintln
(
os
.
Stderr
,
""
)
fmt
.
Fprintln
(
os
.
Stderr
,
""
)
...
@@ -511,7 +511,7 @@ func extractFileNames(input string) []string {
...
@@ -511,7 +511,7 @@ func extractFileNames(input string) []string {
// Regex to match file paths starting with optional drive letter, / ./ \ or .\ and include escaped or unescaped spaces (\ or %20)
// Regex to match file paths starting with optional drive letter, / ./ \ or .\ and include escaped or unescaped spaces (\ or %20)
// and followed by more characters and a file extension
// and followed by more characters and a file extension
// This will capture non filename strings, but we'll check for file existence to remove mismatches
// This will capture non filename strings, but we'll check for file existence to remove mismatches
regexPattern
:=
`(?:[a-zA-Z]:)?(?:\./|/|\\)[\S\\ ]+?\.(?i:jpg|jpeg|png)\b`
regexPattern
:=
`(?:[a-zA-Z]:)?(?:\./|/|\\)[\S\\ ]+?\.(?i:jpg|jpeg|png
|webp
)\b`
re
:=
regexp
.
MustCompile
(
regexPattern
)
re
:=
regexp
.
MustCompile
(
regexPattern
)
return
re
.
FindAllString
(
input
,
-
1
)
return
re
.
FindAllString
(
input
,
-
1
)
...
@@ -553,7 +553,7 @@ func getImageData(filePath string) ([]byte, error) {
...
@@ -553,7 +553,7 @@ func getImageData(filePath string) ([]byte, error) {
}
}
contentType
:=
http
.
DetectContentType
(
buf
)
contentType
:=
http
.
DetectContentType
(
buf
)
allowedTypes
:=
[]
string
{
"image/jpeg"
,
"image/jpg"
,
"image/png"
}
allowedTypes
:=
[]
string
{
"image/jpeg"
,
"image/jpg"
,
"image/png"
,
"image/webp"
}
if
!
slices
.
Contains
(
allowedTypes
,
contentType
)
{
if
!
slices
.
Contains
(
allowedTypes
,
contentType
)
{
return
nil
,
fmt
.
Errorf
(
"invalid image type: %s"
,
contentType
)
return
nil
,
fmt
.
Errorf
(
"invalid image type: %s"
,
contentType
)
}
}
...
...
cmd/interactive_test.go
View file @
c7f4ae7b
...
@@ -12,14 +12,17 @@ func TestExtractFilenames(t *testing.T) {
...
@@ -12,14 +12,17 @@ func TestExtractFilenames(t *testing.T) {
// Unix style paths
// Unix style paths
input
:=
` some preamble
input
:=
` some preamble
./relative\ path/one.png inbetween1 ./not a valid two.jpg inbetween2 ./1.svg
./relative\ path/one.png inbetween1 ./not a valid two.jpg inbetween2 ./1.svg
/unescaped space /three.jpeg inbetween3 /valid\ path/dir/four.png "./quoted with spaces/five.JPG`
/unescaped space /three.jpeg inbetween3 /valid\ path/dir/four.png "./quoted with spaces/five.JPG
/unescaped space /six.webp inbetween6 /valid\ path/dir/seven.WEBP`
res
:=
extractFileNames
(
input
)
res
:=
extractFileNames
(
input
)
assert
.
Len
(
t
,
res
,
5
)
assert
.
Len
(
t
,
res
,
7
)
assert
.
Contains
(
t
,
res
[
0
],
"one.png"
)
assert
.
Contains
(
t
,
res
[
0
],
"one.png"
)
assert
.
Contains
(
t
,
res
[
1
],
"two.jpg"
)
assert
.
Contains
(
t
,
res
[
1
],
"two.jpg"
)
assert
.
Contains
(
t
,
res
[
2
],
"three.jpeg"
)
assert
.
Contains
(
t
,
res
[
2
],
"three.jpeg"
)
assert
.
Contains
(
t
,
res
[
3
],
"four.png"
)
assert
.
Contains
(
t
,
res
[
3
],
"four.png"
)
assert
.
Contains
(
t
,
res
[
4
],
"five.JPG"
)
assert
.
Contains
(
t
,
res
[
4
],
"five.JPG"
)
assert
.
Contains
(
t
,
res
[
5
],
"six.webp"
)
assert
.
Contains
(
t
,
res
[
6
],
"seven.WEBP"
)
assert
.
NotContains
(
t
,
res
[
4
],
'"'
)
assert
.
NotContains
(
t
,
res
[
4
],
'"'
)
assert
.
NotContains
(
t
,
res
,
"inbetween1"
)
assert
.
NotContains
(
t
,
res
,
"inbetween1"
)
assert
.
NotContains
(
t
,
res
,
"./1.svg"
)
assert
.
NotContains
(
t
,
res
,
"./1.svg"
)
...
@@ -30,10 +33,12 @@ func TestExtractFilenames(t *testing.T) {
...
@@ -30,10 +33,12 @@ func TestExtractFilenames(t *testing.T) {
/absolute/nospace/three.jpeg inbetween3 /absolute/with space/four.png inbetween4
/absolute/nospace/three.jpeg inbetween3 /absolute/with space/four.png inbetween4
./relative\ path/five.JPG inbetween5 "./relative with/spaces/six.png inbetween6
./relative\ path/five.JPG inbetween5 "./relative with/spaces/six.png inbetween6
d:\path with\spaces\seven.JPEG inbetween7 c:\users\jdoe\eight.png inbetween8
d:\path with\spaces\seven.JPEG inbetween7 c:\users\jdoe\eight.png inbetween8
d:\program files\someplace\nine.png inbetween9 "E:\program files\someplace\ten.PNG some ending
d:\program files\someplace\nine.png inbetween9 "E:\program files\someplace\ten.PNG
c:/users/jdoe/eleven.webp inbetween11 c:/program files/someplace/twelve.WebP inbetween12
d:\path with\spaces\thirteen.WEBP some ending
`
`
res
=
extractFileNames
(
input
)
res
=
extractFileNames
(
input
)
assert
.
Len
(
t
,
res
,
1
0
)
assert
.
Len
(
t
,
res
,
1
3
)
assert
.
NotContains
(
t
,
res
,
"inbetween2"
)
assert
.
NotContains
(
t
,
res
,
"inbetween2"
)
assert
.
Contains
(
t
,
res
[
0
],
"one.png"
)
assert
.
Contains
(
t
,
res
[
0
],
"one.png"
)
assert
.
Contains
(
t
,
res
[
0
],
"c:"
)
assert
.
Contains
(
t
,
res
[
0
],
"c:"
)
...
@@ -51,6 +56,12 @@ d:\path with\spaces\seven.JPEG inbetween7 c:\users\jdoe\eight.png inbetween8
...
@@ -51,6 +56,12 @@ d:\path with\spaces\seven.JPEG inbetween7 c:\users\jdoe\eight.png inbetween8
assert
.
Contains
(
t
,
res
[
8
],
"d:"
)
assert
.
Contains
(
t
,
res
[
8
],
"d:"
)
assert
.
Contains
(
t
,
res
[
9
],
"ten.PNG"
)
assert
.
Contains
(
t
,
res
[
9
],
"ten.PNG"
)
assert
.
Contains
(
t
,
res
[
9
],
"E:"
)
assert
.
Contains
(
t
,
res
[
9
],
"E:"
)
assert
.
Contains
(
t
,
res
[
10
],
"eleven.webp"
)
assert
.
Contains
(
t
,
res
[
10
],
"c:"
)
assert
.
Contains
(
t
,
res
[
11
],
"twelve.WebP"
)
assert
.
Contains
(
t
,
res
[
11
],
"c:"
)
assert
.
Contains
(
t
,
res
[
12
],
"thirteen.WEBP"
)
assert
.
Contains
(
t
,
res
[
12
],
"d:"
)
}
}
// Ensure that file paths wrapped in single quotes are removed with the quotes.
// Ensure that file paths wrapped in single quotes are removed with the quotes.
...
...
server/routes.go
View file @
c7f4ae7b
...
@@ -8,6 +8,7 @@ import (
...
@@ -8,6 +8,7 @@ import (
"encoding/json"
"encoding/json"
"errors"
"errors"
"fmt"
"fmt"
"image"
"io"
"io"
"io/fs"
"io/fs"
"log/slog"
"log/slog"
...
@@ -25,6 +26,7 @@ import (
...
@@ -25,6 +26,7 @@ import (
"github.com/gin-contrib/cors"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"golang.org/x/image/webp"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/errgroup"
"github.com/ollama/ollama/api"
"github.com/ollama/ollama/api"
...
@@ -1304,6 +1306,10 @@ func Serve(ln net.Listener) error {
...
@@ -1304,6 +1306,10 @@ func Serve(ln net.Listener) error {
s
.
sched
.
Run
(
schedCtx
)
s
.
sched
.
Run
(
schedCtx
)
// register the experimental webp decoder
// so webp images can be used in multimodal inputs
image
.
RegisterFormat
(
"webp"
,
"RIFF????WEBP"
,
webp
.
Decode
,
webp
.
DecodeConfig
)
// At startup we retrieve GPU information so we can get log messages before loading a model
// At startup we retrieve GPU information so we can get log messages before loading a model
// This will log warnings to the log in case we have problems with detected GPUs
// This will log warnings to the log in case we have problems with detected GPUs
gpus
:=
discover
.
GetGPUInfo
()
gpus
:=
discover
.
GetGPUInfo
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment