Skip to content

Commit

Permalink
changes requested
Browse files Browse the repository at this point in the history
  • Loading branch information
rosannamilner committed Aug 6, 2024
1 parent 2be7cd6 commit dcbfedc
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 209 deletions.
7 changes: 0 additions & 7 deletions src/components/NavItems/Assistant/Assistant.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ const Assistant = () => {

//third party check states
const neResult = useSelector((state) => state.assistant.neResultCategory);
const newsFramingResult = useSelector(
(state) => state.assistant.newsFramingResult,
);
const newsGenreResult = useSelector(
(state) => state.assistant.newsGenreResult,
);
const hpResult = useSelector((state) => state.assistant.hpResult);

// source credibility
const positiveSourceCred = useSelector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,101 +166,6 @@ export default function assistantApiCalls() {
);
};

const MAX_NUM_RETRIES = 3;

/**
* Calls an async function that throws an exception when it fails, will retry for numMaxRetries
* @param numMaxRetries Number of times the function will be retried
* @param asyncFunc The async function to call
* @param errorFunc Called when asyncFunc throws an error when there are additional retries
* @returns {Promise<*>} Output of asyncFunc
*/
async function callAsyncWithNumRetries(
numMaxRetries,
asyncFunc,
errorFunc = null,
) {
for (let retryCount = 0; retryCount < numMaxRetries; retryCount++) {
try {
return await asyncFunc();
} catch (e) {
if (retryCount + 1 >= MAX_NUM_RETRIES) {
throw e;
} else {
if (errorFunc) errorFunc(retryCount, e);
}
}
}
}

const callNewsFramingService = async (text) => {
return await callAsyncWithNumRetries(
MAX_NUM_RETRIES,
async () => {
const result = await axios.post(
assistantEndpoint + "gcloud/news-framing-clfr",
{ text: text },
);
return result.data;
},
(numTries) => {
console.log(
"Could not connect to news framing service, tries " +
(numTries + 1) +
"/" +
MAX_NUM_RETRIES,
);
},
);
};

const callNewsGenreService = async (text) => {
return await callAsyncWithNumRetries(
MAX_NUM_RETRIES,
async () => {
const result = await axios.post(
assistantEndpoint + "gcloud/news-genre-clfr",
{ text: text },
);
return result.data;
},
(numTries) => {
console.log(
"Could not connect to news genre service, tries " +
(numTries + 1) +
"/" +
MAX_NUM_RETRIES,
);
},
);
};

const callPersuasionService = async (text) => {
return await callAsyncWithNumRetries(
MAX_NUM_RETRIES,
async () => {
const result = await axios.post(
assistantEndpoint + "gcloud/persuasion-span-clfr",
{ text: text },
);
return result.data;
},
(numTries) => {
console.log(
"Could not connect to persuasion service, tries " +
(numTries + 1) +
"/" +
MAX_NUM_RETRIES,
);
},
);
};

const callOcrScriptService = async () => {
const result = await axios.get(assistantEndpoint + "gcloud/ocr-scripts");
return result.data;
};

const callSubjectivityService = async (text) => {
return await callAsyncWithNumRetries(
MAX_NUM_RETRIES,
Expand Down Expand Up @@ -332,7 +237,6 @@ export default function assistantApiCalls() {
callSourceCredibilityService,
callNamedEntityService,
callOcrService,
callOcrScriptService,
callNewsFramingService,
callNewsGenreService,
callPersuasionService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,22 @@ const ExtractedSourceCredibilityDBKFDialog = ({
? sourceCredibilityResults.map((value, key) => (
<Accordion key={key}>
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
{sourceCredibilityResults[key] ? (
sourceCredibilityResults[
key
].credibilityScope.includes("/") ? (
<Typography color={trafficLightColor}>
{` ${keyword("this")}`}
{getUrlTypeFromCredScope(
value.credibilityScope,
)}
{` ${keyword(
"source_credibility_warning_account",
)} ${" "}${value.credibilitySource}`}
</Typography>
) : sourceCredibilityResults[key]
.credibilityScope ? (
<Typography color={trafficLightColor}>
{` ${keyword(
"source_cred_popup_header_domain",
)} ${
sourceCredibilityResults[key]
.credibilitySource
} `}
</Typography>
) : null
{value.credibilityScope.includes("/") ? (
<Typography color={trafficLightColor}>
{` ${keyword("this")}`}
{getUrlTypeFromCredScope(
value.credibilityScope,
)}
{` ${keyword(
"source_credibility_warning_account",
)} ${" "}${value.credibilitySource}`}
</Typography>
) : value.credibilityScope ? (
<Typography color={trafficLightColor}>
{` ${keyword(
"source_cred_popup_header_domain",
)} ${value.credibilitySource} `}
</Typography>
) : null}
</AccordionSummary>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ const SourceCredibilityResult = (props) => {
{value.credibilityScope.includes("/") ? (
<Typography>
{` ${keyword("this")}`}
{/* {inputUrlType
? capitaliseFirstLetter(inputUrlType)
: null} */}
{getUrlTypeFromCredScope(value.credibilityScope)}
{` ${keyword(
"source_credibility_warning_account",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import React, { useState } from "react";
import { useDispatch } from "react-redux";

import Card from "@mui/material/Card";
import {
CardHeader,
Expand All @@ -22,10 +20,10 @@ import {
interpRgb,
rgbToString,
rgbToLuminance,
rgbListToGradient,
treeMapToElements,
wrapPlainTextSpan,
} from "./assistantUtils";
import ColourGradientTooltipContent from "./ColourGradientTooltipContent";

import "./assistantTextResultStyle.css";

Expand Down Expand Up @@ -290,50 +288,3 @@ export function ClassifiedText({

return <Typography align={"left"}>{output}</Typography>;
}

export function ColourGradientScale({ textLow, textHigh, rgbList }) {
return (
<>
<Grid container>
<Grid item xs={6}>
<Typography align="left" fontSize="small" fontWeight="bold">
{textLow}
</Typography>
</Grid>
<Grid item xs={6}>
<Typography align="right" fontSize="small" fontWeight="bold">
{textHigh}
</Typography>
</Grid>
</Grid>
<div
style={{
width: "100%",
height: "1em",
background: rgbListToGradient(rgbList),
}}
/>
</>
);
}

export function ColourGradientTooltipContent({
description = "",
colourScaleText,
textLow = "Low",
textHigh = "High",
rgbLow = [0, 0, 0],
rgbHigh = [255, 255, 255],
}) {
return (
<div className={"content"}>
{description}
<p>{colourScaleText}</p>
<ColourGradientScale
textLow={textLow}
textHigh={textHigh}
rgbList={[rgbLow, rgbHigh]}
/>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import React, { useState } from "react";
import { useDispatch } from "react-redux";

import { Button } from "@mui/material";

import Card from "@mui/material/Card";
import { CardHeader, Chip, List, ListItem, ListItemText } from "@mui/material";
Expand All @@ -23,7 +20,7 @@ import {
mergeSpanIndices,
wrapPlainTextSpan,
} from "./assistantUtils";
import { ColourGradientTooltipContent } from "./AssistantTextClassification";
import ColourGradientTooltipContent from "./ColourGradientTooltipContent";
import { styled } from "@mui/system";

// Had to create a custom styled span as the default style attribute does not support
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from "react";
import Grid from "@mui/material/Grid";
import Typography from "@mui/material/Typography";

import { rgbListToGradient } from "./assistantUtils";

export function ColourGradientScale({ textLow, textHigh, rgbList }) {
return (
<>
<Grid container>
<Grid item xs={6}>
<Typography align="left" fontSize="small" fontWeight="bold">
{textLow}
</Typography>
</Grid>
<Grid item xs={6}>
<Typography align="right" fontSize="small" fontWeight="bold">
{textHigh}
</Typography>
</Grid>
</Grid>
<div
style={{
width: "100%",
height: "1em",
background: rgbListToGradient(rgbList),
}}
/>
</>
);
}

export default function ColourGradientTooltipContent({
description = "",
colourScaleText,
textLow = "Low",
textHigh = "High",
rgbLow = [0, 0, 0],
rgbHigh = [255, 255, 255],
}) {
return (
<div className={"content"}>
{description}
<p>{colourScaleText}</p>
<ColourGradientScale
textLow={textLow}
textHigh={textHigh}
rgbList={[rgbLow, rgbHigh]}
/>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React from "react";
import _ from "lodash";
import { ColourGradientTooltipContent } from "./AssistantTextClassification";
import Tooltip from "@mui/material/Tooltip";

/**
* Interpolate RGB between an arbitrary range
Expand Down Expand Up @@ -85,35 +83,17 @@ function treeMapToElementsRecursive(
if ("span" in treeElem) {
const span = treeElem.span;
if (spanHighlightIndices === null) {
console.log("No span highlight: ", text.substring(span.start, span.end));
childElems.push(text.substring(span.start, span.end));
} else {
console.log("Span highlight: ", text.substring(span.start, span.end));
let currentIndex = span.start;
for (let i = 0; i < spanHighlightIndices.length; i++) {
const hSpan = spanHighlightIndices[i];
console.log(
"Matching span",
span.start,
span.end,
hSpan.indices[0],
hSpan.indices[1],
);
const hSpanStart = hSpan.indices[0];
const hSpanEnd = hSpan.indices[1];
if (
(span.start <= hSpanStart && hSpanStart <= span.end) ||
(span.start <= hSpanEnd && hSpanEnd <= span.end)
) {
//If there's an overlap
console.log(
"Found lapping span ",
span.start,
span.end,
hSpanStart,
hSpanEnd,
);

// If span doesn't start before the current index
if (hSpanStart > currentIndex) {
childElems.push(text.substring(currentIndex, hSpanStart));
Expand All @@ -123,7 +103,6 @@ function treeMapToElementsRecursive(
hSpanStart < span.start ? span.start : hSpanStart;
const boundedEnd = hSpanEnd > span.end ? span.end : hSpanEnd;
if (wrapFunc) {
console.log("Wrapping: ", text.substring(boundedStart, boundedEnd));
childElems.push(
wrapFunc(
text.substring(boundedStart, boundedEnd),
Expand All @@ -133,10 +112,6 @@ function treeMapToElementsRecursive(
),
);
} else {
// console.log(
// "Not wrapping: ",
// text.substring(boundedStart, boundedEnd),
// );
childElems.push(text.substring(boundedStart, boundedEnd));
}

Expand Down

0 comments on commit dcbfedc

Please sign in to comment.