Spaces:
Sleeping
Sleeping
| """ | |
| Python Generator Agent - Uses LLM to generate equivalent Python code from Ada analysis. | |
| """ | |
| from smolagents import ToolCallingAgent | |
| class PythonGeneratorAgent: | |
| """Agent responsible for generating Python code from Ada analysis using LLM.""" | |
| def __init__(self, model): | |
| self.model = model | |
| self.agent = ToolCallingAgent( | |
| tools=[], | |
| model=model | |
| ) | |
| def generate_python_code(self, ada_code: str, parsed_analysis: str, semantic_analysis: str) -> str: | |
| """ | |
| Generate Python code equivalent to the Ada code using LLM. | |
| Args: | |
| ada_code: The original Ada source code | |
| parsed_analysis: Structural analysis from parser agent | |
| semantic_analysis: Semantic analysis from semantic agent | |
| Returns: | |
| Pure Python code string without markdown formatting | |
| """ | |
| try: | |
| prompt = f""" | |
| Generate equivalent Python code for the given Ada code based on the analysis provided. | |
| Requirements: | |
| - Return ONLY pure Python code, no markdown or explanations | |
| - Include proper imports at the top | |
| - Use Python best practices and idiomatic code | |
| - Add type hints where appropriate | |
| - Include docstrings for functions and classes | |
| - Convert Ada types to appropriate Python equivalents: | |
| * Ada records β Python dataclasses or classes | |
| * Ada arrays β Python lists or numpy arrays | |
| * Ada enumerations β Python Enum classes | |
| * Ada procedures β Python functions returning None | |
| * Ada functions β Python functions with return types | |
| Original Ada Code: | |
| ```ada | |
| {ada_code} | |
| ``` | |
| Structural Analysis: | |
| {parsed_analysis} | |
| Semantic Analysis: | |
| {semantic_analysis} | |
| Generate the Python code: | |
| """ | |
| result = self.agent.run(prompt) | |
| return result if isinstance(result, str) else str(result) | |
| except Exception as e: | |
| return f"# Error\n\nFailed to generate Python code: {str(e)}" |