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

Update app.py fourthly

Browse files
Files changed (1) hide show
  1. app.py +52 -7
app.py CHANGED
@@ -31,16 +31,16 @@ 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 = 4
35
- temperature: float = 0.7
36
 
37
 
38
  class FixRequest(BaseModel):
39
  code: str # code bị lỗi
40
  language: str | None = "Python"
41
  max_new_tokens: int = 128
42
- num_beams: int = 4
43
- temperature: float = 0.3 # thấp để sửa lỗi ổn định hơn
44
 
45
 
46
  class CompleteRequest(BaseModel):
@@ -79,6 +79,44 @@ def run_model(prompt: str,
79
  return text.strip()
80
 
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  # ==== ENDPOINT 1: TẠO CODE TỪ PROMPT ====
83
 
84
 
@@ -91,12 +129,13 @@ 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(
@@ -106,6 +145,8 @@ Task:
106
  temperature=req.temperature,
107
  )
108
 
 
 
109
  return CodeResponse(output=output)
110
 
111
 
@@ -120,6 +161,7 @@ def fix_code(req: FixRequest):
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}
@@ -134,6 +176,8 @@ Corrected {lang} code:
134
  temperature=req.temperature,
135
  )
136
 
 
 
137
  return CodeResponse(output=output)
138
 
139
 
@@ -175,7 +219,8 @@ Missing {lang} code:
175
  temperature=req.temperature,
176
  )
177
 
178
- return CodeResponse(output=output)
 
179
 
180
 
181
  # ==== HEALTHCHECK ====
 
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
 
38
  class FixRequest(BaseModel):
39
  code: str # code bị lỗi
40
  language: str | None = "Python"
41
  max_new_tokens: int = 128
42
+ num_beams: int = 1
43
+ temperature: float = 0.2 # thấp để sửa lỗi ổn định hơn
44
 
45
 
46
  class CompleteRequest(BaseModel):
 
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
 
 
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(
 
145
  temperature=req.temperature,
146
  )
147
 
148
+ output = clean_code(output, lang)
149
+
150
  return CodeResponse(output=output)
151
 
152
 
 
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}
 
176
  temperature=req.temperature,
177
  )
178
 
179
+ output = clean_code(output, lang)
180
+
181
  return CodeResponse(output=output)
182
 
183
 
 
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 ====