Skip to content

Instantly share code, notes, and snippets.

@luisquintanilla
Created November 26, 2024 20:51
Show Gist options
  • Save luisquintanilla/9065c770211fc31a7d7b5833a8117ebc to your computer and use it in GitHub Desktop.
Save luisquintanilla/9065c770211fc31a7d7b5833a8117ebc to your computer and use it in GitHub Desktop.
AI Webpage to .NET DataFrame

Prerequisites

  • .NET 9 SDK
  • Install the following NuGet Packages
    • Microsoft.Extensions.AI.AzureAIInference
    • Microsoft.Extensions.AI
    • Azure.Identity
    • Microsoft.Data.Analysis
  • GitHub Personal Access Token

Input

What the input source image looks like

Image of .NET Supported Versions Table in .NET Website

Output

.NET Version Original release date Latest patch version Patch release date Release type Support phase End of support
.NET 9 11/12/2024 9.0.0 11/12/2024 STS Active 5/12/2026
.NET 8 11/14/2023 8.0.11 11/12/2024 LTS Active 11/10/2026
using Microsoft.Extensions.AI;
using Azure.AI.Inference;
using Azure;
using ChatRole = Microsoft.Extensions.AI.ChatRole;
using Microsoft.Data.Analysis;
IChatClient client =
new ChatCompletionsClient(
endpoint: new Uri("https://models.inference.ai.azure.com"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("GH_TOKEN")))
.AsChatClient("gpt-4o-mini");
var systemPrompt =
"""
You are a data entry AI assistant.
You help users take data from screenshots and convert it into structured data.
""";
var messages = new List<ChatMessage>
{
new ChatMessage(ChatRole.System, systemPrompt),
new ChatMessage(ChatRole.User, new AIContent[] {
new ImageContent("https://gist.github.com/user-attachments/assets/92bc605d-7d63-4af7-82c9-f4c1887028ab", "image/jpeg"),
new TextContent("Using this screenshot, extract the table data and format as CSV. Escape ',' separator so there are no conflicts with the CSV format."),
})
};
var jsonResult = await client.CompleteAsync<DFResult>(messages, options: new ChatOptions {Temperature = 0.1f});
var res = jsonResult.Result;
var df = DataFrame.LoadCsvFromString(res.AIResult, header: res.HasHeader, separator: ',');
DataFrame.SaveCsv(df, "data.csv");
class DFResult
{
public string AIResult { get; set; }
public bool HasHeader { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment