Spaces:
Sleeping
Sleeping
Claude commited on
Replace retired zephyr default with a fallback chain of supported chat models
Browse filesHuggingFaceH4/zephyr-7b-beta is no longer carried by any Inference
Provider (router returns model_not_supported), which broke the chat
assistant. chat_update now tries a candidate list in order - an
HF_CHAT_MODEL override first, then Qwen2.5-7B-Instruct,
Mistral-7B-Instruct-v0.3, and Qwen2.5-72B-Instruct - and the run
metadata records whichever model actually served the call
(ACTIVE_MODEL) rather than the configured default.
- README.md +8 -3
- app.py +1 -1
- chat_config.py +36 -11
README.md
CHANGED
|
@@ -92,9 +92,14 @@ The Step 1 assistant calls the Hugging Face Inference API, so it needs a token:
|
|
| 92 |
- **Locally / as a fallback:** paste a token into the "Use your own
|
| 93 |
Hugging Face token" accordion in Step 1; it's only used for that session.
|
| 94 |
|
| 95 |
-
The
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
If the chat call fails or no token is available, the rest of the app
|
| 100 |
still works fully via the "Advanced: edit settings manually" accordion
|
|
|
|
| 92 |
- **Locally / as a fallback:** paste a token into the "Use your own
|
| 93 |
Hugging Face token" accordion in Step 1; it's only used for that session.
|
| 94 |
|
| 95 |
+
The token needs **Inference Providers** permission: use the simple
|
| 96 |
+
"Read" token type, or on a fine-grained token check "Make calls to
|
| 97 |
+
Inference Providers".
|
| 98 |
+
|
| 99 |
+
The assistant tries `Qwen/Qwen2.5-7B-Instruct` first, then falls back to
|
| 100 |
+
`mistralai/Mistral-7B-Instruct-v0.3` and `Qwen/Qwen2.5-72B-Instruct` if
|
| 101 |
+
a provider doesn't carry it. Set an `HF_CHAT_MODEL` env var/Space
|
| 102 |
+
variable to force a specific model instead.
|
| 103 |
|
| 104 |
If the chat call fails or no token is available, the rest of the app
|
| 105 |
still works fully via the "Advanced: edit settings manually" accordion
|
app.py
CHANGED
|
@@ -242,7 +242,7 @@ def process_files(files, categories, custom_keywords_str, custom_fields_str, onl
|
|
| 242 |
"custom_keywords": "; ".join(custom_keywords),
|
| 243 |
"custom_fields": "; ".join(custom_field_labels),
|
| 244 |
"only_matches_filter": only_matches,
|
| 245 |
-
"chat_model": chat_config.
|
| 246 |
"chat_transcript": _format_transcript(chat_history),
|
| 247 |
}
|
| 248 |
metadata_df = pd.DataFrame([metadata_row], columns=METADATA_COLUMNS)
|
|
|
|
| 242 |
"custom_keywords": "; ".join(custom_keywords),
|
| 243 |
"custom_fields": "; ".join(custom_field_labels),
|
| 244 |
"only_matches_filter": only_matches,
|
| 245 |
+
"chat_model": chat_config.ACTIVE_MODEL if chat_history else "",
|
| 246 |
"chat_transcript": _format_transcript(chat_history),
|
| 247 |
}
|
| 248 |
metadata_df = pd.DataFrame([metadata_row], columns=METADATA_COLUMNS)
|
chat_config.py
CHANGED
|
@@ -12,7 +12,22 @@ import extractors
|
|
| 12 |
|
| 13 |
CATEGORY_CHOICES = list(extractors.KEYWORD_SETS.keys())
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
SYSTEM_PROMPT = f"""You are a configuration assistant for a document OCR/data-extraction tool.
|
| 18 |
|
|
@@ -127,17 +142,27 @@ def chat_update(history, user_message, current_settings, hf_token=None):
|
|
| 127 |
messages.extend(history)
|
| 128 |
messages.append({"role": "user", "content": user_message})
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
return (
|
| 138 |
-
f"The chat model call failed
|
| 139 |
-
f"
|
| 140 |
-
"
|
|
|
|
|
|
|
| 141 |
current_settings,
|
| 142 |
)
|
| 143 |
|
|
|
|
| 12 |
|
| 13 |
CATEGORY_CHOICES = list(extractors.KEYWORD_SETS.keys())
|
| 14 |
|
| 15 |
+
# Tried in order until one is accepted by an inference provider. An
|
| 16 |
+
# HF_CHAT_MODEL env var/Space variable always takes first priority.
|
| 17 |
+
CANDIDATE_MODELS = [
|
| 18 |
+
m for m in [
|
| 19 |
+
os.environ.get("HF_CHAT_MODEL"),
|
| 20 |
+
"Qwen/Qwen2.5-7B-Instruct",
|
| 21 |
+
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 22 |
+
"Qwen/Qwen2.5-72B-Instruct",
|
| 23 |
+
] if m
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
DEFAULT_MODEL = CANDIDATE_MODELS[0]
|
| 27 |
+
|
| 28 |
+
# Set to whichever model actually served the most recent successful call,
|
| 29 |
+
# so the run-metadata audit trail records the real model used.
|
| 30 |
+
ACTIVE_MODEL = DEFAULT_MODEL
|
| 31 |
|
| 32 |
SYSTEM_PROMPT = f"""You are a configuration assistant for a document OCR/data-extraction tool.
|
| 33 |
|
|
|
|
| 142 |
messages.extend(history)
|
| 143 |
messages.append({"role": "user", "content": user_message})
|
| 144 |
|
| 145 |
+
global ACTIVE_MODEL
|
| 146 |
+
client = InferenceClient(token=token, provider="auto")
|
| 147 |
+
content = None
|
| 148 |
+
last_exc = None
|
| 149 |
+
for model in CANDIDATE_MODELS:
|
| 150 |
+
try:
|
| 151 |
+
response = client.chat_completion(
|
| 152 |
+
messages=messages, model=model, max_tokens=400, temperature=0.2
|
| 153 |
+
)
|
| 154 |
+
content = response.choices[0].message.content
|
| 155 |
+
ACTIVE_MODEL = model
|
| 156 |
+
break
|
| 157 |
+
except Exception as exc:
|
| 158 |
+
last_exc = exc
|
| 159 |
+
if content is None:
|
| 160 |
return (
|
| 161 |
+
f"The chat model call failed for every candidate model "
|
| 162 |
+
f"({', '.join(CANDIDATE_MODELS)}). Last error: {last_exc}. "
|
| 163 |
+
"Check that HF_TOKEN is valid and has Inference Providers "
|
| 164 |
+
"permission, or set the HF_CHAT_MODEL env var to a model your "
|
| 165 |
+
"account can reach.",
|
| 166 |
current_settings,
|
| 167 |
)
|
| 168 |
|