🐛 fix(endpoints.py): import save_uploaded_file function from cache.base module
🐛 fix(graph/base.py): change file_path assignment to use the new file_path key in value dict 🐛 fix(template/field/base.py): change content key to file_path key in result dict ✨ feat(endpoints.py): add endpoint to upload file ✨ feat(cache/base.py): add function to save uploaded file to cache The save_uploaded_file function was not being imported in the endpoints.py module, causing a NameError when trying to use it. The file_path variable in the Node class was being assigned the value of the content key in the value dict, but it should be assigned the value of the file_path key instead. The content key in the TemplateFieldCreator class was changed to file_path to better reflect its purpose. An endpoint to upload files was added to the endpoints.py module, which uses the save_uploaded_file function from the cache.base module to save the uploaded file to the cache.
This commit is contained in:
parent
5798750b32
commit
28ec8338f0
5 changed files with 33 additions and 10 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
from importlib.metadata import version
|
from importlib.metadata import version
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException, UploadFile
|
||||||
|
|
||||||
from langflow.api.schemas import (
|
from langflow.api.schemas import (
|
||||||
ExportedFlow,
|
ExportedFlow,
|
||||||
|
|
@ -9,6 +9,7 @@ from langflow.api.schemas import (
|
||||||
PredictRequest,
|
PredictRequest,
|
||||||
PredictResponse,
|
PredictResponse,
|
||||||
)
|
)
|
||||||
|
from langflow.cache.base import save_uploaded_file
|
||||||
from langflow.interface.run import process_graph_cached
|
from langflow.interface.run import process_graph_cached
|
||||||
from langflow.interface.types import build_langchain_types_dict
|
from langflow.interface.types import build_langchain_types_dict
|
||||||
|
|
||||||
|
|
@ -36,6 +37,15 @@ async def get_load(predict_request: PredictRequest):
|
||||||
raise HTTPException(status_code=500, detail=str(e)) from e
|
raise HTTPException(status_code=500, detail=str(e)) from e
|
||||||
|
|
||||||
|
|
||||||
|
# Endpoint to upload file
|
||||||
|
@router.post("/upload/{client_id}")
|
||||||
|
async def create_upload_file(file: UploadFile, client_id: str):
|
||||||
|
# Cache file
|
||||||
|
file_path = save_uploaded_file(file.file, file_name=client_id)
|
||||||
|
|
||||||
|
return {"filename": file_path}
|
||||||
|
|
||||||
|
|
||||||
# get endpoint to return version of langflow
|
# get endpoint to return version of langflow
|
||||||
@router.get("/version")
|
@router.get("/version")
|
||||||
def get_version():
|
def get_version():
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,12 @@ def post_validate_node(node_id: str, data: dict):
|
||||||
raise ValueError(f"Node {node_id} not found")
|
raise ValueError(f"Node {node_id} not found")
|
||||||
if not isinstance(node, VectorStoreNode):
|
if not isinstance(node, VectorStoreNode):
|
||||||
node.build()
|
node.build()
|
||||||
return json.dumps({"valid": True, "params": str(node._built_object_repr())})
|
return json.dumps(
|
||||||
|
{
|
||||||
|
"valid": True,
|
||||||
|
"params": f"{str(node._built_object_repr())[:300]}...",
|
||||||
|
}
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(e)
|
logger.exception(e)
|
||||||
return json.dumps({"valid": False, "params": str(e)})
|
return json.dumps({"valid": False, "params": str(e)})
|
||||||
|
|
|
||||||
13
src/backend/langflow/cache/base.py
vendored
13
src/backend/langflow/cache/base.py
vendored
|
|
@ -135,6 +135,19 @@ def save_binary_file(content: str, file_name: str, accepted_types: list[str]) ->
|
||||||
return file_path
|
return file_path
|
||||||
|
|
||||||
|
|
||||||
|
@create_cache_folder
|
||||||
|
def save_uploaded_file(file, file_name):
|
||||||
|
cache_path = Path(tempfile.gettempdir()) / PREFIX
|
||||||
|
file_path = cache_path / file_name
|
||||||
|
|
||||||
|
with open(file_path, "wb") as new_file:
|
||||||
|
# Iterate over the uploaded file in small chunks to conserve memory
|
||||||
|
while chunk := file.read(8192): # Read 8KB at a time (adjust as needed)
|
||||||
|
new_file.write(chunk)
|
||||||
|
|
||||||
|
return file_path
|
||||||
|
|
||||||
|
|
||||||
@create_cache_folder
|
@create_cache_folder
|
||||||
def save_cache(hash_val: str, chat_data, clean_old_cache_files: bool):
|
def save_cache(hash_val: str, chat_data, clean_old_cache_files: bool):
|
||||||
cache_path = Path(tempfile.gettempdir()) / PREFIX / f"{hash_val}.dill"
|
cache_path = Path(tempfile.gettempdir()) / PREFIX / f"{hash_val}.dill"
|
||||||
|
|
|
||||||
|
|
@ -87,12 +87,7 @@ class Node:
|
||||||
# Load the type in value.get('suffixes') using
|
# Load the type in value.get('suffixes') using
|
||||||
# what is inside value.get('content')
|
# what is inside value.get('content')
|
||||||
# value.get('value') is the file name
|
# value.get('value') is the file name
|
||||||
file_name = value.get("value")
|
file_path = value.get("file_path")
|
||||||
content = value.get("content")
|
|
||||||
type_to_load = value.get("suffixes")
|
|
||||||
file_path = cache_utils.save_binary_file(
|
|
||||||
content=content, file_name=file_name, accepted_types=type_to_load
|
|
||||||
)
|
|
||||||
|
|
||||||
params[key] = file_path
|
params[key] = file_path
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ class TemplateFieldCreator(BaseModel, ABC):
|
||||||
suffixes: list[str] = []
|
suffixes: list[str] = []
|
||||||
fileTypes: list[str] = []
|
fileTypes: list[str] = []
|
||||||
file_types: list[str] = []
|
file_types: list[str] = []
|
||||||
content: Union[str, None] = None
|
file_path: Union[str, None] = None
|
||||||
password: bool = False
|
password: bool = False
|
||||||
options: list[str] = []
|
options: list[str] = []
|
||||||
name: str = ""
|
name: str = ""
|
||||||
|
|
@ -35,7 +35,7 @@ class TemplateFieldCreator(BaseModel, ABC):
|
||||||
result["fileTypes"] = result.pop("file_types")
|
result["fileTypes"] = result.pop("file_types")
|
||||||
|
|
||||||
if self.field_type == "file":
|
if self.field_type == "file":
|
||||||
result["content"] = self.content
|
result["file_path"] = self.file_path
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue