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

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +7 -13
server.py CHANGED
@@ -1,8 +1,7 @@
1
  import sys
2
  from flask import Flask, request, jsonify
3
  from flask_cors import CORS
4
- # We need AutoTokenizer for the "stop" button
5
- from transformers import pipeline, AutoTokenizer
6
 
7
  # --- 1. Initialize Flask App ---
8
  app = Flask(__name__)
@@ -22,11 +21,8 @@ try:
22
  print(f"Loading {model_name} model...")
23
  hf_task = 'text-generation'
24
 
25
- # --- THIS IS THE "STOP" TOKEN FIX ---
26
- # 1. We load the tokenizer so we know its "stop" token
27
  tokenizer = AutoTokenizer.from_pretrained(model_name)
28
-
29
- # 2. We pass the model AND the tokenizer to the pipeline
30
  optimizer_pipeline = pipeline(hf_task, model=model_name, tokenizer=tokenizer)
31
  # --- END FIX ---
32
 
@@ -57,20 +53,18 @@ def optimize_code():
57
  if optimizer_pipeline:
58
  print("Running Hugging Face model...")
59
 
60
- # --- THIS IS THE TYPO FIX ---
61
- # It is 'opt_result', not 'opt_.result'
62
  opt_result = optimizer_pipeline(
63
  code_snippet,
64
  max_new_tokens=200,
65
- eos_token_id=tokenizer.eos_token_id # <-- THIS TELLS IT TO STOP
66
  )
67
- # --- END TYPO FIX ---
68
 
69
  full_response = opt_result[0]['generated_text']
70
 
71
- # Clean up the Llama chat response
72
- if "<|assistant|>" in full_response:
73
- clean_optimization = full_response.split("<|assistant|>")[-1].strip()
74
  else:
75
  clean_optimization = full_response
76
 
 
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
  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
  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