Java Code Optimizer
A web-based interface for optimizing Java code using a fine-tuned CodeT5-small model.
Overview
This application provides a user-friendly web interface to optimize Java code using a fine-tuned Salesforce/codet5-small model. The model has been trained on Java optimization tasks and can transform verbose or inefficient Java code into more optimal versions.
Features
- π Real-time Optimization: Get instant optimization suggestions for your Java code
- π― Specialized Model: Fine-tuned specifically for Java code optimization tasks
- π» GPU/CPU Support: Automatically utilizes GPU if available, falls back to CPU
- π¨ Modern Interface: Clean, responsive design with dark/light mode support
- π Example Cases: Pre-loaded examples to demonstrate optimization capabilities
- π Health Monitoring: Real-time status of model loading and device utilization
Model Information
- Base Model: Salesforce/codet5-small
- Training Data: ~6K training / 680 validation Java optimization pairs
- Framework: HuggingFace Transformers with Seq2SeqTrainer
- Optimization Focus: Java code refactoring and performance improvements
Installation & Setup
Prerequisites
- Python 3.8+
- Git
- Internet connection (for initial model loading)
Step-by-Step Instructions
Clone/Navigate to the Project Directory
cd ~/model/java_optimizerActivate the Virtual Environment
source ~/Python/.venv/bin/activateInstall Required Dependencies
pip install -r requirements.txtVerify Model Files Exist The application expects the fine-tuned model to be in
/home/ssp/model/codet5-fast/. You should see files like:config.jsonmodel.safetensorstokenizer_config.jsonvocab.jsonmerges.txt
Run the Application
python app.pyAccess the Web Interface Open your web browser and navigate to:
http://localhost:5000
Usage
- Enter Java Code: Type or paste your Java code into the input textarea
- Click Optimize: Press the "β‘ Optimize Code" button
- View Results: The optimized code will appear in the output textarea
- Try Examples: Click on any of the pre-loaded examples to test the optimizer
Example Optimizations
The model has been trained to recognize and optimize common Java patterns:
- Switch Expressions: Converting verbose switch statements to switch expressions
- Collection Operations: Replacing manual iterator removal with
removeIf() - String Handling: Optimizing string concatenation with
StringBuilder - And more...
API Endpoints
GET /
- Returns the main HTML interface
POST /optimize
- Request Body:
{ "code": "your Java code here" } - Response:
{ "original": "input Java code", "optimized": "optimized Java code" } - Error Response:
{ "error": "error message" }
GET /health
- Returns application health status including model loading state and device info
GET /model-info
- Returns information about the model files and size
Troubleshooting
Common Issues
Model Not Loading
- Verify the model directory
/home/ssp/model/codet5-fast/exists - Check that all required files are present
- Ensure you have sufficient disk space and memory
- Verify the model directory
CUDA/GPU Issues
- The application will automatically fall back to CPU if GPU is unavailable
- To force CPU usage, modify the
DEVICEvariable inapp.py
Port Already in Use
- Change the port in
app.py(line withapp.run()) - Or stop the existing process using port 5000
- Change the port in
Dependencies Missing
- Run
pip install torch transformers flaskmanually - Check Python version compatibility
- Run
Performance Notes
- First optimization may be slower due to model loading
- Subsequent requests are faster as model stays in memory
- GPU acceleration significantly improves inference speed
- Model size is approximately 240MB
Development
File Structure
java_optimizerΕ
βββ app.py # Main Flask application
βββ requirements.txt # Python dependencies
βββ test_model.py # Model testing script
βββ templates/
βββ index.html # Frontend interface
Making Changes
- Modify
app.pyfor backend logic changes - Update
templates/index.htmlfor UI changes - Adjust model parameters in
app.pyif needed - Restart the application after changes
License
This project is provided for educational and demonstration purposes.
Acknowledgements
- Model based on Salesforce/codet5-small
- Built with Flask and HuggingFace Transformers
- Inspired by Java optimization best practices# DeepLearning