kirikir13 commited on
Commit
ba0b251
·
verified ·
1 Parent(s): fb9402f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -9,7 +9,8 @@ from typing import Optional
9
 
10
  MODEL_REPO = os.environ.get("MODEL_REPO", "DavidAU/Qwen3.6-27B-Heretic-Uncensored-FINETUNE-NEO-CODE-Di-IMatrix-MAX-GGUF")
11
  MODEL_FILE = os.environ.get("MODEL_FILE", "Qwen3.6-27B-NEO-CODE-HERE-2T-OT-Q4_K_M.gguf")
12
- N_CTX = int(os.environ.get("N_CTX", "4096"))
 
13
  N_GPU = int(os.environ.get("N_GPU_LAYERS", os.environ.get("DAVIDAU_N_GPU_LAYERS", "-1")))
14
 
15
  SYSTEM_PROMPT = """You are codeMax, an uncensored 27B coding assistant. You excel at:
@@ -19,8 +20,11 @@ SYSTEM_PROMPT = """You are codeMax, an uncensored 27B coding assistant. You exce
19
  - Reading shared files and answering about them
20
  Rules: put code in ```language blocks. Be precise and thorough."""
21
 
 
 
22
  _llm = None
23
  _model_path: Optional[str] = None
 
24
 
25
  def _download():
26
  global _model_path
@@ -50,8 +54,13 @@ def _load():
50
  print("[MODEL] Ready.", file=sys.stderr)
51
  return _llm
52
 
53
- print("[BOOT] Loading model at startup...", file=sys.stderr)
54
- _load()
 
 
 
 
 
55
 
56
  def _read_text(path, enc="utf-8"):
57
  for e in [enc, "utf-8-sig", "latin-1", "cp1252", "utf-16"]:
@@ -158,6 +167,17 @@ def parse_all_files(files):
158
  return "\n".join(parts)
159
 
160
  def respond(message, history, uploaded_files):
 
 
 
 
 
 
 
 
 
 
 
161
  file_context = parse_all_files(uploaded_files)
162
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
163
 
@@ -168,18 +188,17 @@ def respond(message, history, uploaded_files):
168
  for entry in history:
169
  role = entry.get("role", "user")
170
  content = entry.get("content", "")
171
- if content:
172
  messages.append({"role": role, "content": content})
173
 
174
  messages.append({"role": "user", "content": message})
175
 
176
- llm = _load()
177
  stream = llm.create_chat_completion(
178
  messages=messages,
179
  temperature=0.6,
180
  top_p=0.8,
181
  top_k=20,
182
- max_tokens=4096,
183
  stream=True,
184
  )
185
 
@@ -195,13 +214,8 @@ def respond(message, history, uploaded_files):
195
  {"role": "assistant", "content": partial},
196
  ]
197
 
198
- yield history + [
199
- {"role": "user", "content": message},
200
- {"role": "assistant", "content": partial},
201
- ]
202
-
203
  def create_demo():
204
- with gr.Blocks(title="codeMax — Qwen 3.6 27B Coder") as demo:
205
  gr.Markdown(
206
  "# codeMax\n"
207
  "**Qwen 3.6 27B · Uncensored · Code-optimized · Dedicated GPU**\n"
@@ -213,6 +227,7 @@ def create_demo():
213
  chatbot = gr.Chatbot(
214
  label="Chat",
215
  height=580,
 
216
  avatar_images=(None, "https://huggingface.co/front/assets/huggingface_logo-noborder.svg"),
217
  )
218
  with gr.Row():
@@ -264,5 +279,4 @@ if __name__ == "__main__":
264
  server_name="0.0.0.0",
265
  server_port=7860,
266
  ssr_mode=False,
267
- css="""footer { display: none !important; }""",
268
- )
 
9
 
10
  MODEL_REPO = os.environ.get("MODEL_REPO", "DavidAU/Qwen3.6-27B-Heretic-Uncensored-FINETUNE-NEO-CODE-Di-IMatrix-MAX-GGUF")
11
  MODEL_FILE = os.environ.get("MODEL_FILE", "Qwen3.6-27B-NEO-CODE-HERE-2T-OT-Q4_K_M.gguf")
12
+ N_CTX = int(os.environ.get("N_CTX", "16384")) # FIX: was 4096
13
+ MAX_TOKENS = int(os.environ.get("MAX_TOKENS", "4096")) # FIX: env-tunable
14
  N_GPU = int(os.environ.get("N_GPU_LAYERS", os.environ.get("DAVIDAU_N_GPU_LAYERS", "-1")))
15
 
16
  SYSTEM_PROMPT = """You are codeMax, an uncensored 27B coding assistant. You excel at:
 
20
  - Reading shared files and answering about them
21
  Rules: put code in ```language blocks. Be precise and thorough."""
22
 
23
+ CSS = """footer { display: none !important; }""" # FIX: css lives here now
24
+
25
  _llm = None
26
  _model_path: Optional[str] = None
27
+ _boot_error: Optional[str] = None # FIX: surface startup failures in chat instead of a dead space
28
 
29
  def _download():
30
  global _model_path
 
54
  print("[MODEL] Ready.", file=sys.stderr)
55
  return _llm
56
 
57
+ # FIX: don't let a startup OOM kill the whole space silently — serve the UI, report the error
58
+ try:
59
+ print("[BOOT] Loading model at startup...", file=sys.stderr)
60
+ _load()
61
+ except Exception as e:
62
+ _boot_error = f"{type(e).__name__}: {e}"
63
+ print(f"[BOOT] Model load failed (will retry on first message): {_boot_error}", file=sys.stderr)
64
 
65
  def _read_text(path, enc="utf-8"):
66
  for e in [enc, "utf-8-sig", "latin-1", "cp1252", "utf-16"]:
 
167
  return "\n".join(parts)
168
 
169
  def respond(message, history, uploaded_files):
170
+ # FIX: if boot failed, say so in chat and retry once
171
+ try:
172
+ llm = _load()
173
+ except Exception as e:
174
+ err = _boot_error or f"{type(e).__name__}: {e}"
175
+ yield history + [
176
+ {"role": "user", "content": message},
177
+ {"role": "assistant", "content": f"⚠️ Model failed to load: `{err}`\n\nMost likely: hardware too small (needs A10G or 32GB CPU space) or still downloading — try again in a minute."},
178
+ ]
179
+ return
180
+
181
  file_context = parse_all_files(uploaded_files)
182
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
183
 
 
188
  for entry in history:
189
  role = entry.get("role", "user")
190
  content = entry.get("content", "")
191
+ if isinstance(content, str) and content:
192
  messages.append({"role": role, "content": content})
193
 
194
  messages.append({"role": "user", "content": message})
195
 
 
196
  stream = llm.create_chat_completion(
197
  messages=messages,
198
  temperature=0.6,
199
  top_p=0.8,
200
  top_k=20,
201
+ max_tokens=MAX_TOKENS,
202
  stream=True,
203
  )
204
 
 
214
  {"role": "assistant", "content": partial},
215
  ]
216
 
 
 
 
 
 
217
  def create_demo():
218
+ with gr.Blocks(title="codeMax — Qwen 3.6 27B Coder", css=CSS) as demo: # FIX: css here, not in launch()
219
  gr.Markdown(
220
  "# codeMax\n"
221
  "**Qwen 3.6 27B · Uncensored · Code-optimized · Dedicated GPU**\n"
 
227
  chatbot = gr.Chatbot(
228
  label="Chat",
229
  height=580,
230
+ type="messages", # FIX: explicit, matches respond() format
231
  avatar_images=(None, "https://huggingface.co/front/assets/huggingface_logo-noborder.svg"),
232
  )
233
  with gr.Row():
 
279
  server_name="0.0.0.0",
280
  server_port=7860,
281
  ssr_mode=False,
282
+ ) # FIX: css removed from launch()