Skip to content

Commit

Permalink
fallback to request if socket connection fails for object detection a…
Browse files Browse the repository at this point in the history
…nd question answering metrics for backcompat
  • Loading branch information
imatiach-msft committed Nov 9, 2023
1 parent 002b1a0 commit 3059504
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
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: ${error}`
);
return callFlaskService<TRequest, TResponse>(
config,
data,
urlPathCall,
abortSignal
);
});
}

0 comments on commit 3059504

Please sign in to comment.