File size: 4,881 Bytes
a2c3f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# 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

1. **Clone/Navigate to the Project Directory**
   ```bash
   cd ~/model/java_optimizer
   ```

2. **Activate the Virtual Environment**
   ```bash
   source ~/Python/.venv/bin/activate
   ```

3. **Install Required Dependencies**
   ```bash
   pip install -r requirements.txt
   ```

4. **Verify Model Files Exist**
   The application expects the fine-tuned model to be in `/home/ssp/model/codet5-fast/`.
   You should see files like:
   - `config.json`
   - `model.safetensors`
   - `tokenizer_config.json`
   - `vocab.json`
   - `merges.txt`

5. **Run the Application**
   ```bash
   python app.py
   ```

6. **Access the Web Interface**
   Open your web browser and navigate to:
   ```
   http://localhost:5000
   ```

## Usage

1. **Enter Java Code**: Type or paste your Java code into the input textarea
2. **Click Optimize**: Press the "⚑ Optimize Code" button
3. **View Results**: The optimized code will appear in the output textarea
4. **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**: 
  ```json
  {
    "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

1. **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

2. **CUDA/GPU Issues**
   - The application will automatically fall back to CPU if GPU is unavailable
   - To force CPU usage, modify the `DEVICE` variable in `app.py`

3. **Port Already in Use**
   - Change the port in `app.py` (line with `app.run()`)
   - Or stop the existing process using port 5000

4. **Dependencies Missing**
   - Run `pip install torch transformers flask` manually
   - Check Python version compatibility

### 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
1. Modify `app.py` for backend logic changes
2. Update `templates/index.html` for UI changes
3. Adjust model parameters in `app.py` if needed
4. 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