athv commited on
Commit
e1089e2
·
verified ·
1 Parent(s): 23b1e25

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +14 -6
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, AutoTokenizer # <-- MUST HAVE THIS
5
 
6
  # --- 1. Initialize Flask App ---
7
  app = Flask(__name__)
@@ -21,8 +21,11 @@ try:
21
  print(f"Loading {model_name} model...")
22
  hf_task = 'text-generation'
23
 
24
- # --- MUST HAVE THIS TOKENIZER FIX ---
 
25
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
 
26
  optimizer_pipeline = pipeline(hf_task, model=model_name, tokenizer=tokenizer)
27
  # --- END FIX ---
28
 
@@ -53,18 +56,23 @@ def optimize_code():
53
  if optimizer_pipeline:
54
  print("Running Hugging Face model...")
55
 
56
- # Run the model with the "stop" token
 
57
  opt_result = optimizer_pipeline(
58
  code_snippet,
59
  max_new_tokens=200,
60
- eos_token_id=tokenizer.eos_token_id # <-- MUST HAVE THIS
61
  )
 
62
 
63
  full_response = opt_result[0]['generated_text']
64
 
65
  # Clean up: We only want the part AFTER "Optimized Code:"
66
- if "Optimized Code:" in full_response:
67
- clean_optimization = full_response.split("Optimized Code:")[-1].strip()
 
 
 
68
  else:
69
  clean_optimization = full_response
70
 
 
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__)
 
21
  print(f"Loading {model_name} model...")
22
  hf_task = 'text-generation'
23
 
24
+ # --- THIS IS THE "STOP" TOKEN 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
 
 
56
  if optimizer_pipeline:
57
  print("Running Hugging Face model...")
58
 
59
+ # --- THIS IS THE TYPO FIX ---
60
+ # It is 'opt_result', not 'opt_.result'
61
  opt_result = optimizer_pipeline(
62
  code_snippet,
63
  max_new_tokens=200,
64
+ eos_token_id=tokenizer.eos_token_id # <-- THIS TELLS IT TO STOP
65
  )
66
+ # --- END TYPO FIX ---
67
 
68
  full_response = opt_result[0]['generated_text']
69
 
70
  # Clean up: We only want the part AFTER "Optimized Code:"
71
+ # We must clean the *original* prompt, not the *new* one.
72
+ if "<|assistant|>" in full_response:
73
+ clean_optimization = full_response.split("<|assistant|>")[-1].strip()
74
+ elif "Optimized Code:" in full_response:
75
+ clean_optimization = full_response.split("Optimized Code:")[-1].strip()
76
  else:
77
  clean_optimization = full_response
78