Skip to content

Ali-Gatorre/python-maker-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

51 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿค– Python Maker Bot v0.2.1

An AI-Powered Python Code Generator Built with Rust

๐ŸŽฎ Now with Interactive Mode for games, GUIs, and programs requiring user input!

A Rust-based interactive shell that leverages AI language models to generate, refine, and execute Python code on demand. This agentic tool helps you quickly prototype Python scripts with conversational AI assistance.


โœจ Features

๐ŸŽฏ Core Functionality

  • AI-Powered Code Generation: Uses HuggingFace's Qwen2.5-Coder-32B-Instruct model for high-quality Python code (configurable)
  • Interactive REPL: Easy-to-use command-line interface with helpful commands
  • Automatic Code Execution: Run generated Python scripts directly from the shell
  • Smart Code Extraction: Handles markdown-formatted responses and extracts clean Python code

๐Ÿ”„ Advanced Capabilities

  • Multi-Turn Refinement: Maintain conversation history to iteratively improve code
  • Interactive Mode ๐ŸŽฎ: Automatically detects and runs interactive programs (pygame games, user input, GUIs)
  • Syntax Check & Auto-Refine: Validates code with py_compile before execution; offers to auto-fix syntax errors via AI
  • API Retry with Backoff: Automatic retries with exponential backoff on network errors, rate limits, and server errors
  • Execution Timeout: Configurable timeout kills runaway scripts (Captured mode only)
  • Conversation History Limit: Automatically trims old messages to keep context manageable
  • Script Management: List and re-run previously generated scripts anytime
  • Dependency Detection: Automatically detects non-standard library imports
  • Auto-Installation: Prompts to install required packages via pip (or auto-installs with config)
  • Session Logging: All API calls and executions logged to timestamped files
  • Success Metrics: Track and display success rates and session statistics

๐ŸŽจ User Experience

  • Colored Output: Syntax-highlighted code display with colorized terminal output
  • File Management: Save generated code to files with /save command
  • History Tracking: View conversation history with /history
  • Session Stats: Monitor performance with /stats

๐Ÿš€ Quick Start

Prerequisites

  • Rust (1.70+): Install Rust
  • Python 3: For executing generated scripts
  • HuggingFace Token: Required for API access (free tier available)

Installation

  1. Clone the repository:
git clone https://github.com/Ali-Gatorre/Rust_project.git
cd Rust_project/project_code
  1. Set up HuggingFace Token: Create a .env file in the project_code directory:
echo "HF_TOKEN=your_huggingface_token_here" > .env

Get your token from HuggingFace Settings

  1. Build and run:
cargo build --release
cargo run

๐Ÿ“– Usage Guide

Interactive Commands

Command Description
/help Show all available commands
/quit or /exit Exit the program
/clear Clear conversation history
/refine Refine the last generated code
/save <filename> Save last code to a file
/history Show conversation history
/stats Display session statistics
/list List all previously generated scripts
/run <filename> Execute a previously generated script

Example Session

> Create a script that prints fibonacci numbers up to 100

----------- Generated Code -----------
# Fibonacci numbers up to 100
def fibonacci(limit):
    a, b = 0, 1
    while a < limit:
        print(a, end=' ')
        a, b = b, a + b
    print()

fibonacci(100)
-----------------------------------

Execute this script? (o/n) : o

--- Execution Result ---
STDOUT:
0 1 1 2 3 5 8 13 21 34 55 89

Multi-Turn Refinement

> Create a simple calculator
[Code generated...]

> /refine
What would you like to change or add? Add division by zero handling
[Improved code generated...]

Interactive Programs (NEW in v0.2!)

> Create a pygame game with a bouncing ball

โš ๏ธ  Detected non-standard dependencies: pygame
Install these dependencies? (o/n) : o
โœ“ Dependencies installed successfully

----------- Generated Code -----------
import pygame
# ... game code ...
-----------------------------------

Execute this script? (o/n) : o

๐ŸŽฎ Interactive mode detected (pygame/input/GUI)
   Running with inherited stdio for user interaction...

[Pygame window opens with bouncing ball animation]

See INTERACTIVE_MODE.md for detailed documentation on running games, programs with user input, and GUI applications.


๐Ÿ—๏ธ Architecture

Project Structure

project_code/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ main.rs          # Entry point; loads .env and config
โ”‚   โ”œโ”€โ”€ config.rs        # AppConfig with TOML deserialization
โ”‚   โ”œโ”€โ”€ api.rs           # HuggingFace API client with retry/backoff
โ”‚   โ”œโ”€โ”€ interface.rs     # Interactive REPL with syntax check and auto-refine
โ”‚   โ”œโ”€โ”€ python_exec.rs   # Python execution engine with timeout
โ”‚   โ”œโ”€โ”€ utils.rs         # Code extraction, import parsing, UTF-8 utils
โ”‚   โ””โ”€โ”€ logger.rs        # Logging and metrics
โ”œโ”€โ”€ generated/           # Generated Python scripts
โ”œโ”€โ”€ logs/                # Session logs
โ”œโ”€โ”€ Cargo.toml           # Rust dependencies
โ””โ”€โ”€ pymakebot.toml       # Optional configuration file

Technology Stack

  • Language: Rust 2021 Edition
  • AI Model: Qwen/Qwen2.5-Coder-32B-Instruct (HuggingFace) โ€” configurable
  • Key Dependencies:
    • reqwest: HTTP client for API calls
    • tokio: Async runtime
    • serde/serde_json: JSON serialization
    • toml/dirs: Configuration file support
    • wait-timeout: Execution timeout
    • colored: Terminal color output
    • regex: Code extraction (cached with LazyLock)
    • chrono: Timestamps
    • rand: Retry jitter

๐Ÿ”ง Configuration

Environment Variables

  • HF_TOKEN: Your HuggingFace API token (required, via .env file)

Configuration File (pymakebot.toml)

Create an optional pymakebot.toml in the project directory or your home directory. All fields are optional โ€” missing fields use defaults:

# AI model settings
model = "Qwen/Qwen2.5-Coder-32B-Instruct"
api_url = "https://router.huggingface.co/v1/chat/completions"
max_tokens = 16284
temperature = 0.2

# Execution settings
execution_timeout_secs = 30    # Kill scripts after this many seconds (0 = no timeout)
auto_install_deps = false      # Auto-install detected dependencies without prompting

# API resilience
max_retries = 3                # Retry on network errors, 429, and 5xx responses

# History management
max_history_messages = 20      # Trim oldest messages when history exceeds this

# File locations
log_dir = "logs"
generated_dir = "generated"

Load order: ./pymakebot.toml โ†’ ~/pymakebot.toml โ†’ built-in defaults


๐Ÿ“Š Logging and Metrics

Session Logs

All sessions are logged to logs/session_TIMESTAMP.log with:

  • API requests and responses
  • Execution results
  • Errors and warnings

Metrics Tracked

  • Total API requests
  • Successful vs failed executions
  • API errors
  • Success rate percentage

View anytime with /stats


๐Ÿ›ก๏ธ Security Considerations

โš ๏ธ Important: This tool executes AI-generated code automatically.

Best Practices:

  1. Review generated code before execution
  2. Use in a sandboxed environment
  3. Don't commit your .env file
  4. Monitor API usage to avoid unexpected costs
  5. Be cautious with file system operations in generated code

Safety Features:

  • Syntax check via py_compile before execution catches errors early
  • Execution timeout prevents runaway scripts
  • Dependency detection warns about non-standard imports before install

Limitations:

  • Requires HuggingFace Pro for heavy usage (free tier has rate limits)
  • Generated code quality depends on prompt clarity

๐Ÿค Contributing

Contributions are welcome! Areas for improvement:

  • Implement virtual environment isolation per script
  • Support for additional AI models (OpenAI, Anthropic, etc.)
  • Web UI using Tauri or similar
  • Support for other programming languages

๐Ÿ“š Documentation

Complete guides available:


๐Ÿ“ License

MIT License - see LICENSE file for details


๐Ÿ™ Acknowledgments

  • HuggingFace for providing the inference API
  • Qwen Team for the excellent code generation model
  • Rust community for amazing libraries

๐Ÿ“ž Contact


๐Ÿ”„ Version History

v0.2.2 (Current - February 2026)

  • ๐Ÿ”ง Configuration File: pymakebot.toml support with load chain (local โ†’ home โ†’ defaults)
  • ๐Ÿ” API Retry: Exponential backoff with jitter on network errors, 429, and 5xx
  • โฑ๏ธ Execution Timeout: Configurable timeout kills runaway scripts in Captured mode
  • โœ… Syntax Check: Pre-execution validation via py_compile with auto-refine on errors
  • ๐Ÿ“ History Limit: Automatic trimming of conversation history to configured max
  • ๐Ÿ› Bug Fixes: UTF-8 safe string slicing, correct success detection (exit_code == 0), cached regex compilation
  • ๐Ÿงน Code Quality: Zero clippy warnings, 66 tests (59 unit + 7 integration)

v0.2.1 (December 2025)

  • ๐ŸŽฎ Interactive Mode: Automatic detection for pygame, input(), tkinter, GUIs
  • ๐Ÿ“‚ Script Management: /list and /run commands for previously generated scripts
  • ๐ŸŽฏ Enhanced AI Prompts: Better code generation with self-contained scripts (no external files)
  • ๐Ÿ”ง Token Limit Increase: 4096 tokens for complete game implementations
  • ๐Ÿงน Smart Code Extraction: Multiple fallback strategies for markdown cleanup
  • ๐Ÿ“ Comprehensive Documentation: 7 new documentation files

v0.2.0 (December 2025)

  • Multi-turn conversation support with history
  • Dependency detection and auto-installation
  • Colored terminal output with syntax highlighting
  • Session logging and metrics tracking
  • Enhanced command system (/save, /history, /stats, /refine)

v0.1.0 (Initial)

  • Basic code generation via HuggingFace API
  • Simple Python script execution
  • Core REPL interface

Made with โค๏ธ by Alvaro Serero, Ali Dabale, Chloรฉ Daunas and Ovia Chanemouganandam

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages