Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fallback to request if socket connection fails for object detection and question answering metrics for backcompat #2409

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions apps/widget/src/app/ModelAssessment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import {
import { ModelAssessmentDashboard } from "@responsible-ai/model-assessment";
import React from "react";

import { callFlaskService, connectToFlaskService } from "./callFlaskService";
import {
callFlaskService,
connectToFlaskServiceWithBackupCall
} from "./callFlaskService";
import { CallbackType, IModelAssessmentProps } from "./ModelAssessmentUtils";

export class ModelAssessment extends React.Component<IModelAssessmentProps> {
Expand All @@ -33,16 +36,18 @@ export class ModelAssessment extends React.Component<IModelAssessmentProps> {
objectDetectionCache: Map<string, [number, number, number]>,
abortSignal: AbortSignal
): Promise<any[]> => {
return connectToFlaskService(
const parameters = [
selectionIndexes,
aggregateMethod,
className,
iouThreshold,
objectDetectionCache
];
return connectToFlaskServiceWithBackupCall(
this.props.config,
[
selectionIndexes,
aggregateMethod,
className,
iouThreshold,
objectDetectionCache
],
parameters,
"handle_object_detection_json",
"/get_object_detection_metrics",
abortSignal
);
};
Expand All @@ -57,10 +62,12 @@ export class ModelAssessment extends React.Component<IModelAssessmentProps> {
>,
abortSignal: AbortSignal
): Promise<any[]> => {
return connectToFlaskService(
const parameters = [selectionIndexes, questionAnsweringCache];
return connectToFlaskServiceWithBackupCall(
this.props.config,
[selectionIndexes, questionAnsweringCache],
parameters,
"handle_question_answering_json",
"/get_question_answering_metrics",
abortSignal
);
};
Expand Down
34 changes: 34 additions & 0 deletions apps/widget/src/app/callFlaskService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,48 @@ export async function connectToFlaskService<TRequest, TResponse>(
socket.on("disconnect", () => {
console.log("socket disconnected");
});
socket.on("error", (error: Error) => {
console.log(`Encountered socket error, aborting connection: ${error}`);
reject(error);
});
socket.emit(
urlPath,
{
data: JSON.stringify(data)
},
(response: IDataResponse<TResponse>) => {
console.log(response);
if (response === undefined) {
console.log("Encountered socket error, aborting connection");
return reject(new Error("No response from socket"));
}
return resolve(response.data);
}
);
});
}

export async function connectToFlaskServiceWithBackupCall<TRequest, TResponse>(
config: Pick<IAppConfig, "baseUrl" | "withCredentials">,
data: TRequest,
urlPathConnect: string,
urlPathCall: string,
abortSignal?: AbortSignal
): Promise<TResponse> {
return connectToFlaskService<TRequest, TResponse>(
config,
data,
urlPathConnect,
abortSignal
).catch((error) => {
console.log(
`Error connecting to socket, falling back to http request: ${error}`
);
return callFlaskService<TRequest, TResponse>(
config,
data,
urlPathCall,
abortSignal
);
});
}
Loading