KazeStudy commited on
Commit
e9cc738
·
1 Parent(s): c46817f

Update app.py thirdly

Browse files
Files changed (1) hide show
  1. app.py +56 -24
app.py CHANGED
@@ -26,6 +26,7 @@ model.eval()
26
 
27
  # ==== REQUEST / RESPONSE MODELS ====
28
 
 
29
  class GenerateRequest(BaseModel):
30
  prompt: str # mô tả cần sinh code
31
  language: str | None = "Python"
@@ -47,8 +48,8 @@ class CompleteRequest(BaseModel):
47
  suffix: str = "" # code phía sau con trỏ (nếu có)
48
  language: str | None = "Python"
49
  max_new_tokens: int = 64 # completion thường ngắn
50
- num_beams: int = 4
51
- temperature: float = 0.7
52
 
53
 
54
  class CodeResponse(BaseModel):
@@ -57,6 +58,7 @@ class CodeResponse(BaseModel):
57
 
58
  # ==== TIỆN ÍCH DÙNG CHUNG ====
59
 
 
60
  def run_model(prompt: str,
61
  max_new_tokens: int,
62
  num_beams: int,
@@ -70,24 +72,32 @@ def run_model(prompt: str,
70
  num_beams=num_beams,
71
  temperature=temperature,
72
  early_stopping=True,
 
73
  )
74
 
75
  text = tokenizer.decode(outputs[0], skip_special_tokens=True)
76
- return text
77
 
78
 
79
  # ==== ENDPOINT 1: TẠO CODE TỪ PROMPT ====
80
 
 
81
  @app.post("/generate-code", response_model=CodeResponse)
82
  def generate_code(req: GenerateRequest):
83
  lang = req.language or "Python"
84
 
85
- prompt = (
86
- f"Generate {lang} code ONLY.\n"
87
- f"Do NOT use any other programming language.\n\n"
88
- f"Task:\n{req.prompt}\n\n"
89
- f"{lang} code:\n"
90
- )
 
 
 
 
 
 
91
 
92
  output = run_model(
93
  prompt,
@@ -101,16 +111,21 @@ def generate_code(req: GenerateRequest):
101
 
102
  # ==== ENDPOINT 2: SỬA LỖI CODE ====
103
 
 
104
  @app.post("/fix-code", response_model=CodeResponse)
105
  def fix_code(req: FixRequest):
106
  lang = req.language or "Python"
107
 
108
- prompt = (
109
- f"The following {lang} code contains bugs.\n"
110
- f"Fix all bugs and return ONLY the corrected {lang} code.\n\n"
111
- f"Buggy {lang} code:\n{req.code}\n\n"
112
- f"Corrected {lang} code:\n"
113
- )
 
 
 
 
114
 
115
  output = run_model(
116
  prompt,
@@ -122,20 +137,36 @@ def fix_code(req: FixRequest):
122
  return CodeResponse(output=output)
123
 
124
 
125
- # ==== ENDPOINT 3: GỢI Ý CODE (COMPLETION) ====
 
126
 
127
  @app.post("/complete-code", response_model=CodeResponse)
128
  def complete_code(req: CompleteRequest):
129
  lang = req.language or "Python"
130
 
131
- # prefix + suffix giống kiểu Copilot completion
132
- prompt = (
133
- f"Complete the following {lang} code.\n"
134
- f"Only generate the missing code between the prefix and suffix.\n\n"
135
- f"Prefix:\n{req.prefix}\n\n"
136
- f"Suffix:\n{req.suffix}\n\n"
137
- f"Missing {lang} code:\n"
138
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
  output = run_model(
141
  prompt,
@@ -149,6 +180,7 @@ def complete_code(req: CompleteRequest):
149
 
150
  # ==== HEALTHCHECK ====
151
 
 
152
  @app.get("/")
153
  def root():
154
  return {"status": "CodeT5+ backend is running 🚀"}
 
26
 
27
  # ==== REQUEST / RESPONSE MODELS ====
28
 
29
+
30
  class GenerateRequest(BaseModel):
31
  prompt: str # mô tả cần sinh code
32
  language: str | None = "Python"
 
48
  suffix: str = "" # code phía sau con trỏ (nếu có)
49
  language: str | None = "Python"
50
  max_new_tokens: int = 64 # completion thường ngắn
51
+ num_beams: int = 1 # completion kiểu Cursor thường để 1 beam
52
+ temperature: float = 0.3 # ổn định hơn
53
 
54
 
55
  class CodeResponse(BaseModel):
 
58
 
59
  # ==== TIỆN ÍCH DÙNG CHUNG ====
60
 
61
+
62
  def run_model(prompt: str,
63
  max_new_tokens: int,
64
  num_beams: int,
 
72
  num_beams=num_beams,
73
  temperature=temperature,
74
  early_stopping=True,
75
+ repetition_penalty=1.05, # nhẹ để giảm lặp
76
  )
77
 
78
  text = tokenizer.decode(outputs[0], skip_special_tokens=True)
79
+ return text.strip()
80
 
81
 
82
  # ==== ENDPOINT 1: TẠO CODE TỪ PROMPT ====
83
 
84
+
85
  @app.post("/generate-code", response_model=CodeResponse)
86
  def generate_code(req: GenerateRequest):
87
  lang = req.language or "Python"
88
 
89
+ prompt = f"""
90
+ You are a helpful coding assistant.
91
+
92
+ Generate ONLY valid {lang} source code for the task below.
93
+ Do NOT add any explanations, comments in natural language, or markdown.
94
+ Return only raw {lang} code.
95
+
96
+ Task:
97
+ {req.prompt}
98
+
99
+ {lang} code:
100
+ """.strip()
101
 
102
  output = run_model(
103
  prompt,
 
111
 
112
  # ==== ENDPOINT 2: SỬA LỖI CODE ====
113
 
114
+
115
  @app.post("/fix-code", response_model=CodeResponse)
116
  def fix_code(req: FixRequest):
117
  lang = req.language or "Python"
118
 
119
+ prompt = f"""
120
+ The following {lang} code contains bugs.
121
+ Fix all bugs and return ONLY the corrected {lang} code.
122
+ Do NOT add any explanations or comments in natural language.
123
+
124
+ Buggy {lang} code:
125
+ {req.code}
126
+
127
+ Corrected {lang} code:
128
+ """.strip()
129
 
130
  output = run_model(
131
  prompt,
 
137
  return CodeResponse(output=output)
138
 
139
 
140
+ # ==== ENDPOINT 3: GỢI Ý CODE KIỂU CURSOR (COMPLETION) ====
141
+
142
 
143
  @app.post("/complete-code", response_model=CodeResponse)
144
  def complete_code(req: CompleteRequest):
145
  lang = req.language or "Python"
146
 
147
+ prompt = f"""
148
+ You are an AI code completion engine like Cursor or GitHub Copilot.
149
+
150
+ You will be given the prefix and suffix of a {lang} file.
151
+ Your task is to generate ONLY the missing {lang} code between them.
152
+
153
+ Rules:
154
+ - DO NOT repeat the prefix.
155
+ - DO NOT repeat the suffix.
156
+ - DO NOT add any explanations, natural language text, or markdown.
157
+ - DO NOT add imports/includes if they already appear in the prefix.
158
+ - Return ONLY raw {lang} code that can be directly inserted at the cursor.
159
+
160
+ Prefix:
161
+ {req.prefix}
162
+
163
+ <CURSOR HERE>
164
+
165
+ Suffix:
166
+ {req.suffix}
167
+
168
+ Missing {lang} code:
169
+ """.strip()
170
 
171
  output = run_model(
172
  prompt,
 
180
 
181
  # ==== HEALTHCHECK ====
182
 
183
+
184
  @app.get("/")
185
  def root():
186
  return {"status": "CodeT5+ backend is running 🚀"}