Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -205,16 +205,35 @@ async def perform_core_search(raw_query: str):
|
|
| 205 |
return [{"match_score": round(s, 2), **doc} for s, doc in scored_results[:100]], prediction, best_score
|
| 206 |
|
| 207 |
def _generate_text(messages, max_tokens: int):
|
| 208 |
-
|
| 209 |
|
| 210 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
result = ai_generator(
|
| 212 |
prompt,
|
| 213 |
max_new_tokens=max_tokens,
|
| 214 |
do_sample=False,
|
| 215 |
-
truncation=True
|
|
|
|
|
|
|
| 216 |
)
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
return text_out
|
| 219 |
|
| 220 |
async def run_ai_task(messages, max_tokens: int, query_key: str):
|
|
|
|
| 205 |
return [{"match_score": round(s, 2), **doc} for s, doc in scored_results[:100]], prediction, best_score
|
| 206 |
|
| 207 |
def _generate_text(messages, max_tokens: int):
|
| 208 |
+
tokenizer = ai_generator.tokenizer
|
| 209 |
|
| 210 |
+
# 1. Use the model's Native Chat Template (Prevents random autocomplete/echoing)
|
| 211 |
+
try:
|
| 212 |
+
prompt = tokenizer.apply_chat_template(
|
| 213 |
+
messages,
|
| 214 |
+
tokenize=False,
|
| 215 |
+
add_generation_prompt=True
|
| 216 |
+
)
|
| 217 |
+
except Exception:
|
| 218 |
+
# Fallback if model doesn't have a strict template
|
| 219 |
+
prompt = "\n".join([f"{m['role'].capitalize()}: {m['content']}" for m in messages]) + "\nAssistant: "
|
| 220 |
+
|
| 221 |
+
# 2. return_full_text=False ensures the prompt isn't returned in the output!
|
| 222 |
result = ai_generator(
|
| 223 |
prompt,
|
| 224 |
max_new_tokens=max_tokens,
|
| 225 |
do_sample=False,
|
| 226 |
+
truncation=True,
|
| 227 |
+
return_full_text=False, # 🔥 THIS FIXES THE REPEATING QUERY ISSUE
|
| 228 |
+
pad_token_id=tokenizer.eos_token_id if tokenizer.eos_token_id is not None else 0
|
| 229 |
)
|
| 230 |
+
|
| 231 |
+
text_out = result[0]['generated_text'].strip()
|
| 232 |
+
|
| 233 |
+
# 3. Extra safety cleanup for small AI models
|
| 234 |
+
if text_out.lower().startswith("assistant:"):
|
| 235 |
+
text_out = text_out[10:].strip()
|
| 236 |
+
|
| 237 |
return text_out
|
| 238 |
|
| 239 |
async def run_ai_task(messages, max_tokens: int, query_key: str):
|