CoolFace
Apppublic

vaisnav25/Recursive-Code-Generator-Executor

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
App README

๐Ÿค– Recursive AI Executor (RAIE)

An intelligent Python code generation and execution platform that learns from failures and progressively improves code quality through recursive AI-powered debugging.

โœจ Features

๐Ÿง  Intelligent Retry Logic

  • โ€”Progressive Learning: Each failed attempt informs the next generation cycle
  • โ€”Error Pattern Recognition: Automatically categorizes and learns from different error types
  • โ€”Adaptive System Prompts: AI prompts become more sophisticated with each retry
  • โ€”Success Rate Tracking: Monitor improvement over time

๐Ÿ” Advanced Error Analysis

  • โ€”Automatic Error Categorization: Syntax, Import, Name, Type, Index, Key, and Indentation errors
  • โ€”Context-Aware Suggestions: Tailored recommendations based on error patterns
  • โ€”Line Number Extraction: Pinpoint exact error locations
  • โ€”Cumulative Learning: Builds knowledge from all previous attempts

๐Ÿ’ป Code Execution Engine

  • โ€”Sandboxed Execution: Safe code execution in temporary files
  • โ€”Timeout Protection: Prevents infinite loops with 15-second timeout
  • โ€”Comprehensive Error Reporting: Detailed stdout/stderr capture
  • โ€”Real-time Status Updates: Live progress tracking during execution

๐Ÿ“Š Enhanced Analytics

  • โ€”Execution History: Complete record of all code generation sessions
  • โ€”Learning Insights: Visual indicators of when AI learning is applied
  • โ€”Performance Metrics: Success rates and attempt statistics
  • โ€”Detailed Logs: Comprehensive debugging information for each attempt

๐Ÿš€ Installation

Prerequisites

  • โ€”Python 3.7 or higher
  • โ€”Mistral AI API key

Setup

  1. 1.Clone or download the repository
bash
   git clone <repository-url>
   cd raie
  1. 1.Install dependencies
bash
   pip install -r requirements.txt
  1. 1.Set up environment variables

Create a .env file in the project root:

env
   MISTRAL_API_KEY=your_mistral_api_key_here

Alternatively, you can enter the API key directly in the web interface.

  1. 1.Run the application
bash
   streamlit run raie.py
  1. 1.Access the web interface

Open your browser and navigate to http://localhost:8501

๐ŸŽฏ Usage

Basic Workflow

  1. 1.Enter Your Programming Task
  2. 2.Describe what you want the code to do
  3. 3.Be specific about requirements and expected behavior
  4. 4.Example: "Create a function to find prime numbers up to n=100 and display them"
  1. 1.Configure Settings (Optional)
  2. 2.Adjust maximum retry attempts (1-10)
  3. 3.Enable/disable learning context display
  4. 4.Monitor success rates and metrics
  1. 1.Generate & Execute
  2. 2.Click "๐Ÿš€ Generate & Execute"
  3. 3.Watch the AI progressively improve code through multiple attempts
  4. 4.View real-time error analysis and learning insights
  1. 1.Review Results
  2. 2.Examine generated code and execution output
  3. 3.Download successful code for later use
  4. 4.Analyze execution logs for debugging insights

Example Prompts

โœ… Good Prompts:
- "Create a password generator with customizable length and character sets"
- "Build a CSV file reader that handles missing values and calculates statistics"
- "Write a web scraper for extracting product prices from HTML"
- "Generate a simple calculator with error handling for division by zero"

โŒ Avoid:
- Vague requests: "write some code"
- Requiring external APIs without credentials
- Tasks needing non-standard libraries (RAIE uses only Python standard library)

๐Ÿ—๏ธ Architecture

Core Components

๐Ÿ”ง ErrorAnalyzer
  • โ€”Purpose: Categorizes and analyzes execution errors
  • โ€”Key Methods:
  • โ€”categorize_error(): Classifies error types and extracts metadata
  • โ€”build_learning_context(): Creates cumulative learning from all attempts
๐Ÿค– MistralAIClient
  • โ€”Purpose: Handles AI code generation with progressive intelligence
  • โ€”Key Methods:
  • โ€”generate_code(): Creates code with attempt-aware prompting
  • โ€”_build_system_prompt(): Constructs increasingly sophisticated system prompts
  • โ€”_clean_generated_code(): Removes markdown artifacts from generated code
โšก CodeExecutor
  • โ€”Purpose: Safely executes Python code in sandboxed environment
  • โ€”Key Methods:
  • โ€”execute_python_code(): Runs code with timeout and error capture

Learning Progression

Attempt 1: Basic code generation
    โ†“
Attempt 2-3: Debugging mode with error analysis
    โ†“
Attempt 4+: Expert recovery mode with complete rewrites

๐Ÿ”ง Configuration

Environment Variables

env
MISTRAL_API_KEY=your_api_key_here    # Required: Mistral AI API key

Runtime Settings

  • โ€”Max Retry Attempts: 1-10 (default: 5)
  • โ€”Show Learning Context: Display AI learning insights
  • โ€”Success Rate Tracking: Monitor performance metrics

๐Ÿ“Š Error Categories

RAIE automatically recognizes and learns from these error types:

CategoryExamplesAI Learning Response
SyntaxInvalid syntax, unexpected EOFEnhanced syntax checking
ImportModuleNotFoundErrorStandard library alternatives
NameUndefined variablesVariable definition validation
TypeWrong argument countsType checking and validation
IndexList index out of rangeBounds checking
KeyDictionary key errorsKey existence validation
IndentationInconsistent spacingFormatting corrections

๐Ÿ”’ Security Features

  • โ€”Sandboxed Execution: Code runs in temporary files with limited permissions
  • โ€”Timeout Protection: 15-second execution limit prevents runaway processes
  • โ€”No External Dependencies: Uses only Python standard library for safety
  • โ€”Input Validation: Comprehensive error handling throughout the application

๐Ÿค Contributing

Code Structure

raie.py
โ”œโ”€โ”€ ErrorAnalyzer        # Error categorization and learning
โ”œโ”€โ”€ MistralAIClient     # AI code generation
โ”œโ”€โ”€ CodeExecutor        # Safe code execution
โ””โ”€โ”€ main()              # Streamlit interface

Development Guidelines

  • โ€”Follow Python PEP 8 style guidelines
  • โ€”Add comprehensive error handling
  • โ€”Include docstrings for all classes and methods
  • โ€”Test edge cases and error conditions

๐Ÿ“ˆ Performance Tips

For Better Results

  • โ€”Be Specific: Detailed prompts generate better code
  • โ€”Include Context: Mention expected inputs, outputs, and constraints
  • โ€”Error Tolerance: Let RAIE learn from failures - don't stop at first error
  • โ€”Review Logs: Use execution analytics to understand AI learning patterns

Troubleshooting

  • โ€”API Errors: Verify Mistral AI API key and network connectivity
  • โ€”Execution Timeouts: Simplify complex algorithms or increase timeout
  • โ€”Memory Issues: Restart application if session state becomes corrupted

๐Ÿ“„ License

This project is open source. Please check the license file for specific terms.

๐Ÿ†˜ Support

If you encounter issues:

  1. 1.Check the execution logs for detailed error information
  2. 2.Verify your Mistral AI API key is valid
  3. 3.Ensure all dependencies are installed correctly
  4. 4.Review the troubleshooting section above

๐Ÿ”„ Version History

Current Features

  • โ€”โœ… Recursive AI-powered code generation
  • โ€”โœ… Progressive error learning
  • โ€”โœ… Comprehensive execution analytics
  • โ€”โœ… Real-time status updates
  • โ€”โœ… Code download functionality

Roadmap

  • โ€”๐Ÿ”„ Support for additional AI models
  • โ€”๐Ÿ”„ Custom error pattern training
  • โ€”๐Ÿ”„ Code performance optimization suggestions
  • โ€”๐Ÿ”„ Integration with version control systems

Built with โค๏ธ and powered by AI intelligence that gets smarter with every failure!