Skip to content

Commit

Permalink
Add image-to-markdown converter example files and configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
manodyaSenevirathne committed Aug 14, 2024
1 parent fd9525d commit f6ddc18
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/image-to-markdown-converter/.github/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
../image to markdown converter.md
11 changes: 11 additions & 0 deletions examples/image-to-markdown-converter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Ballerina generates this directory during the compilation of a package.
# It contains compiler-generated artifacts and the final executable if this is an application package.
target/

# Ballerina maintains the compiler-generated source code here.
# Remove this if you want to commit generated sources.
generated/

# Contains configuration values used during development time.
# See https://ballerina.io/learn/provide-values-to-configurable-variables/ for more details.
Config.toml
14 changes: 14 additions & 0 deletions examples/image-to-markdown-converter/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
org = "manodya"
name = "CLI_assistant"
version = "0.1.0"
distribution = "2201.9.2"

[[dependency]]
org = "ballerinax"
name = "openai.chat"
version = "2.0.1"
repository = "local"

[build-options]
observabilityIncluded = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Image to Markdown Document Converter

This use case demonstrates how openai API v1 can be utilized to generate markdown documentation from visual content. It starts by converting an image file—such as a whiteboard sketch or diagram—into a base64-encoded string. Using the OpenAI API v1's vision capabilities it can generate a markdown document that includes detailed descriptions of the image's contents, such as diagrams, notes, or code snippets. The resulting markdown is structured with appropriate headings and summaries, and saved as a `.md` file in the same directory as the original image.

## Prerequisites

1. Generate a API as described in the [Setup guide](https://central.ballerina.io/ballerinax/openai.chat/latest#setup-guide).

2. For each example, create a `Config.toml` file the related configuration. Here's an example of how your `Config.toml` file should look:

```toml
token = "<API Key>"
```

## Running an Example

Execute the following commands to build an example from the source:

* To build an example:

```bash
bal build
```

* To run an example:

```bash
bal run
```
90 changes: 90 additions & 0 deletions examples/image-to-markdown-converter/main.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/file;
import ballerina/io;
import ballerinax/openai.chat;

configurable string token = ?;
final chat:Client openAIChat = check new ({auth: {token}});

public function main() returns error? {
string imagePath = getImageFilePath();
string|error base64Image = encodeImageToBase64(imagePath);

if base64Image is error {
io:println(base64Image);
return;
}
string|error markdownDoc = generateDocumentation(base64Image);

if markdownDoc is error {
io:println(markdownDoc);
return;
}
check saveMarkdownToFile(markdownDoc, imagePath);
io:println("Markdown documentation generated and saved successfully.");
}

function getImageFilePath() returns string {
io:println("Enter the path to the image file:");
return io:readln().trim();
}

function encodeImageToBase64(string imagePath) returns string|error {
byte[] imageBytes = check io:fileReadBytes(imagePath);
return imageBytes.toBase64();
}

function generateDocumentation(string base64Image) returns string|error {
string prompt = "Generate markdown documentation based on the content of the following image. Include detailed descriptions of any diagrams, notes, or code snippets present. Structure the documentation with appropriate headings, and provide a summary of the key concepts discussed. Additionally, include any relevant annotations or comments that might aid in understanding the content";

chat:CreateChatCompletionRequest request = {
model: "gpt-4o-mini",
messages: [
{
role: "user",
content: [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": string `data:image/jpeg;base64,${base64Image}`
}
}
]
}
],
max_tokens: 300
};
chat:CreateChatCompletionResponse response = check openAIChat->/chat/completions.post(request);
string? markdownDoc = response.choices[0].message.content;

if markdownDoc is () {
return error("Failed to generate markdown documentation.");
}
return markdownDoc;
}

function saveMarkdownToFile(string markdownDoc, string imagePath) returns error? {
string imageName = check file:basename(imagePath);
string parentPath = check file:parentPath(imagePath);
string docName = string `${re `\.`.split(imageName)[0]}_documentation.md`;
check io:fileWriteBytes(parentPath + "/" + docName, markdownDoc.toBytes());
}

0 comments on commit f6ddc18

Please sign in to comment.