Skip to content

Commit

Permalink
Fixed Generating Answer being displayed on load
Browse files Browse the repository at this point in the history
  • Loading branch information
KronemeyerJoshua committed May 28, 2024
1 parent 4834881 commit 697d49e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
17 changes: 6 additions & 11 deletions app/frontend/src/pages/tda/Tda.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const Tda = ({folderPath, tags}: Props) => {
const [otherq, setOtherq] = useState('');
const [selectedQuery, setSelectedQuery] = useState('');
const [dataFrame, setDataFrame] = useState<object[]>([]);
const [loading, setLoading] = useState(false);
const [renderAnswer, setRenderAnswer] = useState(false);
const [inputValue, setInputValue] = useState("");
const [fileu, setFile] = useState<File | null>(null);
const [images, setImages] = useState<string[]>([]);
Expand Down Expand Up @@ -76,7 +76,7 @@ const fetchImages = async () => {
const handleAnalysis = () => {
setImages([])
setOutput('');
setLoading(true);
setRenderAnswer(true);
setTimeout(async () => {
try {
const query = setOtherQ(selectedQuery);
Expand All @@ -90,11 +90,10 @@ const fetchImages = async () => {
setStreamKey(prevKey => prevKey + 1);
} else {
setOutput("no file file has been uploaded.")
setLoading(false);
}
} catch (error) {
console.log(error);
setLoading(false);
setRenderAnswer(false);
}
}, 0);
};
Expand All @@ -111,26 +110,23 @@ const fetchImages = async () => {
setImages([])
const query = setOtherQ(selectedQuery);
setOutput('');
setLoading(true);
setRenderAnswer(true);
if (fileu) {
const result = await processCsvAgentResponse(query, fileu);
setLoading(false);
setOutput(result.toString());
fetchImages();
return;

}
else {
setOutput("no file file has been uploaded.")
setLoading(false);
}
} catch (error) {
lastError = error;
}
}
// If the code reaches here, all retries have failed. Handle the error as needed.
console.error(lastError);
setLoading(false);
setOutput('An error occurred.');
};

Expand Down Expand Up @@ -246,7 +242,6 @@ const fetchImages = async () => {
}
else {
setOutput("no file file has been uploaded.")
setLoading(false);
}
}
}, [selectedQuery]);
Expand All @@ -265,7 +260,6 @@ const handleCloseEvent = () => {
eventSourceRef.current.close();
eventSourceRef.current = null;
fetchImages();
setLoading(false);
console.log('EventSource closed');
}
}
Expand Down Expand Up @@ -399,7 +393,8 @@ const handleCloseEvent = () => {
<div style={{width: '100%'}}>
<h2>Tabular Data Assistant Response:</h2>
<div>
<CharacterStreamer key={streamKey} eventSource={eventSourceRef.current} classNames={cstyle.centeredAnswerContainer} nonEventString={output} onStreamingComplete={handleCloseEvent} typingSpeed={10} />
{ renderAnswer &&
<CharacterStreamer key={streamKey} eventSource={eventSourceRef.current} classNames={cstyle.centeredAnswerContainer} nonEventString={output} onStreamingComplete={handleCloseEvent} typingSpeed={10} /> }
</div>
<h2>Generated Images:</h2>
<div>
Expand Down
20 changes: 7 additions & 13 deletions app/frontend/src/pages/tutor/Tutor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import React, { useRef } from 'react';
//import { Button } from '@fluentui/react';
import { Accordion, Card, Button } from 'react-bootstrap';
import {getHint, processAgentResponse, getSolve, streamData} from "../../api";
import {getHint, processAgentResponse, streamData} from "../../api";
import { useEffect, useState } from "react";
import styles from './Tutor.module.css';
import ReactMarkdown from 'react-markdown';
Expand All @@ -15,12 +15,11 @@ import CharacterStreamer from '../../components/CharacterStreamer/CharacterStrea

const Tutor = () => {
const [streamKey, setStreamKey] = useState(0);
const [loading, setLoading] = useState(false);
const [renderAnswer, setRenderAnswer] = useState(false);
const [error, setError] = useState(false);
const [errorMessage, setErrorMessage] = useState('');
const [mathProblem, setMathProblem] = useState('');
const [output, setOutput] = useState('');
//const [output, setOutput] = useState<string | null>("");
const [selectedButton, setSelectedButton] = useState<string | null>(null);
const eventSourceRef = useRef<EventSource | null>(null);

Expand All @@ -32,14 +31,12 @@ const Tutor = () => {

const handleInput = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setLoading(true);
userInput(mathProblem);
};

const userInput = (problem: string) => {
// Process the user's math problem here
console.log(problem);
setLoading(false);
};

function delay(ms: number): Promise<void> {
Expand All @@ -53,7 +50,6 @@ const Tutor = () => {
): Promise<T> {

setError(false);
setLoading(true);
for (let attempt = 1; attempt <= retries; attempt++) {
try {
return await asyncFn(); // Try executing the function
Expand All @@ -67,33 +63,33 @@ const Tutor = () => {
}
}
setError(true);
setLoading(false);
// If we reach this point, all retries have failed
throw new Error(`Max retries reached. Last error: ${errorMessage}`);
}

async function hinter(question: string) {
setStreamKey(prevKey => prevKey + 1);
setOutput('');
setRenderAnswer(true);
await retryAsyncFn(() => getHint(question), 3, 1000).then((response) => {
setOutput(response.toString());
});
setLoading(false);

}


async function getAnswer(question: string) {
setStreamKey(prevKey => prevKey + 1);
setOutput('');
setRenderAnswer(true);
await retryAsyncFn(() => processAgentResponse(question), 3, 1000).then((response) => {
setOutput(response.toString());
});
setLoading(false);
};

async function handleExampleClick(value: string) {
setStreamKey(prevKey => prevKey + 1);
setRenderAnswer(true);
setMathProblem(value);
getAnswer(value);
}
Expand All @@ -109,7 +105,7 @@ const EXAMPLES: ExampleModel[] = [
const handleButton2Click = () => {
setStreamKey(prevKey => prevKey + 1);
setOutput('');
setLoading(true);
setRenderAnswer(true);
setSelectedButton('button2');
if (eventSourceRef.current) {
eventSourceRef.current.close();
Expand All @@ -124,7 +120,6 @@ const EXAMPLES: ExampleModel[] = [
if (eventSourceRef.current) {
eventSourceRef.current.close();
eventSourceRef.current = null;
setLoading(false);
console.log('EventSource closed');
}
}
Expand All @@ -134,7 +129,6 @@ const EXAMPLES: ExampleModel[] = [
if (eventSourceRef.current) {
eventSourceRef.current.close();
eventSourceRef.current = null;
setLoading(false);
console.log('EventSource closed');
}
};
Expand Down Expand Up @@ -199,7 +193,7 @@ return (
</div>
</form>
{error && <div className="spinner">{errorMessage}</div>}
{<CharacterStreamer key={streamKey} eventSource={eventSourceRef.current} onStreamingComplete={handleCloseEvent} classNames={styles.centeredAnswerContainer} nonEventString={output} /> }
{renderAnswer && <CharacterStreamer key={streamKey} eventSource={eventSourceRef.current} onStreamingComplete={handleCloseEvent} classNames={styles.centeredAnswerContainer} nonEventString={output} /> }
</div>
</div>
)
Expand Down

0 comments on commit 697d49e

Please sign in to comment.