Skip to content

Commit

Permalink
Merge pull request #162 from mikepsinn/develop
Browse files Browse the repository at this point in the history
Text2Measurements library, app, tests, etc.
  • Loading branch information
mikepsinn authored Mar 26, 2024
2 parents 8ab234f + d027063 commit ab96249
Show file tree
Hide file tree
Showing 23 changed files with 3,304 additions and 246 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/text-2-measurements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Text2Measurements tests

on:
push:
paths:
- 'libs/text-2-measurements/**'
pull_request:
paths:
- 'libs/text-2-measurements/**'

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install Dependencies
run: |
cd libs/text-2-measurements
npm ci
- name: Run Tests
run: |
cd libs/text-2-measurements
npm test
3 changes: 0 additions & 3 deletions libs/text-2-measurements/.babelrc

This file was deleted.

3 changes: 2 additions & 1 deletion libs/text-2-measurements/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ OPENAI_API_KEY=
AZURE_OPENAI_ENDPOINT= # The full URL of the Azure OpenAI REST API (e.g. https://YOUR_RESOURCE_NAME.openai.azure.com/openai/deployments/YOUR_DEPLOYMENT_NAME/chat/completions?api-version=2023-05-15)
AZURE_OPENAI_API_KEY= # The Azure OpenAI API key


# Just for the example express server
SESSION_SECRET=your-secret-key
18 changes: 0 additions & 18 deletions libs/text-2-measurements/.eslintrc.json

This file was deleted.

10 changes: 10 additions & 0 deletions libs/text-2-measurements/example_usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { processStatement } = require('./src/lib/statement-2-measurements');

async function parseStatement() {
const statement = "I have been feeling very tired and fatigued today. I have been having trouble concentrating and I have been feeling very down. I took a cold shower for 5 minutes and I took a 20 minute nap. I also took magnesium 200mg, Omega3 one capsule 500mg";
const localDateTime = "2021-01-01T20:00:00"; // Example date and time
const result = await processStatement(statement, localDateTime);
console.log(result);
}

parseStatement();
11 changes: 0 additions & 11 deletions libs/text-2-measurements/jest.config.ts

This file was deleted.

22 changes: 18 additions & 4 deletions libs/text-2-measurements/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@curedao/text-2-measurements",
"name": "@fdai/text-2-measurements",
"version": "0.0.1",
"description": "This library uses artificial intelligence to convert natural language inputs into structured data about diet, treatment, and symptom tracking.",
"main": "index.js",
Expand All @@ -10,7 +10,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/curedao/decentralized-fda.git"
"url": "git+https://github.com/FDA-AI/FDAi.git"
},
"keywords": [
"natural language processing",
Expand All @@ -23,7 +23,21 @@
"author": "",
"license": "GPL-3.0-or-later",
"bugs": {
"url": "https://github.com/curedao/decentralized-fda/issues"
"url": "https://github.com/FDA-AI/FDAi/issues"
},
"homepage": "https://curedao.org"
"homepage": "https://fdai.earth",
"dependencies": {
"axios": "*",
"dotenv": "*",
"typechat": "^0.0.10"
},
"devDependencies": {
"@types/jest": "*",
"@types/node": "*",
"jest": "*",
"ts-jest": "*",
"typescript": "*",
"express-session": "*",
"express": "*"
}
}
41 changes: 0 additions & 41 deletions libs/text-2-measurements/project.json

This file was deleted.

34 changes: 34 additions & 0 deletions libs/text-2-measurements/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Page</title>
<link rel="stylesheet" href="styles.css">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/themes/prism-tomorrow.min.css" rel="stylesheet" />
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>

<div>
<a href="/settings">Settings</a>
<form id="myForm" action="/parse" method="POST">
<label for="statement">What were your symptoms, foods, and treatments today?</label><br>
<textarea id="statement" name="statement" rows="4" cols="50"></textarea><br>
<input type="submit" value="Submit">
</form>
<div id="loader" style="display: none;">Loading...</div>
<div class="card mt-3">
<div class="card-body">
<pre id="response" class="language-json"><code></code></pre>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="script.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/components/prism-json.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions libs/text-2-measurements/public/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
$(document).ready(function() {
$('#myForm').on('submit', function(e) {
e.preventDefault();
$('#loader').show();
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: $(this).serialize(),
success: function(data) {
$('#loader').hide();
var jsonData = JSON.parse(data); // Parse the response string into a JSON object
$('#response code').text(JSON.stringify(jsonData, null, 2)); // Pretty-print the JSON object
Prism.highlightAll();
$('.card').addClass('has-content'); // Add class to show the response box
},
error: function() {
$('#loader').hide();
$('#response').html('An error occurred.');
$('.card').addClass('has-content'); // Add class to show the response box
}
});
});
});
59 changes: 59 additions & 0 deletions libs/text-2-measurements/public/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
body {
font-family: 'Roboto', sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
padding: 0;
box-sizing: border-box;
}

form {
width: 300px;
padding: 20px;
background-color: #fff;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}

label, textarea, input[type="submit"] {
display: block;
margin-bottom: 10px;
}

textarea {
width: 100%;
height: 100px;
padding: 10px;
box-sizing: border-box;
}

input[type="submit"] {
background-color: #007BFF;
color: #fff;
border: none;
padding: 10px;
cursor: pointer;
transition: background-color 0.3s ease;
}

input[type="submit"]:hover {
background-color: #0056b3;
}

a {
display: block;
text-align: center;
margin-top: 20px;
color: #007BFF;
text-decoration: none;
}

.card {
display: none;
}

.card.has-content {
display: block;
}
80 changes: 80 additions & 0 deletions libs/text-2-measurements/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const express = require('express');
const bodyParser = require('body-parser');
const os = require('os');
const { processStatement } = require('./src/lib/statement-2-measurements');
const session = require('express-session');
const app = express();
const dotenv = require('dotenv');
// Load environment variables from .env file
dotenv.config();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const path = require('path');

// ...

// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
}));

// Route to render the form
app.get('/settings', (req, res) => {
res.send(`
<form action="/settings" method="POST">
<label for="api_key">OpenAI API Key:</label><br>
<input type="text" id="api_key" name="api_key"><br>
<label for="model">OpenAI Model:</label><br>
<select id="model" name="model">
<option value="gpt-3.5-turbo">gpt-3.5-turbo</option>
<option value="gpt-4">gpt-4</option>
<option value="gpt-4-0125-preview">gpt-4-0125-preview</option>
</select><br>
<input type="submit" value="Submit">
</form>
`);
});

// Form submission handler
app.post('/settings', (req, res) => {
// Store user's input in the session
req.session.OPENAI_API_KEY = req.body.api_key;
req.session.OPENAI_MODEL = req.body.model;
res.redirect('/');
});

// Use OPENAI_API_KEY and OPENAI_MODEL in your OpenAI API calls

app.post('/parse', async (req, res) => {
const statement = req.body.statement;
// Before calling the OpenAI API
const OPENAI_API_KEY = req.session.OPENAI_API_KEY || process.env.OPENAI_API_KEY;
const OPENAI_MODEL = req.session.OPENAI_MODEL || process.env.OPENAI_MODEL;
const apiKey = req.headers['t2m-api-key']; // Assuming the API key is sent in the headers

// Check if T2M_API_KEY is set and matches the one in the request
if (process.env.T2M_API_KEY && process.env.T2M_API_KEY !== apiKey) {
return res.status(400).send('Invalid T2M_API_KEY');
}
const result = await processStatement(statement);
res.send(result); // Pretty print the response
});

app.listen(process.env.PORT || 3000, () => {
console.log(`Server is running at http://${os.hostname()}:${process.env.PORT || 3000}`);
});

app.use((req, res, next) => {
console.log(`Request URL: ${req.protocol}://${req.get('host')}${req.originalUrl}`);
next();
});
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,4 @@ function textToMeasurements(question, answer, categories, units) {
module.exports = {
textToMeasurements
};

Loading

0 comments on commit ab96249

Please sign in to comment.