ps1811 commited on
Commit
3a35e18
Β·
1 Parent(s): f689170

Corrected model download path

Browse files
Files changed (2) hide show
  1. app/models/llm.py +2 -2
  2. app/recs/generate.py +50 -28
app/models/llm.py CHANGED
@@ -6,8 +6,8 @@ import threading
6
  from huggingface_hub import hf_hub_download
7
  from llama_cpp import Llama
8
 
9
- HF_REPO = "Abiray/MiniCPM5-1B-GGUF"
10
- HF_FILENAME = "minicpm5-1b-Q4_K_M.gguf"
11
 
12
  _model: Llama | None = None
13
  _init_lock = threading.Lock()
 
6
  from huggingface_hub import hf_hub_download
7
  from llama_cpp import Llama
8
 
9
+ HF_REPO = os.getenv("LLAMA_HF_REPO", "openbmb/MiniCPM5-1B-GGUF")
10
+ HF_FILENAME = os.getenv("LLAMA_HF_FILENAME", "MiniCPM5-1B-Q4_K_M.gguf")
11
 
12
  _model: Llama | None = None
13
  _init_lock = threading.Lock()
app/recs/generate.py CHANGED
@@ -29,16 +29,42 @@ def _messages_to_prompt(messages: list[dict[str, str]]) -> str:
29
  for msg in messages:
30
  role = msg["role"]
31
  content = msg["content"]
32
- if role == "system":
33
- chunks.append(f"<|im_start|>system\n{content}\n")
34
- elif role == "user":
35
- chunks.append(f"<|im_start|>user\n{content}\n")
36
- elif role == "assistant":
37
- chunks.append(f"<|im_start|>assistant\n{content}\n")
38
  chunks.append("<|im_start|>assistant\n")
39
  return "".join(chunks)
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def generate_explanation(prompt: str, rec: Dict | None = None, stream: bool = False) -> str:
43
  print("\nπŸ”₯ [generate_explanation] CALLED", flush=True)
44
 
@@ -65,30 +91,26 @@ def generate_explanation(prompt: str, rec: Dict | None = None, stream: bool = Fa
65
  ]
66
 
67
  print("πŸš€ [generate_explanation] calling LLM...", flush=True)
68
- out = llm(
69
- _messages_to_prompt(messages),
70
- max_tokens=int(os.getenv("LLAMA_MAX_TOKENS", "512")),
71
- temperature=0.7,
72
- stop=_STOP_SEQUENCES,
73
- echo=False,
74
- )
75
- raw = (out["choices"][0].get("text") or "").strip()
 
 
 
 
 
 
76
  print("πŸ“‘ [generate_explanation] response received", flush=True)
77
  print("πŸ“„ [generate_explanation] raw output length:", len(raw), flush=True)
 
 
78
 
79
- clean = re.sub(
80
- r"<\s*think\s*>.*?<\s*/\s*think\s*>",
81
- "",
82
- raw,
83
- flags=re.DOTALL | re.IGNORECASE,
84
- )
85
- clean = re.sub(
86
- r"<think>.*?</think>",
87
- "",
88
- clean,
89
- flags=re.DOTALL | re.IGNORECASE,
90
- )
91
- clean = re.sub(r"\s+", " ", clean).strip()
92
  clean = sanitize_explanation(clean, rec)
93
 
94
  print("✨ [generate_explanation] cleaned output ready", flush=True)
@@ -97,4 +119,4 @@ def generate_explanation(prompt: str, rec: Dict | None = None, stream: bool = Fa
97
  except Exception as e:
98
  print("❌ [generate_explanation] ERROR:", repr(e), flush=True)
99
  traceback.print_exc()
100
- return fallback_explanation(rec)
 
29
  for msg in messages:
30
  role = msg["role"]
31
  content = msg["content"]
32
+ chunks.append(f"<|im_start|>{role}\n{content}{_IM_END}\n")
 
 
 
 
 
33
  chunks.append("<|im_start|>assistant\n")
34
  return "".join(chunks)
35
 
36
 
37
+ def _strip_thinking(text: str) -> str:
38
+ text = re.sub(r"<\s*think\s*>.*?<\s*/\s*think\s*>", "", text, flags=re.DOTALL | re.IGNORECASE)
39
+ text = re.sub(
40
+ r"<think>.*?</think>",
41
+ "",
42
+ text,
43
+ flags=re.DOTALL | re.IGNORECASE,
44
+ )
45
+ return re.sub(r"\s+", " ", text).strip()
46
+
47
+
48
+ def _message_text(message: dict) -> str:
49
+ content = (message.get("content") or "").strip()
50
+ reasoning = (message.get("reasoning_content") or "").strip()
51
+ if content and reasoning:
52
+ return content if len(content) >= len(reasoning) else reasoning
53
+ return content or reasoning
54
+
55
+
56
+ def _run_completion(llm, messages: list[dict[str, str]]) -> str:
57
+ prompt = _messages_to_prompt(messages)
58
+ out = llm(
59
+ prompt,
60
+ max_tokens=int(os.getenv("LLAMA_MAX_TOKENS", "512")),
61
+ temperature=0.7,
62
+ stop=_STOP_SEQUENCES,
63
+ echo=False,
64
+ )
65
+ return (out["choices"][0].get("text") or "").strip()
66
+
67
+
68
  def generate_explanation(prompt: str, rec: Dict | None = None, stream: bool = False) -> str:
69
  print("\nπŸ”₯ [generate_explanation] CALLED", flush=True)
70
 
 
91
  ]
92
 
93
  print("πŸš€ [generate_explanation] calling LLM...", flush=True)
94
+ raw = _run_completion(llm, messages)
95
+
96
+ if not raw:
97
+ print("⚠️ [generate_explanation] raw empty β€” trying create_chat_completion", flush=True)
98
+ try:
99
+ out = llm.create_chat_completion(
100
+ messages=messages,
101
+ max_tokens=int(os.getenv("LLAMA_MAX_TOKENS", "512")),
102
+ temperature=0.7,
103
+ )
104
+ raw = _message_text(out["choices"][0]["message"])
105
+ except TypeError as exc:
106
+ print(f"⚠️ [generate_explanation] chat_completion failed: {exc}", flush=True)
107
+
108
  print("πŸ“‘ [generate_explanation] response received", flush=True)
109
  print("πŸ“„ [generate_explanation] raw output length:", len(raw), flush=True)
110
+ if raw:
111
+ print("πŸ“„ [generate_explanation] raw preview:", raw[:400], flush=True)
112
 
113
+ clean = _strip_thinking(raw)
 
 
 
 
 
 
 
 
 
 
 
 
114
  clean = sanitize_explanation(clean, rec)
115
 
116
  print("✨ [generate_explanation] cleaned output ready", flush=True)
 
119
  except Exception as e:
120
  print("❌ [generate_explanation] ERROR:", repr(e), flush=True)
121
  traceback.print_exc()
122
+ return f"⚠️ Analysis failed: {e}"