athv commited on
Commit
0582940
·
verified ·
1 Parent(s): 73ff042

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +15 -16
server.py CHANGED
@@ -1,7 +1,7 @@
1
  import sys
2
  from flask import Flask, request, jsonify
3
  from flask_cors import CORS
4
- from transformers import pipeline
5
 
6
  # --- 1. Initialize Flask App ---
7
  app = Flask(__name__)
@@ -15,16 +15,19 @@ optimizer_pipeline = None
15
 
16
  # --- Hugging Face Model Loading ---
17
  try:
18
- # Use the "large" instruction-tuned model
19
  model_name = 'TinyLlama/TinyLlama-1.1B-Chat-v1.0'
20
-
21
 
22
  print(f"Loading {model_name} model...")
 
23
 
24
- # Use 'text2text-generation' for T5 models
25
- hf_task = 'text-generation' # <-- IMPORTANT: Change task
 
26
 
27
- optimizer_pipeline = pipeline(hf_task, model=model_name)
 
 
28
 
29
  print("Hugging Face model loaded successfully.")
30
  print("--- Server is ready. ---")
@@ -35,11 +38,6 @@ except Exception as e_hf:
35
 
36
 
37
  # --- 3. Create an API Endpoint ---
38
- @app.route('/')
39
- def home():
40
- # A simple route to check if the server is alive
41
- return "Code Optimizer API is running."
42
-
43
  @app.route('/optimize', methods=['POST'])
44
  def optimize_code():
45
  print("\n--- Received a request ---")
@@ -48,7 +46,6 @@ def optimize_code():
48
  print("Request error: No 'code' key in JSON.")
49
  return jsonify({'error': 'No code provided'}), 400
50
 
51
- # This 'code_snippet' contains your full "Expert" prompt from VS Code
52
  code_snippet = request.json['code']
53
  print(f"Received prompt length: {len(code_snippet)} chars")
54
 
@@ -60,10 +57,12 @@ def optimize_code():
60
  print("Running Hugging Face model...")
61
 
62
  # Run the model.
63
- # max_new_tokens=200 gives it enough space to write the optimized code
64
- opt_result = optimizer_pipeline(code_snippet, max_new_tokens=200)
 
 
 
65
 
66
- # Get the full generated text
67
  full_response = opt_result[0]['generated_text']
68
 
69
  # Clean up: We only want the part AFTER "Optimized Code:"
@@ -87,5 +86,5 @@ def optimize_code():
87
 
88
  # --- 4. Run the Server ---
89
  if __name__ == '__main__':
90
- # Listen on 0.0.0.0 and port 7860 for Hugging Face Spaces
91
  app.run(host='0.0.0.0', port=7860)
 
1
  import sys
2
  from flask import Flask, request, jsonify
3
  from flask_cors import CORS
4
+ from transformers import pipeline, AutoTokenizer # <-- ADDED AutoTokenizer
5
 
6
  # --- 1. Initialize Flask App ---
7
  app = Flask(__name__)
 
15
 
16
  # --- Hugging Face Model Loading ---
17
  try:
18
+ # This is our smart chat model
19
  model_name = 'TinyLlama/TinyLlama-1.1B-Chat-v1.0'
 
20
 
21
  print(f"Loading {model_name} model...")
22
+ hf_task = 'text-generation'
23
 
24
+ # --- THIS IS THE FIX ---
25
+ # 1. We load the tokenizer so we know its "stop" token
26
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
27
 
28
+ # 2. We pass the model AND the tokenizer to the pipeline
29
+ optimizer_pipeline = pipeline(hf_task, model=model_name, tokenizer=tokenizer)
30
+ # --- END FIX ---
31
 
32
  print("Hugging Face model loaded successfully.")
33
  print("--- Server is ready. ---")
 
38
 
39
 
40
  # --- 3. Create an API Endpoint ---
 
 
 
 
 
41
  @app.route('/optimize', methods=['POST'])
42
  def optimize_code():
43
  print("\n--- Received a request ---")
 
46
  print("Request error: No 'code' key in JSON.")
47
  return jsonify({'error': 'No code provided'}), 400
48
 
 
49
  code_snippet = request.json['code']
50
  print(f"Received prompt length: {len(code_snippet)} chars")
51
 
 
57
  print("Running Hugging Face model...")
58
 
59
  # Run the model.
60
+ opt_.result = optimizer_pipeline(
61
+ code_snippet,
62
+ max_new_tokens=200,
63
+ eos_token_id=tokenizer.eos_token_id # <-- THIS TELLS IT TO STOP
64
+ )
65
 
 
66
  full_response = opt_result[0]['generated_text']
67
 
68
  # Clean up: We only want the part AFTER "Optimized Code:"
 
86
 
87
  # --- 4. Run the Server ---
88
  if __name__ == '__main__':
89
+ # Listen on 0.0.0.0 to allow external connections
90
  app.run(host='0.0.0.0', port=7860)