mirror of
https://github.com/open-webui/pipelines
synced 2025-06-26 18:15:58 +00:00
refac
This commit is contained in:
parent
88f3d59fcb
commit
cabd666152
58
main.py
58
main.py
@ -42,25 +42,10 @@ PIPELINES = {}
|
|||||||
PIPELINE_MODULES = {}
|
PIPELINE_MODULES = {}
|
||||||
|
|
||||||
|
|
||||||
def on_startup():
|
def get_all_pipelines():
|
||||||
def load_modules_from_directory(directory):
|
pipelines = {}
|
||||||
for filename in os.listdir(directory):
|
for pipeline_id in PIPELINE_MODULES.keys():
|
||||||
if filename.endswith(".py"):
|
pipeline = PIPELINE_MODULES[pipeline_id]
|
||||||
module_name = filename[:-3] # Remove the .py extension
|
|
||||||
module_path = os.path.join(directory, filename)
|
|
||||||
spec = importlib.util.spec_from_file_location(module_name, module_path)
|
|
||||||
module = importlib.util.module_from_spec(spec)
|
|
||||||
spec.loader.exec_module(module)
|
|
||||||
yield module
|
|
||||||
|
|
||||||
for loaded_module in load_modules_from_directory("./pipelines"):
|
|
||||||
# Do something with the loaded module
|
|
||||||
logging.info("Loaded:", loaded_module.__name__)
|
|
||||||
|
|
||||||
pipeline = loaded_module.Pipeline()
|
|
||||||
pipeline_id = pipeline.id if hasattr(pipeline, "id") else loaded_module.__name__
|
|
||||||
|
|
||||||
PIPELINE_MODULES[pipeline_id] = pipeline
|
|
||||||
|
|
||||||
if hasattr(pipeline, "type"):
|
if hasattr(pipeline, "type"):
|
||||||
if pipeline.type == "manifold":
|
if pipeline.type == "manifold":
|
||||||
@ -73,7 +58,7 @@ def on_startup():
|
|||||||
f"{pipeline.name}{manifold_pipeline_name}"
|
f"{pipeline.name}{manifold_pipeline_name}"
|
||||||
)
|
)
|
||||||
|
|
||||||
PIPELINES[manifold_pipeline_id] = {
|
pipelines[manifold_pipeline_id] = {
|
||||||
"module": pipeline_id,
|
"module": pipeline_id,
|
||||||
"type": pipeline.type if hasattr(pipeline, "type") else "pipe",
|
"type": pipeline.type if hasattr(pipeline, "type") else "pipe",
|
||||||
"id": manifold_pipeline_id,
|
"id": manifold_pipeline_id,
|
||||||
@ -83,7 +68,7 @@ def on_startup():
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
if pipeline.type == "filter":
|
if pipeline.type == "filter":
|
||||||
PIPELINES[pipeline_id] = {
|
pipelines[pipeline_id] = {
|
||||||
"module": pipeline_id,
|
"module": pipeline_id,
|
||||||
"type": (pipeline.type if hasattr(pipeline, "type") else "pipe"),
|
"type": (pipeline.type if hasattr(pipeline, "type") else "pipe"),
|
||||||
"id": pipeline_id,
|
"id": pipeline_id,
|
||||||
@ -105,7 +90,7 @@ def on_startup():
|
|||||||
"valves": pipeline.valves if hasattr(pipeline, "valves") else None,
|
"valves": pipeline.valves if hasattr(pipeline, "valves") else None,
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
PIPELINES[pipeline_id] = {
|
pipelines[pipeline_id] = {
|
||||||
"module": pipeline_id,
|
"module": pipeline_id,
|
||||||
"type": (pipeline.type if hasattr(pipeline, "type") else "pipe"),
|
"type": (pipeline.type if hasattr(pipeline, "type") else "pipe"),
|
||||||
"id": pipeline_id,
|
"id": pipeline_id,
|
||||||
@ -113,6 +98,31 @@ def on_startup():
|
|||||||
"valves": pipeline.valves if hasattr(pipeline, "valves") else None,
|
"valves": pipeline.valves if hasattr(pipeline, "valves") else None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return pipelines
|
||||||
|
|
||||||
|
|
||||||
|
def on_startup():
|
||||||
|
def load_modules_from_directory(directory):
|
||||||
|
for filename in os.listdir(directory):
|
||||||
|
if filename.endswith(".py"):
|
||||||
|
module_name = filename[:-3] # Remove the .py extension
|
||||||
|
module_path = os.path.join(directory, filename)
|
||||||
|
spec = importlib.util.spec_from_file_location(module_name, module_path)
|
||||||
|
module = importlib.util.module_from_spec(spec)
|
||||||
|
spec.loader.exec_module(module)
|
||||||
|
yield module
|
||||||
|
|
||||||
|
for loaded_module in load_modules_from_directory("./pipelines"):
|
||||||
|
# Do something with the loaded module
|
||||||
|
logging.info("Loaded:", loaded_module.__name__)
|
||||||
|
|
||||||
|
pipeline = loaded_module.Pipeline()
|
||||||
|
pipeline_id = pipeline.id if hasattr(pipeline, "id") else loaded_module.__name__
|
||||||
|
|
||||||
|
PIPELINE_MODULES[pipeline_id] = pipeline
|
||||||
|
|
||||||
|
PIPELINES = get_all_pipelines()
|
||||||
|
|
||||||
|
|
||||||
on_startup()
|
on_startup()
|
||||||
|
|
||||||
@ -162,6 +172,8 @@ async def get_models():
|
|||||||
"""
|
"""
|
||||||
Returns the available pipelines
|
Returns the available pipelines
|
||||||
"""
|
"""
|
||||||
|
app.state.PIPELINES = get_all_pipelines()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"data": [
|
"data": [
|
||||||
{
|
{
|
||||||
@ -190,6 +202,8 @@ async def get_models():
|
|||||||
|
|
||||||
@app.get("/{pipeline_id}/valves")
|
@app.get("/{pipeline_id}/valves")
|
||||||
async def get_valves(pipeline_id: str):
|
async def get_valves(pipeline_id: str):
|
||||||
|
app.state.PIPELINES = get_all_pipelines()
|
||||||
|
|
||||||
if pipeline_id not in app.state.PIPELINES:
|
if pipeline_id not in app.state.PIPELINES:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
Loading…
Reference in New Issue
Block a user