KazeStudy commited on
Commit
3587c1f
·
1 Parent(s): d60814d

Update app.py fithly

Browse files
Files changed (1) hide show
  1. app.py +32 -98
app.py CHANGED
@@ -28,10 +28,10 @@ model.eval()
28
 
29
 
30
  class GenerateRequest(BaseModel):
31
- prompt: str # mô tả cần sinh code
32
  language: str | None = "Python"
33
  max_new_tokens: int = 128
34
- num_beams: int = 1 # ít beam hơn cho ổn định
35
  temperature: float = 0.3 # giảm randomness
36
 
37
 
@@ -45,11 +45,11 @@ class FixRequest(BaseModel):
45
 
46
  class CompleteRequest(BaseModel):
47
  prefix: str # code phía trước con trỏ
48
- suffix: str = "" # code phía sau con trỏ (nếu )
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):
@@ -72,71 +72,27 @@ def run_model(prompt: str,
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
- def clean_code(raw: str, lang: str) -> str:
83
- """
84
- Dọn mấy dòng rác đầu output (vd: ':', 'program:', ...) cho ra code “sạch” hơn.
85
- Không đụng gì phần giữa & cuối.
86
- """
87
- lines = raw.splitlines()
88
- if not lines:
89
- return raw.strip()
90
-
91
- lang_low = (lang or "").lower()
92
-
93
- def looks_like_code(s: str) -> bool:
94
- s = s.strip()
95
- if not s:
96
- return False
97
-
98
- if lang_low == "python":
99
- # thường bắt đầu bằng import/def/class/# comment
100
- prefixes = ("def ", "class ", "import ", "from ", "#", "@")
101
- return s.startswith(prefixes)
102
- elif lang_low in ("c", "c++", "cpp"):
103
- prefixes = ("#include", "int ", "void ", "char ", "float ",
104
- "double ", "struct ", "typedef ")
105
- return s.startswith(prefixes)
106
- else:
107
- # fallback cho ngôn ngữ khác
108
- return any(ch in s for ch in (";", "{", "}", "=", "function ", "public ", "private "))
109
-
110
- start = 0
111
- for i, line in enumerate(lines):
112
- if looks_like_code(line):
113
- start = i
114
- break
115
-
116
- cleaned = "\n".join(lines[start:]).strip()
117
- return cleaned if cleaned else raw.strip()
118
-
119
-
120
  # ==== ENDPOINT 1: TẠO CODE TỪ PROMPT ====
121
 
122
 
123
  @app.post("/generate-code", response_model=CodeResponse)
124
  def generate_code(req: GenerateRequest):
 
 
 
 
125
  lang = req.language or "Python"
126
 
127
- prompt = f"""
128
- You are a helpful coding assistant.
129
-
130
- Generate ONLY valid {lang} source code for the task below.
131
- Do NOT add any explanations, comments in natural language, or markdown.
132
- Do NOT repeat the task description.
133
- Return only raw {lang} code that can be run.
134
-
135
- Task:
136
- {req.prompt}
137
-
138
- Begin {lang} code now:
139
- """.strip()
140
 
141
  output = run_model(
142
  prompt,
@@ -145,8 +101,6 @@ Begin {lang} code now:
145
  temperature=req.temperature,
146
  )
147
 
148
- output = clean_code(output, lang)
149
-
150
  return CodeResponse(output=output)
151
 
152
 
@@ -155,19 +109,17 @@ Begin {lang} code now:
155
 
156
  @app.post("/fix-code", response_model=CodeResponse)
157
  def fix_code(req: FixRequest):
 
 
 
158
  lang = req.language or "Python"
159
 
160
- prompt = f"""
161
- The following {lang} code contains bugs.
162
- Fix all bugs and return ONLY the corrected {lang} code.
163
- Do NOT add any explanations or comments in natural language.
164
- Do NOT change the language or rewrite the task.
165
-
166
- Buggy {lang} code:
167
- {req.code}
168
-
169
- Corrected {lang} code:
170
- """.strip()
171
 
172
  output = run_model(
173
  prompt,
@@ -176,41 +128,24 @@ Corrected {lang} code:
176
  temperature=req.temperature,
177
  )
178
 
179
- output = clean_code(output, lang)
180
-
181
  return CodeResponse(output=output)
182
 
183
 
184
- # ==== ENDPOINT 3: GỢI Ý CODE KIỂU CURSOR (COMPLETION) ====
185
 
186
 
187
  @app.post("/complete-code", response_model=CodeResponse)
188
  def complete_code(req: CompleteRequest):
 
 
 
 
 
189
  lang = req.language or "Python"
190
 
191
- prompt = f"""
192
- You are an AI code completion engine like Cursor or GitHub Copilot.
193
-
194
- You will be given the prefix and suffix of a {lang} file.
195
- Your task is to generate ONLY the missing {lang} code between them.
196
-
197
- Rules:
198
- - DO NOT repeat the prefix.
199
- - DO NOT repeat the suffix.
200
- - DO NOT add any explanations, natural language text, or markdown.
201
- - DO NOT add imports/includes if they already appear in the prefix.
202
- - Return ONLY raw {lang} code that can be directly inserted at the cursor.
203
-
204
- Prefix:
205
- {req.prefix}
206
-
207
- <CURSOR HERE>
208
-
209
- Suffix:
210
- {req.suffix}
211
-
212
- Missing {lang} code:
213
- """.strip()
214
 
215
  output = run_model(
216
  prompt,
@@ -219,8 +154,7 @@ Missing {lang} code:
219
  temperature=req.temperature,
220
  )
221
 
222
- # completion thường là snippet ngắn, không clean để tránh cắt nhầm
223
- return CodeResponse(output=output.strip())
224
 
225
 
226
  # ==== HEALTHCHECK ====
 
28
 
29
 
30
  class GenerateRequest(BaseModel):
31
+ prompt: str # mô tả cần sinh code (nên gửi tiếng Anh)
32
  language: str | None = "Python"
33
  max_new_tokens: int = 128
34
+ num_beams: int = 1 # ít beam cho ổn định
35
  temperature: float = 0.3 # giảm randomness
36
 
37
 
 
45
 
46
  class CompleteRequest(BaseModel):
47
  prefix: str # code phía trước con trỏ
48
+ suffix: str = "" # code phía sau con trỏ (chưa dùng nhiều, vì Codet5 không phải infill)
49
  language: str | None = "Python"
50
  max_new_tokens: int = 64 # completion thường ngắn
51
+ num_beams: int = 1
52
+ temperature: float = 0.3
53
 
54
 
55
  class CodeResponse(BaseModel):
 
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
+ """
88
+ Sinh code từ mô tả.
89
+ Lưu ý: Codet5+ “thích” prompt ngắn, dạng pattern.
90
+ """
91
  lang = req.language or "Python"
92
 
93
+ # Prompt cực ngắn, đúng style CodeT5 (tránh essay dài)
94
+ # dụ: "Python code:\n# Task: Create a function that prints numbers from 1 to 10.\n"
95
+ prompt = f"{lang} code:\n# Task: {req.prompt}\n"
 
 
 
 
 
 
 
 
 
 
96
 
97
  output = run_model(
98
  prompt,
 
101
  temperature=req.temperature,
102
  )
103
 
 
 
104
  return CodeResponse(output=output)
105
 
106
 
 
109
 
110
  @app.post("/fix-code", response_model=CodeResponse)
111
  def fix_code(req: FixRequest):
112
+ """
113
+ Sửa lỗi code: input là code sai, output là code đúng.
114
+ """
115
  lang = req.language or "Python"
116
 
117
+ # Cũng giữ prompt thật đơn giản
118
+ prompt = (
119
+ f"Fix the following {lang} code:\n"
120
+ f"{req.code}\n\n"
121
+ f"Fixed {lang} code:\n"
122
+ )
 
 
 
 
 
123
 
124
  output = run_model(
125
  prompt,
 
128
  temperature=req.temperature,
129
  )
130
 
 
 
131
  return CodeResponse(output=output)
132
 
133
 
134
+ # ==== ENDPOINT 3: GỢI Ý CODE (KIỂU CURSOR – DÙ CHỈ DÙNG PREFIX) ====
135
 
136
 
137
  @app.post("/complete-code", response_model=CodeResponse)
138
  def complete_code(req: CompleteRequest):
139
+ """
140
+ Gợi ý code tiếp theo dựa trên prefix.
141
+ Lưu ý: Codet5p-770m không phải model infill thực sự,
142
+ nên suffix ít tác dụng. Ở đây ta dùng chủ yếu prefix.
143
+ """
144
  lang = req.language or "Python"
145
 
146
+ # Dùng prefix làm context, để model tiếp tục code.
147
+ # Suffix thể dùng để hiển thị phía client, còn model chủ yếu nhìn prefix.
148
+ prompt = f"{lang} code:\n{req.prefix}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
  output = run_model(
151
  prompt,
 
154
  temperature=req.temperature,
155
  )
156
 
157
+ return CodeResponse(output=output)
 
158
 
159
 
160
  # ==== HEALTHCHECK ====