Spaces:
Sleeping
Sleeping
| import json | |
| from typing import Dict | |
| def read_text_file(file_path: str) -> str: | |
| with open(file_path, 'r', encoding='utf-8') as file: | |
| return file.read() | |
| def read_json_file(file_path: str) -> Dict: | |
| with open(file_path, 'r', encoding='utf-8') as file: | |
| return json.load(file) | |
| def write_text_file(file_path: str, content: str): | |
| with open(file_path, 'w', encoding='utf-8') as file: | |
| file.write(content) | |
| def write_json_file(file_path: str, data:dict) -> Dict: | |
| with open(file_path, "w", encoding="utf-8") as json_file: | |
| json_file.write(json.dumps(data, indent=4, ensure_ascii=False)) | |
| def parse_llm_output_to_json(output_text: str) -> dict: | |
| """ | |
| Safely parse LLM output text into a Python dictionary. | |
| """ | |
| start = output_text.find("{") | |
| end = output_text.rfind("}") + 1 | |
| json_str = output_text[start:end] | |
| try: | |
| data = json.loads(json_str) | |
| except: | |
| raise | |
| data = {} | |
| return data | |
| def json_to_markdown(paper): | |
| """ | |
| Converts a paper dictionary to a Markdown string with multi-level headlines. | |
| Args: | |
| paper (dict): The paper dictionary containing problem details and tasks. | |
| Returns: | |
| str: A Markdown-formatted string representing the paper. | |
| """ | |
| markdown_lines = [] | |
| # Problem Background | |
| markdown_lines.append("## Problem Background") | |
| markdown_lines.append(paper.get('problem_background', 'No background provided.') + "\n") | |
| # Problem Requirement | |
| markdown_lines.append("## Problem Requirement") | |
| markdown_lines.append(paper.get('problem_requirement', 'No requirements provided.') + "\n") | |
| # Problem Analysis | |
| markdown_lines.append("## Problem Analysis") | |
| markdown_lines.append(paper.get('problem_analysis', 'No analysis provided.') + "\n") | |
| # Problem Modeling | |
| if 'problem_modeling' in paper: | |
| markdown_lines.append("## Problem Modeling") | |
| markdown_lines.append(paper.get('problem_modeling', 'No modeling provided.') + "\n") | |
| # Tasks | |
| tasks = paper.get('tasks', []) | |
| if tasks: | |
| markdown_lines.append("## Tasks\n") | |
| for idx, task in enumerate(tasks, start=1): | |
| markdown_lines.append(f"### Task {idx}") | |
| task_description = task.get('task_description', 'No description provided.') | |
| markdown_lines.append("#### Task Description") | |
| markdown_lines.append(task_description + "\n") | |
| # Task Analysis | |
| task_analysis = task.get('task_analysis', 'No analysis provided.') | |
| markdown_lines.append("#### Task Analysis") | |
| markdown_lines.append(task_analysis + "\n") | |
| # Mathematical Formulas | |
| task_formulas = task.get('mathematical_formulas', 'No formulas provided.') | |
| markdown_lines.append("#### Mathematical Formulas") | |
| if isinstance(task_formulas, list): | |
| for formula in task_formulas: | |
| markdown_lines.append(f"$${formula}$$") | |
| else: | |
| markdown_lines.append(f"$${task_formulas}$$") | |
| markdown_lines.append("") # Add an empty line | |
| # Mathematical Modeling Process | |
| task_modeling = task.get('mathematical_modeling_process', 'No modeling process provided.') | |
| markdown_lines.append("#### Mathematical Modeling Process") | |
| markdown_lines.append(task_modeling + "\n") | |
| # Result | |
| task_result = task.get('result', 'No result provided.') | |
| markdown_lines.append("#### Result") | |
| markdown_lines.append(task_result + "\n") | |
| # Answer | |
| task_answer = task.get('answer', 'No answer provided.') | |
| markdown_lines.append("#### Answer") | |
| markdown_lines.append(task_answer + "\n") | |
| # Charts | |
| charts = task.get('charts', []) | |
| if charts: | |
| markdown_lines.append("#### Charts") | |
| for i, chart in enumerate(charts, start=1): | |
| markdown_lines.append(f"##### Chart {i}") | |
| markdown_lines.append(chart + "\n") | |
| # # Usage Information | |
| # if 'usage' in paper: | |
| # markdown_lines.append("## Usage Information") | |
| # markdown_lines.append(paper.get('usage', 'No usage information provided.') + "\n") | |
| # Combine all lines into a single string | |
| markdown_str = "\n".join(markdown_lines) | |
| return markdown_str | |
| def json_to_markdown_general(json_data): | |
| """ | |
| Convert a JSON object to a markdown format. | |
| Args: | |
| - json_data (str or dict): The JSON data to convert. It can be a JSON string or a dictionary. | |
| Returns: | |
| - str: The markdown formatted string. | |
| """ | |
| if isinstance(json_data, str): | |
| json_data = json.loads(json_data) # If input is a JSON string, parse it. | |
| def recursive_markdown(data, indent=0): | |
| markdown_str = "" | |
| indent_space = " " * indent | |
| if isinstance(data, dict): | |
| for key, value in data.items(): | |
| markdown_str += f"### {key}\n" | |
| markdown_str += recursive_markdown(value, indent + 1) | |
| elif isinstance(data, list): | |
| for index, item in enumerate(data): | |
| markdown_str += f"- **Item {index + 1}**\n" | |
| markdown_str += recursive_markdown(item, indent + 1) | |
| else: | |
| markdown_str += f"- {data}\n" | |
| return markdown_str | |
| markdown = recursive_markdown(json_data) | |
| return markdown | |