MCPcopy
hub / github.com/Agenta-AI/agenta / upload_file

Function upload_file

api/oss/src/routers/testset_router.py:80–157  ·  view source on GitHub ↗

Uploads a CSV or JSON file and saves its data to Postgres. Args: upload_type : Either a json or csv file. file (UploadFile): The CSV or JSON file to upload. testset_name (Optional): the name of the testset if provided. Returns: dict: The result of the uploa

(
    request: Request,
    upload_type: str = Form(None),
    file: UploadFile = File(...),
    testset_name: Optional[str] = File(None),
)

Source from the content-addressed store, hash-verified

78 "/upload/", response_model=TestSetSimpleResponse, operation_id="upload_file"
79)
80async def upload_file(
81 request: Request,
82 upload_type: str = Form(None),
83 file: UploadFile = File(...),
84 testset_name: Optional[str] = File(None),
85):
86 """
87 Uploads a CSV or JSON file and saves its data to Postgres.
88
89 Args:
90 upload_type : Either a json or csv file.
91 file (UploadFile): The CSV or JSON file to upload.
92 testset_name (Optional): the name of the testset if provided.
93
94 Returns:
95 dict: The result of the upload process.
96 """
97
98 if is_ee():
99 has_permission = await check_action_access(
100 user_uid=request.state.user_id,
101 project_id=request.state.project_id,
102 permission=Permission.CREATE_TESTSET,
103 )
104 if not has_permission:
105 error_msg = f"You do not have permission to perform this action. Please contact your organization admin."
106 log.error(error_msg)
107 return JSONResponse(
108 {"detail": error_msg},
109 status_code=403,
110 )
111
112 if file.size > TESTSETS_SIZE_LIMIT: # Preemptively check file size
113 raise TESTSETS_SIZE_EXCEPTION
114
115 # Create a document
116 document = {
117 "name": testset_name if testset_name else file.filename,
118 "csvdata": [],
119 }
120
121 if upload_type == "JSON":
122 # Read and parse the JSON file
123 json_data = await file.read()
124 json_text = json_data.decode("utf-8")
125 json_object = json.loads(json_text)
126
127 # Populate the document with column names and values
128 for i, row in enumerate(json_object):
129 document["csvdata"].append(row)
130
131 else:
132 # Read and parse the CSV file
133 csv_data = await file.read()
134 csv_text = csv_data.decode("utf-8")
135
136 # Use StringIO to create a file-like object from the string
137 csv_file_like_object = io.StringIO(csv_text)

Callers

nothing calls this directly

Calls 6

is_eeFunction · 0.90
_validate_testset_limitsFunction · 0.85
errorMethod · 0.45
readMethod · 0.45
create_testsetMethod · 0.45

Tested by

no test coverage detected