Skip to content

Commit

Permalink
Merge pull request #20 from Barqawiz/gen-html
Browse files Browse the repository at this point in the history
Gen function to generate html pages.
  • Loading branch information
Barqawiz authored May 29, 2023
2 parents 9f639c8 + 58128f9 commit 27471a1
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 27 deletions.
18 changes: 10 additions & 8 deletions IntelliNode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const { SemanticSearch } = require('intellinode');
```js
const search = new SemanticSearch(apiKey);
// pivotItem: item to search.
// searchArray: array of strings to search through.
const results = await search.getTopMatches(pivotItem, searchArray, numberOfMatches);
const filteredArray = search.filterTopMatches(results, searchArray)
```
Expand All @@ -50,14 +49,17 @@ const { Gen } = require('intellinode');
```
2. call:
```js
// one line to generate marketing description
const marketingDesc = await Gen.get_marketing_desc('gaming chair', openaiApiKey);

// or one line to generate blog post
// one line to generate blog post
const blogPost = await Gen.get_blog_post(prompt, openaiApiKey);

// or generate images from production description
const image = await Gen.generate_image_from_desc(prompt, openaiApiKey, stabilityApiKey, true);
```
```js
// or generate images from description
const image = await Gen.generate_image_from_desc(prompt, openaiApiKey, stabilityApiKey);
```
```js
// or generate html page code
text = 'a registration page with flat modern theme.'
await Gen.save_html_page(text, folder, file_name, openaiKey);
```

## Models Access
Expand Down
27 changes: 26 additions & 1 deletion IntelliNode/function/Gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const ImageModelInput = require("../model/input/ImageModelInput");
const Text2SpeechInput = require("../model/input/Text2SpeechInput");
const { Chatbot, SupportedChatModels } = require("../function/Chatbot");
const { ChatGPTInput, ChatGPTMessage } = require("../model/input/ChatModelInput");

const SystemHelper = require("../utils/SystemHelper");
const fs = require('fs');
const path = require('path');

class Gen {
static async get_marketing_desc(prompt, openaiKey) {
Expand Down Expand Up @@ -53,6 +55,29 @@ class Gen {
const audioContent = await speechModel.generateSpeech(input);
return audioContent;
}

static async generate_html_page(text, openaiKey) {
// load and fill the template
const promptTemplate = new SystemHelper().loadPrompt("html_page");
const prompt = promptTemplate.replace("${text}", text);

// prepare the bot
const chatbot = new Chatbot(openaiKey);
const input = new ChatGPTInput('generate only html, css and javascript based on the user request in the following format {"html": "<code>", "message":"<text>"}',
{ maxTokens: 2000, model: 'gpt-4' });
// set the user message with the template
input.addUserMessage(prompt);
const responses = await chatbot.chat(input);
return JSON.parse(responses[0].trim());
}

static async save_html_page(text, folder, file_name, openaiKey) {
const htmlCode = await Gen.generate_html_page(text, openaiKey);
const folderPath = path.join(folder, file_name + '.html');
fs.writeFileSync(folderPath, htmlCode['html']);
return true;
}

}

module.exports = { Gen };
4 changes: 2 additions & 2 deletions IntelliNode/function/TextAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TextAnalyzer {
}

async summarize(text, options = {}) {
const summaryPromptTemplate = this.systemHelper.loadSystem("summary");
const summaryPromptTemplate = this.systemHelper.loadPrompt("summary");
const prompt = summaryPromptTemplate.replace("${text}", text);
const modelInput = new LanguageModelInput({
prompt,
Expand All @@ -37,7 +37,7 @@ class TextAnalyzer {
}

async sentimentAnalysis(text, options = {}) {
const mode = this.systemHelper.loadSystem("sentiment");
const mode = this.systemHelper.loadPrompt("sentiment");
const prompt = `${mode}\n\nAnalyze the sentiment of the following text: ${text}\n\nSentiment: `;

const modelInput = new LanguageModelInput({
Expand Down
2 changes: 1 addition & 1 deletion IntelliNode/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "intellinode",
"version": "0.0.18",
"version": "0.0.19",
"description": "Access various AI models, such as ChatGPT, Diffusion, Cohere, Google, and others",
"main": "index.js",
"keywords": ["AI", "ChatGPT", "GPT4", "stable diffusion", "openai", "huggingface", "language models", "image generation", "speech synthesis", "semantic search"],
Expand Down
14 changes: 14 additions & 0 deletions IntelliNode/resource/templates/html_page_prompt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Generate website, javascript and css in one page based on the user request.

Output example:
{"html": "<html><head>....</head><body></body></html>", "message"":"the page ready for render"}

Output example for unrelated request:
{"html": "", "message":"the request does not fit in one page"}

If an image generated, add the image description in alt to use for image generation using DALL·E 2:
<img src="<image name and format>" alt="<image description to use with DALL·E 2>" width="<size or percentage>" height="<size or percentage>">

user request: ${text}

output:
27 changes: 25 additions & 2 deletions IntelliNode/test/Gen.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Gen = require("../function/Gen");
const { Gen } = require("../function/Gen");
require("dotenv").config();
const assert = require("assert");

const fs = require('fs');

const openaiApiKey = process.env.OPENAI_API_KEY;
const stabilityApiKey = process.env.STABILITY_API_KEY;
Expand Down Expand Up @@ -35,9 +35,32 @@ async function testGenerateSpeechSynthesis() {
assert(speech.length > 0, "Test passed");
}


async function testGenerateHtmlPage() {
const tempDir = '../temp';
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
}

const text = "a registration page with flat modern theme.";
const htmlCode = await Gen.generate_html_page(text, openaiApiKey);

fs.writeFileSync(`${tempDir}/generated_page_test.html`, htmlCode["html"]);

assert(htmlCode["html"].length > 0, "Test passed");
}

async function testSaveHTML() {
prompt = "a registration page with flat modern theme."
status = await Gen.save_html_page(prompt, folder='../temp', file_name='test_register', openaiKeyclea=openaiApiKey);
assert.strictEqual(status, true, "Test passed");
}

(async () => {
await testGetMarketingDesc();
await testGetBlogPost();
await testGenerateImageFromDesc();
await testGenerateSpeechSynthesis();
await testGenerateHtmlPage();
await testSaveHTML();
})();
20 changes: 12 additions & 8 deletions IntelliNode/utils/SystemHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ const path = require("path");

class SystemHelper {
constructor() {
this.systemsPath = path.join(__dirname, "..", "resource", "systems");
this.systemsPath = path.join(__dirname, "..", "resource", "templates");
}

loadSystem(file_type) {
loadPrompt(file_type) {
if (file_type === "sentiment") {
const sentimentSystemPath = path.join(this.systemsPath, "sentiment_system.in");
const sentimentSystem = fs.readFileSync(sentimentSystemPath, "utf8");
return sentimentSystem;
const sentimentSystemPath = path.join(this.systemsPath, "sentiment_prompt.in");
const promptTemplate = fs.readFileSync(sentimentSystemPath, "utf8");
return promptTemplate;
} else if (file_type === "summary") {
const summarySystemPath = path.join(this.systemsPath, "summary_system.in");
const summarySystem = fs.readFileSync(summarySystemPath, "utf8");
return summarySystem;
const summarySystemPath = path.join(this.systemsPath, "summary_prompt.in");
const promptTemplate = fs.readFileSync(summarySystemPath, "utf8");
return promptTemplate;
} else if (file_type === "html_page") {
const promptPath = path.join(this.systemsPath, "html_page_prompt.in");
const promptTemplate = fs.readFileSync(promptPath, "utf8");
return promptTemplate;
} else {
throw new Error(`File type '${file_type}' not supported`);
}
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ const { Gen } = require('intellinode');
```
call:
```js
// one line to generate marketing description
const marketingDesc = await Gen.get_marketing_desc('gaming chair', openaiApiKey);

// or generate images from products description
const image = await Gen.generate_image_from_desc(prompt, openaiApiKey, stabilityApiKey, true);
// one line to generate blog post
const blogPost = await Gen.get_blog_post(prompt, openaiApiKey);
```
```js
// or generate html page code
text = 'a registration page with flat modern theme.'
await Gen.save_html_page(text, folder, file_name, openaiKey);
```
For more examples, check [the samples](https://github.com/Barqawiz/IntelliNode/tree/main/samples/command_sample).

Expand Down
Binary file added images/model_output/register-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 27471a1

Please sign in to comment.