Skip to content
Snippets Groups Projects
Commit b65c2de1 authored by Michael F's avatar Michael F
Browse files

Facts4Chat v1.0.0

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 557 additions and 0 deletions
.venv
artifacts/
frontend/node_modules
\ No newline at end of file
LICENSE 0 → 100644
MIT License
Copyright (c) 2024 Ahmed Asakrah, Robin Ebbinghaus, Michael Frichert, Nico Jeske, Colin Kolbe, Benedikt Kordus, Jonas Röger, Daniel Spenner, project-group-662-tu-dortmund-computer-science
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Mini RAG Annotation Tool
## Description
Minimal tool to annotate the output of a model that uses a context based RAG approach. Each chunk in the context can be mapped to a sentence and the output is then formatted in a way such that it resembles a citation-like format. The project is not intended for a production-use and should only be used on a local server.
## Installation
1. **Create a virtual environment:**
```bash
python3 -m venv env
source env/bin/activate
```
2. **Install the required dependencies:**
```bash
pip install -r requirements.txt
```
3. **Install Node.js dependencies:**
```bash
cd frontend
npm install
```
4. Make sure your Wandb tables are located accordingly. Inspect and adjust ´start_server.py´ if needed. For further information about the schema inspect the `Benchmark Generation` repo or the`
## Usage
1. **Start the server**
```bash
cd server
python start_server.py
```
2. **Start the frontend**
```bash
cd ../frontend
npm start
```
3. **Open Webbrowser**
Navigate to `localhost:3000`
\ No newline at end of file
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Getting Started with Create React App
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Available Scripts
In the project directory, you can run:
### `npm start`
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
The page will reload when you make changes.\
You may also see any lint errors in the console.
### `npm test`
Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `npm run build`
Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `npm run eject`
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### Code Splitting
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
### Analyzing the Bundle Size
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
### Making a Progressive Web App
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
### Advanced Configuration
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
### Deployment
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
### `npm run build` fails to minify
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
This diff is collapsed.
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"bootstrap": "^5.3.3",
"http-proxy-middleware": "^3.0.0",
"react": "^18.3.1",
"react-bootstrap": "^2.10.2",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
frontend/public/favicon.ico

3.78 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
frontend/public/logo192.png

5.22 KiB

frontend/public/logo512.png

9.44 KiB

{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
import './App.css';
import './components/AnnotationBox'
import AnnotationBox from './components/AnnotationBox';
function App() {
return (
<div className="App bg-dark text-light">
<header className="App-header">
<AnnotationBox></AnnotationBox>
</header>
</div>
);
}
export default App;
import { render, screen } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
import {React, useState } from "react";
import 'react-bootstrap/Navbar'
import Navbar from 'react-bootstrap/Navbar'
import Container from 'react-bootstrap/Container';
import ButtonGroup from 'react-bootstrap/ButtonGroup';
import ButtonToolbar from 'react-bootstrap/ButtonToolbar'
import Button from 'react-bootstrap/Button'
import InputGroup from 'react-bootstrap/InputGroup'
import Form from 'react-bootstrap/Form'
import ChunkList from "./ChunkList";
import AnnotationText from "./AnnotationText";
function AnnotationBox() {
const [chunks, setChunks] = useState([]);
const [generatedSentences, setGeneratedSentences] = useState([]);
const [question, setQuestion] = useState("")
// Holds the index of the currently selected chunk
const [activeChunkIdx, setActiveChunkIdx] = useState(0);
/*
* These state variables are used to obtain an n:m mapping between sentences and chunks. They are updated
*/
const [chunkIdxToSentenceIdxMap, setChunkIdxToSentenceIdxMap] = useState({});
const [sentenceIdxToChunkIdxMap, setSentenceIdxToChunkIdxMap] = useState({});
/**
* Formats a list of sentences (without a dot) and inserts <RAG1>, <RAG2>, ... tokens whenever a sentences has been covered by a chunk and appends a dot
* @param generatedSentences - List of strings corresponding to the sentences
* @return {String} All sentences combined with RAG-Tokens and dots.
*/
const formatData = (generatedSentences) => {
let output = "";
for (let i = 0; i < generatedSentences.length; i++) {
let toAppend;
if (sentenceIdxToChunkIdxMap[i] && sentenceIdxToChunkIdxMap[i].length !== 0) {
toAppend = generatedSentences[i] + sentenceIdxToChunkIdxMap[i].reduce((acc, x) => acc + "<RAG" + x + ">", "");
}
else {
toAppend = generatedSentences[i];
}
output += toAppend + ". ";
}
return output;
}
const sendAnnotation = async () => {
const toSend = {
"annotation_text": formatData(generatedSentences, chunkIdxToSentenceIdxMap),
"question": question,
"username": document.getElementById("username").value,
"chunks": chunks
}
try {
const response = await fetch("/api/question", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(toSend),
});
if (response.ok) {
const responseData = await response.text(); // Assuming the response is a text message
console.log("Success:", responseData);
reset()
fetchNextQuestion()
} else {
console.error("Failed to submit annotation:", response.statusText);
alert("Failed to submit annotation")
}
} catch (error) {
console.error("Error:", error);
}
console.log(toSend)
}
/**
* Fetches the next question from the server and sets the state variables accordingly. Before fetching the state variables are reset. The model's answer is split by a `.` before setting the sentence variables.
*/
const fetchNextQuestion = async () => {
reset();
try {
const response = await fetch('/api/question');
console.log(response)
const data = await response.json();
setChunks(data["chunks"])
setGeneratedSentences(data["model_response"].split(". "))
setQuestion(data["question"])
} catch (error) {
console.error('Error fetching next question:', error);
}
};
/**
* This function resets all state variables after the next question
*/
const reset = () => {
setActiveChunkIdx(0);
setChunks([]);
setGeneratedSentences([]);
setQuestion("");
setChunkIdxToSentenceIdxMap({});
setSentenceIdxToChunkIdxMap({});
}
return (
<div>
<Container fluid="xxl" style={{ fontSize: '18px' }} data-bs-theme="dark">
<AnnotationText
question={question}
generatedSentences={generatedSentences}
activeChunkIdx={activeChunkIdx}
chunkIdxToSentenceIdxMap={chunkIdxToSentenceIdxMap}
setChunkIdxToSentenceIdxMap={setChunkIdxToSentenceIdxMap}
sentenceIdxToChunkIdxMap={sentenceIdxToChunkIdxMap}
setSentenceIdxToChunkIdxMap={setSentenceIdxToChunkIdxMap} />
<ChunkList chunks={chunks} activeChunkIdx={activeChunkIdx} setActiveChunkIdx={setActiveChunkIdx} />
</Container>
<Navbar sticky="bottom">
<ButtonToolbar
className="justify-content-between"
aria-label="Toolbar with Button groups"
>
<ButtonGroup aria-label="Basic example">
<Button variant="secondary" onClick={fetchNextQuestion}>Next Question</Button>
<Button variant="secondary" onClick={sendAnnotation}>Commit</Button>
</ButtonGroup>
<InputGroup>
<InputGroup.Text id="btnGroupAddon2">@</InputGroup.Text>
<Form.Control
type="text"
placeholder="Annotator's name"
id="username"
/>
</InputGroup>
</ButtonToolbar>
</Navbar>
</div>);
}
export default AnnotationBox;
\ No newline at end of file
import React from 'react';
function AnnotationText({
question,
generatedSentences,
activeChunkIdx,
chunkIdxToSentenceIdxMap,
setChunkIdxToSentenceIdxMap,
sentenceIdxToChunkIdxMap,
setSentenceIdxToChunkIdxMap
}) {
const handleClick = (sentenceIdx) => {
const newChunkIdxToSentenceIdxMap = { ...chunkIdxToSentenceIdxMap };
const newSentenceIdxToChunkIdxMap = { ...sentenceIdxToChunkIdxMap };
if (!newChunkIdxToSentenceIdxMap[activeChunkIdx]) {
newChunkIdxToSentenceIdxMap[activeChunkIdx] = [];
}
if (!newSentenceIdxToChunkIdxMap[sentenceIdx]) {
console.log("fresh instance for sentence created")
newSentenceIdxToChunkIdxMap[sentenceIdx] = [];
}
if (newChunkIdxToSentenceIdxMap[activeChunkIdx].includes(sentenceIdx)) {
newChunkIdxToSentenceIdxMap[activeChunkIdx] = newChunkIdxToSentenceIdxMap[activeChunkIdx].filter(item => item !== sentenceIdx);
} else {
newChunkIdxToSentenceIdxMap[activeChunkIdx].push(sentenceIdx);
}
if (newSentenceIdxToChunkIdxMap[sentenceIdx].includes(activeChunkIdx)) {
console.log("chunk already included in sentence")
newSentenceIdxToChunkIdxMap[sentenceIdx] = newSentenceIdxToChunkIdxMap[sentenceIdx].filter(item => item !== activeChunkIdx);
} else {
console.log("chunk was not included in sentence")
newSentenceIdxToChunkIdxMap[sentenceIdx].push(activeChunkIdx);
}
setChunkIdxToSentenceIdxMap(newChunkIdxToSentenceIdxMap);
setSentenceIdxToChunkIdxMap(newSentenceIdxToChunkIdxMap);
};
return (
<div style = {{
border: '1px solid',
borderRadius: '0.25rem',
padding: "10px",
marginBottom: "10px"
}}>
<h4>{question}</h4>
{generatedSentences.map((sentence, idx) => (
<a
key={idx}
onClick={() => handleClick(idx)}
style={{
cursor: 'pointer',
backgroundColor: chunkIdxToSentenceIdxMap[activeChunkIdx]?.includes(idx) ? '#00ffff' : 'transparent',
color: chunkIdxToSentenceIdxMap[activeChunkIdx]?.includes(idx) ? 'black' : '',
}}
>
{sentence + ". "}
</a>
))}
</div>
);
}
export default AnnotationText;
import React from 'react';
import ListGroup from 'react-bootstrap/ListGroup';
function ChunkList({ chunks, activeChunkIdx, setActiveChunkIdx }) {
const handleClick = (idx) => {
setActiveChunkIdx(idx);
};
return (
<div style={{
maxHeight: '800px', /* Adjust height as needed */
overflowY: 'auto', /* Enable vertical scrolling */
border: '1px solid #dee2e6', /* Optional: add a border for separation */
borderRadius: '0.25rem', /* Optional: rounded corners */
padding: '0.5rem' /* Optional: padding for better spacing */
}}>
<ListGroup>
{chunks.map((val, idx) => (
<ListGroup.Item
key={idx}
active={idx === activeChunkIdx}
onClick={() => handleClick(idx)}
>
{val["text"]}
</ListGroup.Item>
))}
</ListGroup>
</div>
);
}
export default ChunkList;
\ No newline at end of file
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment