File size: 2,927 Bytes
e389ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
from flask import Flask, request, jsonify
from flask_cors import CORS
from transformers import pipeline

# --- 1. Initialize Flask App ---
app = Flask(__name__)
CORS(app)

# --- 2. Load Your Models ---
print("--- Server is starting ---")
print(f"Python version: {sys.version}")

optimizer_pipeline = None

# --- Hugging Face Model Loading ---
try:
    # This is the specialist model for Python code generation
    model_name = 'model_name = Salesforce/codegen-2B-mono'
    
    print(f"Loading {model_name} model...")
    
    # Note: We use 'text-generation' for this model type
    hf_task = 'text-generation' 
    
    optimizer_pipeline = pipeline(hf_task, model=model_name)
    
    print("Hugging Face model loaded successfully.")
    print("--- Server is ready. ---")
    
except Exception as e_hf:
    print(f"--- CRITICAL ERROR loading Hugging Face model: {e_hf} ---")
    print("--- Server will not work. ---")


# --- 3. Create an API Endpoint ---
@app.route('/optimize', methods=['POST'])
def optimize_code():
    print("\n--- Received a request ---")
    
    if not request.json or 'code' not in request.json:
        print("Request error: No 'code' key in JSON.")
        return jsonify({'error': 'No code provided'}), 400

    # This 'code_snippet' contains your full "Expert" prompt from VS Code
    code_snippet = request.json['code']
    print(f"Received prompt length: {len(code_snippet)} chars") 

    response_data = {}

    try:
        # --- Use Optimizer (Hugging Face) ---
        if optimizer_pipeline:
            print("Running Hugging Face model...")
            
            # Run the model. 
            # max_new_tokens=200 gives it enough space to write the optimized code
            opt_result = optimizer_pipeline(code_snippet, max_new_tokens=200)
            
            # Get the full generated text
            full_response = opt_result[0]['generated_text']
            
            # Clean up: We only want the part AFTER "Optimized Code:"
            if "Optimized Code:" in full_response:
                clean_optimization = full_response.split("Optimized Code:")[-1].strip()
            else:
                clean_optimization = full_response

            response_data['optimization'] = clean_optimization
            print("Optimization generated successfully.")
            
        else:
            print("Optimizer model is not loaded.")
            response_data['optimization'] = "Error: Optimizer model is not loaded on server."
        
        return jsonify(response_data)

    except Exception as e:
        print(f"--- ERROR during prediction: {e} ---")
        return jsonify({'error': f'Prediction failed: {e}'}), 500

# --- 4. Run the Server ---
if __name__ == '__main__':
    print("Starting Flask server on http://127.0.0.1:5000")
    app.run(debug=True, port=5000)