Spaces:
Paused
Paused
try
Browse files
app.py
CHANGED
|
@@ -316,28 +316,38 @@ class AbliterationProcessor:
|
|
| 316 |
return_tensors="pt"
|
| 317 |
)
|
| 318 |
|
| 319 |
-
# Generate response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
gen = self.model.generate(
|
| 321 |
toks.to(self.model.device),
|
| 322 |
max_new_tokens=2048,
|
| 323 |
temperature=0.7,
|
| 324 |
do_sample=True,
|
| 325 |
-
pad_token_id=self.tokenizer.eos_token_id
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
# Decode response
|
| 329 |
-
decoded = self.tokenizer.batch_decode(
|
| 330 |
-
gen[0][len(toks[0]):],
|
| 331 |
-
skip_special_tokens=True
|
| 332 |
)
|
| 333 |
|
| 334 |
-
response
|
|
|
|
| 335 |
return response, history + [[message, response]]
|
| 336 |
|
| 337 |
except Exception as e:
|
| 338 |
return f"❌ Chat error: {str(e)}", history
|
| 339 |
-
|
| 340 |
-
|
| 341 |
|
| 342 |
def get_new_model_card(original_card: ModelCard, original_model_id: str, new_repo_url: str) -> ModelCard:
|
| 343 |
"""Create new model card"""
|
|
@@ -530,33 +540,16 @@ def create_interface():
|
|
| 530 |
|
| 531 |
def bot(history):
|
| 532 |
if history and history[-1]["role"] == "user":
|
| 533 |
-
# Start with empty assistant message
|
| 534 |
-
history.append({"role": "assistant", "content": ""})
|
| 535 |
-
|
| 536 |
-
# Get the full response
|
| 537 |
-
response, _ = processor.chat(history[-2]["content"], history[:-2])
|
| 538 |
-
|
| 539 |
-
# Update the assistant message with the full response
|
| 540 |
-
history[-1]["content"] = response
|
| 541 |
-
return history
|
| 542 |
-
|
| 543 |
-
def bot_stream(history):
|
| 544 |
-
if history and history[-1]["role"] == "user":
|
| 545 |
-
# Get the full response first
|
| 546 |
response, _ = processor.chat(history[-1]["content"], history[:-1])
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
partial_response = ""
|
| 550 |
-
for char in response:
|
| 551 |
-
partial_response += char
|
| 552 |
-
yield history + [{"role": "assistant", "content": partial_response}]
|
| 553 |
|
| 554 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 555 |
-
|
| 556 |
)
|
| 557 |
|
| 558 |
send_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 559 |
-
|
| 560 |
)
|
| 561 |
|
| 562 |
clear.click(lambda: [], None, chatbot, queue=False)
|
|
|
|
| 316 |
return_tensors="pt"
|
| 317 |
)
|
| 318 |
|
| 319 |
+
# Generate response with streaming like abliterated_optimized.py
|
| 320 |
+
from transformers import TextStreamer
|
| 321 |
+
|
| 322 |
+
# Create a custom streamer that captures all output
|
| 323 |
+
captured_output = []
|
| 324 |
+
|
| 325 |
+
class CustomStreamer(TextStreamer):
|
| 326 |
+
def __init__(self, tokenizer, skip_prompt=True, skip_special_tokens=True):
|
| 327 |
+
super().__init__(tokenizer, skip_prompt=skip_prompt, skip_special_tokens=skip_special_tokens)
|
| 328 |
+
self.captured = []
|
| 329 |
+
|
| 330 |
+
def on_finalized_text(self, text: str, stream_end: bool = False):
|
| 331 |
+
self.captured.append(text)
|
| 332 |
+
super().on_finalized_text(text, stream_end)
|
| 333 |
+
|
| 334 |
+
streamer = CustomStreamer(self.tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 335 |
+
|
| 336 |
gen = self.model.generate(
|
| 337 |
toks.to(self.model.device),
|
| 338 |
max_new_tokens=2048,
|
| 339 |
temperature=0.7,
|
| 340 |
do_sample=True,
|
| 341 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
| 342 |
+
streamer=streamer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
)
|
| 344 |
|
| 345 |
+
# Get the complete response from streamer
|
| 346 |
+
response = "".join(streamer.captured).strip()
|
| 347 |
return response, history + [[message, response]]
|
| 348 |
|
| 349 |
except Exception as e:
|
| 350 |
return f"❌ Chat error: {str(e)}", history
|
|
|
|
|
|
|
| 351 |
|
| 352 |
def get_new_model_card(original_card: ModelCard, original_model_id: str, new_repo_url: str) -> ModelCard:
|
| 353 |
"""Create new model card"""
|
|
|
|
| 540 |
|
| 541 |
def bot(history):
|
| 542 |
if history and history[-1]["role"] == "user":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
response, _ = processor.chat(history[-1]["content"], history[:-1])
|
| 544 |
+
history.append({"role": "assistant", "content": response})
|
| 545 |
+
return history
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
|
| 547 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 548 |
+
bot, chatbot, chatbot
|
| 549 |
)
|
| 550 |
|
| 551 |
send_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 552 |
+
bot, chatbot, chatbot
|
| 553 |
)
|
| 554 |
|
| 555 |
clear.click(lambda: [], None, chatbot, queue=False)
|