Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| from pathlib import Path | |
| from string import ascii_lowercase | |
| def convert_csv_to_markdown(csv_path: Path) -> str: | |
| # Load the CSV file without using the first row as headers | |
| df = pd.read_csv(csv_path, header=None) | |
| # Manually set column names | |
| df.columns = ["Type", "Group", "Points", "Question", "Correct Answer", | |
| "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", | |
| "Page", "Taxonomy"] | |
| # Generate Markdown output | |
| markdown_output = "## Generated Questions\n\n" | |
| for index, row in df.iterrows(): | |
| question = row["Question"] | |
| correct_index = row["Correct Answer"] | |
| # Skip rows where the correct answer index is missing | |
| if pd.isna(correct_index): | |
| continue | |
| correct_index = int(correct_index) - 1 # Convert 1-based index to 0-based | |
| choices = row[["Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5"]].dropna().tolist() | |
| metadata = row[["Page", "Taxonomy"]].dropna().tolist() | |
| markdown_output += f"{index+1}. {question}<br>" | |
| for i, choice in enumerate(choices): | |
| letter = ascii_lowercase[i] # Get letter a, b, c, etc. | |
| # Indent choices and bold the correct answer | |
| if i == correct_index: | |
| markdown_output += f"  {letter}. **{choice}**<br>" | |
| else: | |
| markdown_output += f"  {letter}. {choice}<br>" | |
| # Add metadata as additional choices, with modified page format | |
| for meta in metadata: | |
| if meta == row["Page"]: # If this metadata item is the page | |
| # Remove first character and prepend "Pages" | |
| page_number = meta[1:] # Remove first character | |
| markdown_output += f" - Pages {page_number}<br>" | |
| else: | |
| markdown_output += f" - {meta}<br><br>" | |
| markdown_output += "<br>" # Space between questions | |
| return markdown_output | |