Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
V
VIPSCore-Python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
VIPS
VIPSCore-Python
Merge requests
!2
Model factory
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Model factory
model_factory
into
main
Overview
0
Commits
7
Pipelines
0
Changes
2
Merged
Tor-Einar Skog
requested to merge
model_factory
into
main
2 years ago
Overview
0
Commits
7
Pipelines
0
Changes
2
Expand
0
0
Merge request reports
Viewing commit
c7b0e556
Prev
Next
Show latest version
2 files
+
82
−
32
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
c7b0e556
First version where run model endpoint works
· c7b0e556
Tor-Einar Skog
authored
2 years ago
app/main.py
0 → 100644
+
94
−
0
Options
from
fastapi
import
FastAPI
from
fastapi.responses
import
PlainTextResponse
from
app.internal.model_factory
import
*
from
vipscore_common.vips_model
import
VIPSModel
from
vipscore_common.entities
import
ModelConfiguration
,
Result
description
=
"""
Python implementation of VIPSCore, to enable models written in Python.
"""
app
=
FastAPI
(
title
=
"
VIPSCore-Python
"
,
description
=
description
,
contact
=
{
"
name
"
:
"
Berit Nordskog
"
,
"
email
"
:
"
post@vips.no
"
,
"
url
"
:
"
https://nibio.no/en/employees/berit-nordskog
"
,
},
license_info
=
{
"
name
"
:
"
GNU Affero General Public License v3
"
,
"
url
"
:
"
https://www.gnu.org/licenses/agpl-3.0.en.html
"
}
)
# TODO Create a nice welcome page
@app.get
(
"
/
"
)
async
def
root
():
return
{
"
message
"
:
"
Hello World
"
}
####### Model listing endpoints #######
@app.get
(
"
/models/json
"
,
name
=
"
List all models (Json, default language)
"
)
async
def
print_model_list_json_default_language
():
return
_print_model_list_json
(
VIPSModel
.
default_language
)
@app.get
(
"
/models/json/{language}
"
,
name
=
"
List all models (Json)
"
)
async
def
print_model_list_json
(
language
:
str
):
return
_print_model_list_json
(
language
)
@app.get
(
"
/models
"
,
response_class
=
PlainTextResponse
,
name
=
"
List all models (plain text, default language)
"
)
async
def
print_model_list_default_language
()
->
str
:
return
_print_model_list
(
VIPSModel
.
default_language
)
@app.get
(
"
/models/{language}
"
,
response_class
=
PlainTextResponse
,
name
=
"
List all models (plain text)
"
)
async
def
print_model_list
(
language
:
str
)
->
str
:
return
_print_model_list
(
language
)
####### Model running endpoints #######
@app.post
(
"
/models/run/
"
,
name
=
"
Run a model
"
)
async
def
run_model_from_config_only
(
model_configuration
:
ModelConfiguration
):
return
_run_model
(
model_configuration
.
model_id
,
model_configuration
)
@app.post
(
"
/models/{model_id}/run/
"
,
name
=
"
Run a model
"
)
async
def
run_model_from_config_only
(
model_id
,
model_configuration
:
ModelConfiguration
):
return
_run_model
(
model_id
,
model_configuration
)
####### Helper functions #######
# Dealing with no method overload in Python :-)
def
_run_model
(
model_id
:
str
,
model_configuration
:
ModelConfiguration
):
"""
Run a model, using provided model_id and ModelConfiguration
Return a list of Result objects
"""
requested_model
=
get_model_instance
(
model_id
)
requested_model
.
set_configuration
(
model_configuration
)
return
requested_model
.
get_result
()
def
_print_model_list
(
language
:
str
):
"""
Returns the list of available models in plain text format
"""
model_list
=
""
vips_models
=
get_vips_models
()
for
model_id
in
vips_models
:
model_list
=
"
%s%s %s
\n
"
%
(
model_list
,
model_id
,
get_model_instance
(
model_id
).
get_model_name
(
language
))
return
model_list
def
_print_model_list_json
(
language
:
str
):
"""
Returns the list of available models in Json format
"""
model_list
=
[]
vips_models
=
get_vips_models
()
for
model_id
in
vips_models
:
model_list
.
append
({
"
modelId
"
:
model_id
,
"
modelName
"
:
get_model_instance
(
model_id
).
get_model_name
()
})
return
model_list
\ No newline at end of file
Loading