Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
GitOrigin-RevId: e0f6d40a13463dd73d6a0b228a7701ba9d6263b2
  • Loading branch information
alyssachvasta authored and copybara-github committed Dec 11, 2024
1 parent 2f7bc48 commit ea62440
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 53 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"husky": "^9.1.6",
"jest": "^29.7.0",
"lint-staged": "^15.2.10",
"marked": "^15.0.3",
"nodemon": "^3.1.4",
"papaparse": "^5.4.1",
"prettier": "^3.3.3",
Expand Down
51 changes: 51 additions & 0 deletions runner-cli/rerunner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Rerun summarize 5x using a CSV file as input and outputting the summaries to another CSV.
// Run like:
// npx ts-node ./evaluations/rerunner.ts --outputFile "data1.csv" \
// --vertexProject "<your project name here>" \
// --inputFile "/usr/local/google/home/achvasta/Downloads/comments-with-vote-tallies.csv"
// --rerunCount 3

import { Command } from "commander";
import { createObjectCsvWriter } from "csv-writer";
import { getCommentsFromCsv, getSummary } from "./runner_utils";

interface outputCsvFormat {
run: number;
summaryType: string;
text: string;
}

async function main(): Promise<void> {
// Parse command line arguments.
const program = new Command();
program
.option("-o, --outputFile <file>", "The output file name.")
.option("-i, --inputFile <file>", "The input file name.")
.option("-r, --rerunCount <count>", "The number of times to rerun.")
.option("-v, --vertexProject <project>", "The Vertex Project name.");
program.parse(process.argv);
const options = program.opts();

const comments = await getCommentsFromCsv(options.inputFile);

let outputTexts: outputCsvFormat[] = [];
const csvWriter = createObjectCsvWriter({
path: options.outputFile,
header: ["run", "summaryType", "text"],
});

for (let i = 0; i < options.rerunCount; i++) {
const summary = await getSummary(options.vertexProject, comments);
outputTexts = outputTexts.concat([
{
run: i,
summaryType: "VoteTally",
text: summary.getText("MARKDOWN"),
},
]);
}

csvWriter.writeRecords(outputTexts).then(() => console.log("CSV file written successfully."));
}

main();
47 changes: 47 additions & 0 deletions runner-cli/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Run the summarizer based on a CSV input and output the result as an hmtl page.

import { Command } from "commander";
import * as fs from "fs";
import { marked } from "marked";
import { getCommentsFromCsv, getSummary } from "./runner_utils";

async function main(): Promise<void> {
// Parse command line arguments.
const program = new Command();
program
.option("-o, --outputFile <file>", "The output file name.")
.option("-i, --inputFile <file>", "The input file name.")
.option("-v, --vertexProject <project>", "The Vertex Project name.");
program.parse(process.argv);
const options = program.opts();

const comments = await getCommentsFromCsv(options.inputFile);

const summary = await getSummary(options.vertexProject, comments);
const markdownContent = summary.getText("MARKDOWN");
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<title>Summary</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
</style>
</head>
<body>
${marked(markdownContent)}
</body>
</html>`;

const outputPath = `${options.outputFile}.html`;
fs.writeFileSync(outputPath, htmlContent);
console.log(`Written summary to ${outputPath}`);
}

main();
56 changes: 3 additions & 53 deletions evaluations/rerunner.ts → runner-cli/runner_utils.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
// Rerun summarize 5x using a CSV file as input and outputting the summaries to another CSV.
// Run like:
// npx ts-node ./evaluations/rerunner.ts --outputFile "data1.csv" \
// --vertexProject "<your project name here>" \
// --inputFile "/usr/local/google/home/achvasta/Downloads/comments-with-vote-tallies.csv"
// --rerunCount 3

import { Command } from "commander";
import { Sensemaker } from "../src/sensemaker";
import { VertexModel } from "../src/models/vertex_model";
import { Comment, SummarizationType, Summary, VoteTally } from "../src/types";
import { createObjectCsvWriter } from "csv-writer";
import { Summary, VoteTally, Comment, SummarizationType } from "../src/types";
import * as path from "path";
import * as fs from "fs";
import { parse } from "csv-parse";

interface outputCsvFormat {
run: number;
summaryType: string;
text: string;
}

// TODO: remove this and make it more general
type VoteTallyCsvRow = {
index: number;
Expand All @@ -40,14 +25,14 @@ type VoteTallyCsvRow = {
"group-1-agree-count": number;
};

async function getSummary(project: string, comments: Comment[]): Promise<Summary> {
export async function getSummary(project: string, comments: Comment[]): Promise<Summary> {
const sensemaker = new Sensemaker({
defaultModel: new VertexModel(project, "us-central1", "gemini-1.5-pro-002"),
});
return await sensemaker.summarize(comments, SummarizationType.VOTE_TALLY);
}

async function getCommentsFromCsv(inputFilePath: string): Promise<Comment[]> {
export async function getCommentsFromCsv(inputFilePath: string): Promise<Comment[]> {
const filePath = path.resolve(inputFilePath);
const fileContent = fs.readFileSync(filePath, { encoding: "utf-8" });

Expand Down Expand Up @@ -85,38 +70,3 @@ async function getCommentsFromCsv(inputFilePath: string): Promise<Comment[]> {
.on("end", () => resolve(data));
});
}

async function main(): Promise<void> {
// Parse command line arguments.
const program = new Command();
program
.option("-o, --outputFile <file>", "The output file name.")
.option("-i, --inputFile <file>", "The input file name.")
.option("-r, --rerunCount <count>", "The number of times to rerun.")
.option("-v, --vertexProject <project>", "The Vertex Project name.");
program.parse(process.argv);
const options = program.opts();

const comments = await getCommentsFromCsv(options.inputFile);

let outputTexts: outputCsvFormat[] = [];
const csvWriter = createObjectCsvWriter({
path: options.outputFile,
header: ["run", "summaryType", "text"],
});

for (let i = 0; i < options.rerunCount; i++) {
const summary = await getSummary(options.vertexProject, comments);
outputTexts = outputTexts.concat([
{
run: i,
summaryType: "VoteTally",
text: summary.getText("MARKDOWN"),
},
]);
}

csvWriter.writeRecords(outputTexts).then(() => console.log("CSV file written successfully."));
}

main();

0 comments on commit ea62440

Please sign in to comment.