Update src/modules/llm_backbone.py
Browse files- src/modules/llm_backbone.py +286 -239
src/modules/llm_backbone.py
CHANGED
|
@@ -1,240 +1,287 @@
|
|
| 1 |
-
"""
|
| 2 |
-
modules/llm_backbone.py
|
| 3 |
-
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 4 |
-
VoiceVerse Pro β LLM Script Generation Layer
|
| 5 |
-
|
| 6 |
-
Model : meta-llama/Llama-3.1-8B-Instruct (default β widely supported 2026)
|
| 7 |
-
Swap via LLMConfig.model_id for any HF-hosted chat model.
|
| 8 |
-
Backend: huggingface_hub.InferenceClient with provider="
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
-
|
| 21 |
-
-
|
| 22 |
-
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
import
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
{
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
β’
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
target_words
|
| 185 |
-
)
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
if
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
return self._client
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
modules/llm_backbone.py
|
| 3 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 4 |
+
VoiceVerse Pro β LLM Script Generation Layer
|
| 5 |
+
|
| 6 |
+
Model : meta-llama/Llama-3.1-8B-Instruct (default β widely supported 2026)
|
| 7 |
+
Swap via LLMConfig.model_id for any HF-hosted chat model.
|
| 8 |
+
Backend: huggingface_hub.InferenceClient with provider="hf-inference"
|
| 9 |
+
Forces HF's own serverless inference endpoint β avoids third-party
|
| 10 |
+
providers (e.g. Together) that independently deprecate models and
|
| 11 |
+
return 410 Gone errors.
|
| 12 |
+
Format : ChatCompletion messages API (system + user roles)
|
| 13 |
+
|
| 14 |
+
WHY NOT HuggingFaceEndpoint?
|
| 15 |
+
langchain-huggingface's HuggingFaceEndpoint internally calls
|
| 16 |
+
InferenceClient.post(), which was REMOVED in huggingface_hub β₯ 0.26.
|
| 17 |
+
Using InferenceClient.chat_completion() directly is the stable 2026 path.
|
| 18 |
+
|
| 19 |
+
DESIGN RULES:
|
| 20 |
+
- The LLM NEVER generates without retrieved context.
|
| 21 |
+
- Context is injected verbatim into every prompt via the user message.
|
| 22 |
+
- Output is structured spoken prose (transcript) or [HOST]/[GUEST] tagged
|
| 23 |
+
dialogue (podcast), depending on output_mode.
|
| 24 |
+
- Temperature, max_new_tokens are runtime-configurable.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
from __future__ import annotations
|
| 28 |
+
|
| 29 |
+
import logging
|
| 30 |
+
import os
|
| 31 |
+
from dataclasses import dataclass
|
| 32 |
+
from typing import Optional
|
| 33 |
+
|
| 34 |
+
logger = logging.getLogger(__name__)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 38 |
+
# Supported model presets (shown in sidebar dropdown)
|
| 39 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 40 |
+
|
| 41 |
+
SUPPORTED_MODELS = [
|
| 42 |
+
"mistralai/Mistral-7B-Instruct-v0.2", # compact, capable
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
DEFAULT_MODEL = SUPPORTED_MODELS[0]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 49 |
+
# Configuration
|
| 50 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 51 |
+
|
| 52 |
+
@dataclass
|
| 53 |
+
class LLMConfig:
|
| 54 |
+
"""Runtime-tunable LLM parameters."""
|
| 55 |
+
model_id: str = DEFAULT_MODEL
|
| 56 |
+
max_new_tokens: int = 1024
|
| 57 |
+
temperature: float = 0.65
|
| 58 |
+
hf_token: Optional[str] = None
|
| 59 |
+
# Force HF's own serverless inference β avoids Together/other providers
|
| 60 |
+
# that deprecate models independently of HF's model hub.
|
| 61 |
+
provider: str = "auto"
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 65 |
+
# Prompt templates
|
| 66 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 67 |
+
|
| 68 |
+
SYSTEM_PROMPT = """\
|
| 69 |
+
You are VoiceVerse, a world-class scriptwriter for spoken-audio content.
|
| 70 |
+
Your ONLY source of facts is the RETRIEVED CONTEXT provided in the user message.
|
| 71 |
+
You MUST NOT introduce information not present in that context.
|
| 72 |
+
Write in a warm, engaging, conversational spoken-English style.
|
| 73 |
+
No markdown, no bullet points, no headers β pure spoken prose only.
|
| 74 |
+
The script will be read aloud by a TTS engine."""
|
| 75 |
+
|
| 76 |
+
USER_TEMPLATE = """\
|
| 77 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 78 |
+
RETRIEVED CONTEXT (your SOLE factual source):
|
| 79 |
+
{context}
|
| 80 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 81 |
+
|
| 82 |
+
TASK:
|
| 83 |
+
{task_description}
|
| 84 |
+
|
| 85 |
+
FORMAT REQUIREMENTS:
|
| 86 |
+
β’ Open with a compelling hook (1β2 sentences).
|
| 87 |
+
β’ Develop the topic across 3β5 natural paragraphs drawn ONLY from the context.
|
| 88 |
+
β’ Close with a memorable takeaway or question to the listener.
|
| 89 |
+
β’ No markdown. No lists. No headers. Pure spoken prose.
|
| 90 |
+
β’ Target length: {target_words} words."""
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# ββ Podcast (two-speaker) prompts βββββββββββββββββββββββββββββββββββββββββββββ
|
| 94 |
+
|
| 95 |
+
PODCAST_SYSTEM_PROMPT = """\
|
| 96 |
+
You are VoiceVerse, a world-class podcast scriptwriter.
|
| 97 |
+
Your ONLY source of facts is the RETRIEVED CONTEXT provided in the user message.
|
| 98 |
+
You MUST NOT introduce information not present in that context.
|
| 99 |
+
Write a natural back-and-forth dialogue between two speakers:
|
| 100 |
+
HOST β female, warm and inquisitive, guides the conversation
|
| 101 |
+
GUEST β male, knowledgeable and enthusiastic, elaborates on topics
|
| 102 |
+
Each line MUST start with exactly "[HOST]" or "[GUEST]" followed by a space and the spoken text.
|
| 103 |
+
No markdown, no stage directions, no descriptions β only spoken dialogue lines.
|
| 104 |
+
The script will be read aloud by a TTS engine with two distinct voices."""
|
| 105 |
+
|
| 106 |
+
PODCAST_USER_TEMPLATE = """\
|
| 107 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 108 |
+
RETRIEVED CONTEXT (your SOLE factual source):
|
| 109 |
+
{context}
|
| 110 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 111 |
+
|
| 112 |
+
TASK:
|
| 113 |
+
{task_description}
|
| 114 |
+
|
| 115 |
+
FORMAT REQUIREMENTS (STRICTLY FOLLOW):
|
| 116 |
+
β’ Every line must start with [HOST] or [GUEST] followed by their spoken words.
|
| 117 |
+
β’ Alternate naturally between HOST and GUEST. Aim for 8β16 exchanges.
|
| 118 |
+
β’ HOST opens and closes the episode.
|
| 119 |
+
β’ Draw ALL facts ONLY from the context above.
|
| 120 |
+
β’ No markdown. No stage directions. No headers. Only dialogue lines.
|
| 121 |
+
β’ Target total length: {target_words} words of dialogue.
|
| 122 |
+
|
| 123 |
+
Example format:
|
| 124 |
+
[HOST] Welcome to VoiceVerse. Today we're diving into something fascinating.
|
| 125 |
+
[GUEST] Thanks for having me. I've been looking forward to this conversation.
|
| 126 |
+
[HOST] Let's start with the basics. What should our listeners know first?
|
| 127 |
+
[GUEST] Great question. The most important thing to understand is..."""
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 131 |
+
# LLM Backbone
|
| 132 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 133 |
+
|
| 134 |
+
class LLMBackbone:
|
| 135 |
+
"""
|
| 136 |
+
Calls huggingface_hub.InferenceClient.chat_completion() to generate
|
| 137 |
+
grounded spoken-style scripts.
|
| 138 |
+
|
| 139 |
+
Uses provider="hf-inference" (HF's own serverless endpoint) to avoid
|
| 140 |
+
third-party providers that independently deprecate models.
|
| 141 |
+
Supports two output modes:
|
| 142 |
+
- Transcript: plain spoken prose
|
| 143 |
+
- Podcast: [HOST]/[GUEST] tagged dialogue for dual-voice TTS
|
| 144 |
+
"""
|
| 145 |
+
|
| 146 |
+
def __init__(self, config: Optional[LLMConfig] = None) -> None:
|
| 147 |
+
self.config = config or LLMConfig()
|
| 148 |
+
self._client = None
|
| 149 |
+
logger.info(
|
| 150 |
+
"LLMBackbone initialised | model=%s | provider=%s",
|
| 151 |
+
self.config.model_id,
|
| 152 |
+
self.config.provider,
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
# ββ Public API βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 156 |
+
|
| 157 |
+
def generate_script(
|
| 158 |
+
self,
|
| 159 |
+
context_text: str,
|
| 160 |
+
task_description: str,
|
| 161 |
+
target_words: int = 400,
|
| 162 |
+
output_mode: str = "Audio Transcript", # matches OutputMode.value
|
| 163 |
+
) -> str:
|
| 164 |
+
"""
|
| 165 |
+
Generate a grounded script.
|
| 166 |
+
|
| 167 |
+
Args:
|
| 168 |
+
context_text: Retrieved context from RAGEngine (REQUIRED).
|
| 169 |
+
task_description: High-level user instruction for the script.
|
| 170 |
+
target_words: Approximate word count target.
|
| 171 |
+
output_mode: "Audio Transcript" or "Podcast (2 Speakers)".
|
| 172 |
+
|
| 173 |
+
Returns:
|
| 174 |
+
Clean script text. Podcast scripts have [HOST]/[GUEST] line prefixes.
|
| 175 |
+
"""
|
| 176 |
+
if not context_text or not context_text.strip():
|
| 177 |
+
raise ValueError(
|
| 178 |
+
"context_text must not be empty. "
|
| 179 |
+
"The LLM requires retrieved context to generate."
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
is_podcast = output_mode == "Podcast (2 Speakers)"
|
| 183 |
+
messages = self._build_messages(
|
| 184 |
+
context_text, task_description, target_words, is_podcast
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
logger.info(
|
| 188 |
+
"Calling chat_completion | model=%s | mode=%s | ~%d context chars",
|
| 189 |
+
self.config.model_id,
|
| 190 |
+
output_mode,
|
| 191 |
+
len(context_text),
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
try:
|
| 195 |
+
response = self._get_client().chat_completion(
|
| 196 |
+
messages=messages,
|
| 197 |
+
max_tokens=self.config.max_new_tokens,
|
| 198 |
+
temperature=self.config.temperature,
|
| 199 |
+
)
|
| 200 |
+
raw_output: str = response.choices[0].message.content
|
| 201 |
+
except Exception as exc:
|
| 202 |
+
logger.error("InferenceClient call failed: %s", exc)
|
| 203 |
+
raise RuntimeError(f"LLM generation failed: {exc}") from exc
|
| 204 |
+
|
| 205 |
+
script = self._post_process(raw_output)
|
| 206 |
+
logger.info("Script generated | %d words | podcast=%s", len(script.split()), is_podcast)
|
| 207 |
+
return script
|
| 208 |
+
|
| 209 |
+
# ββ Message builder ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 210 |
+
|
| 211 |
+
def _build_messages(
|
| 212 |
+
self,
|
| 213 |
+
context: str,
|
| 214 |
+
task: str,
|
| 215 |
+
target_words: int,
|
| 216 |
+
is_podcast: bool = False,
|
| 217 |
+
) -> list[dict]:
|
| 218 |
+
if is_podcast:
|
| 219 |
+
system = PODCAST_SYSTEM_PROMPT
|
| 220 |
+
user_content = PODCAST_USER_TEMPLATE.format(
|
| 221 |
+
context=context,
|
| 222 |
+
task_description=task,
|
| 223 |
+
target_words=target_words,
|
| 224 |
+
)
|
| 225 |
+
else:
|
| 226 |
+
system = SYSTEM_PROMPT
|
| 227 |
+
user_content = USER_TEMPLATE.format(
|
| 228 |
+
context=context,
|
| 229 |
+
task_description=task,
|
| 230 |
+
target_words=target_words,
|
| 231 |
+
)
|
| 232 |
+
return [
|
| 233 |
+
{"role": "system", "content": system},
|
| 234 |
+
{"role": "user", "content": user_content},
|
| 235 |
+
]
|
| 236 |
+
|
| 237 |
+
# ββ Post-processing ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 238 |
+
|
| 239 |
+
@staticmethod
|
| 240 |
+
def _post_process(raw: str) -> str:
|
| 241 |
+
for tag in ("[INST]", "[/INST]", "</s>", "<s>", "<<SYS>>", "<</SYS>>"):
|
| 242 |
+
raw = raw.replace(tag, "")
|
| 243 |
+
lines = [line.rstrip() for line in raw.splitlines()]
|
| 244 |
+
cleaned: list[str] = []
|
| 245 |
+
blank_count = 0
|
| 246 |
+
for line in lines:
|
| 247 |
+
if not line.strip():
|
| 248 |
+
blank_count += 1
|
| 249 |
+
if blank_count <= 2:
|
| 250 |
+
cleaned.append("")
|
| 251 |
+
else:
|
| 252 |
+
blank_count = 0
|
| 253 |
+
cleaned.append(line)
|
| 254 |
+
return "\n".join(cleaned).strip()
|
| 255 |
+
|
| 256 |
+
# ββ Lazy client init βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 257 |
+
|
| 258 |
+
def _get_client(self):
|
| 259 |
+
"""
|
| 260 |
+
Lazy-load huggingface_hub.InferenceClient with provider="hf-inference".
|
| 261 |
+
Uses HF's own serverless inference endpoint β avoids third-party providers
|
| 262 |
+
(e.g. Together) that independently deprecate models and return 410 Gone.
|
| 263 |
+
The client is bound to a specific model at init time.
|
| 264 |
+
"""
|
| 265 |
+
if self._client is None:
|
| 266 |
+
from huggingface_hub import InferenceClient
|
| 267 |
+
|
| 268 |
+
token = self.config.hf_token or os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 269 |
+
if not token:
|
| 270 |
+
raise EnvironmentError(
|
| 271 |
+
"Hugging Face API token not found. "
|
| 272 |
+
"Set HUGGINGFACEHUB_API_TOKEN in your .env file "
|
| 273 |
+
"or paste it in the sidebar."
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
logger.info(
|
| 277 |
+
"Initialising InferenceClient | model=%s | provider=%s",
|
| 278 |
+
self.config.model_id,
|
| 279 |
+
self.config.provider,
|
| 280 |
+
)
|
| 281 |
+
self._client = InferenceClient(
|
| 282 |
+
model=self.config.model_id, # bind model at client level
|
| 283 |
+
token=token,
|
| 284 |
+
provider=self.config.provider,
|
| 285 |
+
)
|
| 286 |
+
logger.info("InferenceClient ready | provider=%s | model=%s", self.config.provider, self.config.model_id)
|
| 287 |
return self._client
|