Elfsong/Venus_Annotation_System
2
1# Venus Annotation System2# Author: Du Mingzhe (mingzhe@nus.edu.sg)3# Date: 2024-09-254 5import uuid6import streamlit as st7import streamlit_ext as ste8from code_editor import code_editor9from datasets import load_dataset, Dataset10 11case_generation = """12Given the problem description and the canonical solution, write these functions and return in the given JSON format. Import all neccessary libraries in the code.13 14Problem Description:15{problem_description}16 17Canonical Solution:18{canonical_solution}19 20{{21 "generate_test_case_input": "a {lang} function 'generate_test_case_input() → Turple' that randomly generate a test case input Turple from a reasonable test range. Wrap the test case input in a tuple.", 22 "serialize_input": "a {lang} function 'serialize_input(Turple) → Str' that takes the test case input {lang} Turple, and generates the serialized test case input string.", 23 "deserialize_input": "a {lang} function 'deserialize_input(Str) → Turple' that takes the serialized test case input string, and generate the {lang} test case input Turple.", 24 "serialize_output": "a {lang} function 'serialize_output(Turple) → Str' that takes the test case output {lang} Turple, and generates the serialized test case output string.", 25 "deserialize_output": "a {lang} function 'deserialize_output(Str) → Turple' that takes the serialized test case output string, and generate the {lang} test case output Turple.", 26 "entry_point": "the entry point function name of the canonical solution"27}}28 29Example 1:30Problem Description:31<p>Given an array of integers <code>nums</code> and an integer <code>target</code>, return <em>indices of the two numbers such that they add up to <code>target</code></em>.</p> <p>You may assume that each input would have <strong><em>exactly</em> one solution</strong>, and you may not use the <em>same</em> element twice.</p> <p>You can return the answer in any order.</p> <p> </p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,7,11,15], target = 9 <strong>Output:</strong> [0,1] <strong>Explanation:</strong> Because nums[0] + nums[1] == 9, we return [0, 1]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,4], target = 6 <strong>Output:</strong> [1,2] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3], target = 6 <strong>Output:</strong> [0,1] </pre> <p> </p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= nums.length <= 10<sup>4</sup></code></li> <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li> <li><strong>Only one valid answer exists.</strong></li> </ul> <p> </p> <strong>Follow-up: </strong>Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code><font face="monospace"> </font>time complexity?32 33Canonical Solution:34class Solution:35 def twoSum(self, nums: List[int], target: int) -> List[int]:36 num_map = {{}}37 for i, num in enumerate(nums):38 complement = target - num39 if complement in num_map:40 return [num_map[complement], i]41 num_map[num] = i42 43Response:44{{45 "generate_test_case_input": "import random\nfrom typing import List, Tuple\n\ndef generate_test_case_input() -> Tuple[List[int], int]:\n length = random.randint(2, 10000)\n nums = [random.randint(-10**9, 10**9) for _ in range(length)]\n idx1, idx2 = random.sample(range(length), 2)\n target = nums[idx1] + nums[idx2]\n return nums, target",46 "serialize_input": "from typing import List, Tuple\n\ndef serialize_input(input: Tuple[List[int], int]) -> str:\n nums, target = input\n return f'{{nums}}\\n{{target}}'\n",47 "deserialize_input": "from typing import List, Tuple\n\ndef deserialize_input(serialized: str) -> Tuple[List[int], int]:\n parts = serialized.strip().split('\\n')\n nums = eval(parts[0])\n target = int(parts[1])\n return nums, target\n",48 "serialize_output": "from typing import List\n\ndef serialize_output(output: List[int]) -> str:\n return str(output)\n",49 "deserialize_output": "from typing import List\n\ndef deserialize_output(serialized: str) -> List[int]:\n return eval(serialized)\n",50 "entry_point": "twoSum"51}}52"""53 54 55st.title(":blue[Venus] Annotation System 🪐")56 57# Step 1: Load the problem set58language = ste.selectbox("Select a problem here", ['python3', 'cpp', 'rust', 'javascript', 'golang', 'java'])59st.write(f"Ok! let's go with [{language}]")60 61my_bar = st.progress(0, text="Loading the problem set...")62 63my_bar.progress(10, text="Loading [Elfsong/Venus]-[{language}] datasets...")64if "raw_ds" not in st.session_state.keys():65 st.session_state["raw_ds"] = load_dataset("Elfsong/Venus", language)66raw_ds = st.session_state["raw_ds"]67 68my_bar.progress(55, text=f"Loading [Elfsong/venus_case]-[{language}] datasets...")69if "case_ds" not in st.session_state.keys():70 st.session_state["case_ds"] = load_dataset("Elfsong/venus_case", language)71case_ds = st.session_state["case_ds"]72 73my_bar.progress(90, text="Filtering out the cases that already exist...")74if "candidates" not in st.session_state.keys():75 case_ds_ids = set(case_ds['train']['question_id'])76 candidates = [raw_ds['train'][i] for i in range(len(raw_ds['train'])) if raw_ds['train'][i]['question_id'] not in case_ds_ids]77 st.session_state["candidates"] = candidates78candidates = st.session_state["candidates"]79 80my_bar.progress(100, text="System Initialized Successfully 🚀")81 82# Step 2: Select the problem83candidates_dict = {}84for candidate in candidates:85 candidate_name = str(candidate['question_id']) + '.' + str(candidate['name']) + ' [' + str(candidate['difficulty']).upper() + ']'86 candidates_dict[candidate_name] = candidate87option = ste.selectbox("Select a problem here", candidates_dict.keys())88example = candidates_dict[option]89 90tab1, tab2, tab3, tab4 = st.tabs(["Problem Description", "Canonical Solution", "Prompt","Test Cases Generator"])91 92with tab1:93 st.html(example['content'])94 95with tab2:96 solutions_displayed = 097 canonical_solutions = list()98 for solution in example['rt_list']:99 if "Solution" in solution['code']:100 st.write(f"Canonical Solution {solutions_displayed + 1}")101 st.code(solution['code'])102 canonical_solutions.append(solution['code'])103 solutions_displayed += 1104 if solutions_displayed >= 3:105 break106 107with tab3:108 prompt = case_generation.format(problem_description=example['content'], canonical_solution=canonical_solutions[0], lang=language)109 st.html(prompt)110 111with tab4:112 editor_buttons = [{113 "name": "Submit", 114 "feather": "Play",115 "primary": True, 116 "hasText": True, 117 "showWithIcon": True, 118 "commands": ["submit"], 119 "style": {"bottom": "0.44rem","right": "0.4rem"}120 }]121 predefined_code = "def generate_test_cases():\n\tpass\n\ndef serialize_input():\n\tpass\n\ndef deserialize_input():\n\tpass\n\ndef serialize_output():\n\tpass\n\ndef deserialize_output():\n\tpass"122 response_dict = code_editor(predefined_code, lang="python", height=20, options={"wrap": False}, buttons=editor_buttons)123 st.write("Click 'Submit' bottom right to upload your functions.")124 if response_dict['type'] == 'submit':125 new_ds = Dataset.from_list([{126 "question_id": example['question_id'],127 "test_case_functions": response_dict['text'],128 }])129 130 ds_name = str(uuid.uuid1())131 qid = example['question_id']132 new_ds.push_to_hub(f"Elfsong/Venus_Anotation", f'{language}-{qid}-{ds_name}')133 st.divider()134 st.write("Thanks for your contribution! 🌟")135 136 