From c0fbf8d930baf0f5cf9aad115424acf17fb00519 Mon Sep 17 00:00:00 2001 From: nik Date: Tue, 20 Feb 2024 14:00:51 +0000 Subject: [PATCH] Fix client render --- adala/environments/kafka.py | 2 +- adala/server/app.py | 13 +- adala/server/ui/src/App.tsx | 143 +++++++++++------- .../ui/src/_api/services/DefaultService.ts | 4 +- adala/server/ui/src/adala.tsx | 8 +- 5 files changed, 107 insertions(+), 63 deletions(-) diff --git a/adala/environments/kafka.py b/adala/environments/kafka.py index f71df938..0b8dfdfc 100644 --- a/adala/environments/kafka.py +++ b/adala/environments/kafka.py @@ -102,7 +102,7 @@ class FileStreamAsyncKafkaEnvironment(AsyncKafkaEnvironment): input_file: str output_file: str error_file: str - pass_through_columns: List[str] + pass_through_columns: Optional[List[str]] = None def _iter_csv_local(self, csv_file_path): """ diff --git a/adala/server/app.py b/adala/server/app.py index f49ed39f..13005542 100644 --- a/adala/server/app.py +++ b/adala/server/app.py @@ -118,10 +118,10 @@ class JobStatusResponse(BaseModel): Example: [10, 100] means 10% of the completeness. """ status: str - processed_total: List[int] = Annotated[List[int], AfterValidator(lambda x: len(x) == 2)] + # processed_total: List[int] = Annotated[List[int], AfterValidator(lambda x: len(x) == 2)] -@app.get('/get-status') +@app.post('/get-status') def get_status(request: JobStatusRequest): """ Get the status of a job. @@ -133,7 +133,14 @@ def get_status(request: JobStatusRequest): JobStatusResponse: The response model for getting the status of a job. """ job = process_file.AsyncResult(request.job_id) - return Response[JobStatusResponse](data=JobStatusResponse(status=job.status)) + try: + status = job.status + except Exception as e: + logger.error(f"Error getting job status: {e}") + status = 'FAILURE' + else: + logger.info(f"Job {request.job_id} status: {status}") + return Response[JobStatusResponse](data=JobStatusResponse(status=status)) class JobCancelRequest(BaseModel): diff --git a/adala/server/ui/src/App.tsx b/adala/server/ui/src/App.tsx index 69a1cb48..d08dee69 100644 --- a/adala/server/ui/src/App.tsx +++ b/adala/server/ui/src/App.tsx @@ -1,66 +1,101 @@ -// @ts-nocheck - -import React, {useState, useEffect} from "react"; +import React, { useState } from "react"; import "./App.css"; import { Adala } from "./adala"; +import testData from "./testData"; + +type FormData = { + inputFile: string; + outputFile: string; + errorFile: string; + instructions: string; + labels: string[]; // Explicitly typing labels as an array of strings + model: string; + apiKey: string; + passThroughColumns: string[]; +}; const App = () => { - const [input, setInput] = useState(""); - const [status, setStatus] = useState(""); - const [jobId, setJobId] = useState(""); // This should be set to the jobId returned from the submit call - const adala = new Adala("http://localhost:30001"); + const [formData, setFormData] = useState(testData); + const [status, setStatus] = useState(""); + const [jobId, setJobId] = useState(""); + const adala = new Adala("http://localhost:30001"); - const handleSubmit = async () => { - try { - const response = await adala.submit({ - inputFile: "input.txt", - outputFile: "output.txt", - errorFile: "error.txt", - instructions: input, - labels: ["positive", "negative"], - model: "gpt-3.5-turbo", - apiKey: "your-api-key" - }); - setJobId(response.data.jobId); - checkStatusPeriodically(); - } catch - (error) { - console.error("Error submitting data to server:", error); - } - }; + const handleSubmit = async () => { + try { + const response = await adala.submit({ + ...formData, + labels: formData.labels, + }); + setJobId(response.job_id); + checkStatusPeriodically(response.job_id); + } catch (error) { + console.error("Error submitting data to server:", error); + } + }; - const checkStatusPeriodically = () => { - const intervalId = setInterval(async () => { - try { - const response = await adala.getStatus({jobId}); - setStatus(response.data.status); - if (response.data.status === "Complete" || response.data.status === "Error") { - clearInterval(intervalId); - } - } catch (error) { - console.error("Error fetching status from server:", error); + const checkStatusPeriodically = (currentJobId: string) => { + const intervalId = setInterval(async () => { + try { + const response = await adala.getStatus({ jobId: currentJobId }); + setStatus(response.status); // Adjust this according to your actual response structure + if (response.status === "SUCCESS" || response.status === "FAILURE") { clearInterval(intervalId); } - }, 5000); // Poll every 5 seconds - }; + } catch (error) { + console.error("Error fetching status from server:", error); + clearInterval(intervalId); + } + }, 5000); // Poll every 5 seconds + }; + + const handleChange = (event: React.ChangeEvent) => { + const { name, value } = event.target; + setFormData(prevState => ({ + ...prevState, + // for list based fields (labels, passThroughColumns), split the value by comma and trim each item + [name]: name === "labels" || name === "passThroughColumns" ? value.split(",").map(item => item.trim()) : value, + })); + }; - return ( -
-
- setInput(e.target.value)} - placeholder="Enter your input" - /> - -
-
-