Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files
app.py
CHANGED
|
@@ -101,76 +101,6 @@ def generate(model_id, extension_method, new_context_length, rope_type, rope_fac
|
|
| 101 |
return f"Error during generation: {str(e)}"
|
| 102 |
|
| 103 |
|
| 104 |
-
# Chat-based generation function for conversational UI
|
| 105 |
-
@spaces.GPU(duration=120)
|
| 106 |
-
def chat_generate(message, history, model_id, extension_method, context_multiplier, rope_type, rope_factor, max_new_tokens, temperature, top_p):
|
| 107 |
-
"""Generate response in conversational format with streaming."""
|
| 108 |
-
if not model_id.strip():
|
| 109 |
-
yield "Error: Please select a model ID"
|
| 110 |
-
return
|
| 111 |
-
|
| 112 |
-
# Get base context length and calculate new context
|
| 113 |
-
base_context = 32768 # Default base for Qwen3
|
| 114 |
-
new_context_length = calculate_context_length(base_context, context_multiplier)
|
| 115 |
-
|
| 116 |
-
# Build full prompt from history
|
| 117 |
-
prompt = message
|
| 118 |
-
for user_msg, assistant_msg in history:
|
| 119 |
-
prompt = f"User: {user_msg}\nAssistant: {assistant_msg}\nUser: {message}\nAssistant:"
|
| 120 |
-
|
| 121 |
-
if not prompt.strip():
|
| 122 |
-
yield "Error: Please enter a message"
|
| 123 |
-
return
|
| 124 |
-
|
| 125 |
-
try:
|
| 126 |
-
model_data = load_model_with_extension(model_id, extension_method, new_context_length, rope_type, rope_factor)
|
| 127 |
-
except Exception as e:
|
| 128 |
-
yield f"Error loading model: {str(e)}"
|
| 129 |
-
return
|
| 130 |
-
|
| 131 |
-
model = model_data["model"]
|
| 132 |
-
tokenizer = model_data["tokenizer"]
|
| 133 |
-
|
| 134 |
-
try:
|
| 135 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 136 |
-
|
| 137 |
-
# Stream generation
|
| 138 |
-
full_response = ""
|
| 139 |
-
from transformers import TextIteratorStreamer
|
| 140 |
-
from threading import Thread
|
| 141 |
-
|
| 142 |
-
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 143 |
-
|
| 144 |
-
generation_kwargs = {
|
| 145 |
-
"inputs": inputs,
|
| 146 |
-
"max_new_tokens": max_new_tokens,
|
| 147 |
-
"temperature": temperature,
|
| 148 |
-
"top_p": top_p,
|
| 149 |
-
"do_sample": temperature > 0,
|
| 150 |
-
"pad_token_id": tokenizer.pad_token_id,
|
| 151 |
-
"eos_token_id": tokenizer.eos_token_id,
|
| 152 |
-
"streamer": streamer
|
| 153 |
-
}
|
| 154 |
-
|
| 155 |
-
# Run generation in thread
|
| 156 |
-
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 157 |
-
thread.start()
|
| 158 |
-
|
| 159 |
-
# Yield streamed response
|
| 160 |
-
for text in streamer:
|
| 161 |
-
full_response += text
|
| 162 |
-
yield full_response
|
| 163 |
-
|
| 164 |
-
thread.join()
|
| 165 |
-
|
| 166 |
-
if not full_response.strip():
|
| 167 |
-
yield "Model generated same text as input. Try adjusting parameters."
|
| 168 |
-
return
|
| 169 |
-
|
| 170 |
-
except Exception as e:
|
| 171 |
-
yield f"Error during generation: {str(e)}"
|
| 172 |
-
|
| 173 |
-
|
| 174 |
# Default model - recent Qwen3 series
|
| 175 |
DEFAULT_MODEL = "Qwen/Qwen3-30B-A3B-Thinking-2507"
|
| 176 |
|
|
@@ -296,12 +226,12 @@ with gr.Blocks(title="Context Window Extender - Chat") as demo:
|
|
| 296 |
):
|
| 297 |
"""Handle chat response with streaming."""
|
| 298 |
if not message.strip():
|
| 299 |
-
yield history + [
|
| 300 |
return
|
| 301 |
|
| 302 |
# Add user message to history
|
| 303 |
-
history.append(
|
| 304 |
-
yield history
|
| 305 |
|
| 306 |
# Generate response
|
| 307 |
try:
|
|
@@ -311,13 +241,7 @@ with gr.Blocks(title="Context Window Extender - Chat") as demo:
|
|
| 311 |
# Build prompt from history
|
| 312 |
prompt = message
|
| 313 |
for user_msg, assistant_msg in history[:-1]:
|
| 314 |
-
|
| 315 |
-
user_content = user_msg.get("content", str(user_msg))
|
| 316 |
-
assistant_content = assistant_msg.get("content", str(assistant_msg)) if isinstance(assistant_msg, dict) else str(assistant_msg)
|
| 317 |
-
else:
|
| 318 |
-
user_content = str(user_msg)
|
| 319 |
-
assistant_content = str(assistant_msg)
|
| 320 |
-
prompt = f"User: {user_content}\nAssistant: {assistant_content}\n" + prompt
|
| 321 |
|
| 322 |
prompt = prompt + "\nAssistant:"
|
| 323 |
|
|
@@ -357,18 +281,20 @@ with gr.Blocks(title="Context Window Extender - Chat") as demo:
|
|
| 357 |
for text in streamer:
|
| 358 |
full_response += text
|
| 359 |
# Update the last message (assistant response)
|
| 360 |
-
|
| 361 |
-
yield
|
| 362 |
|
| 363 |
thread.join()
|
| 364 |
|
| 365 |
if not full_response.strip():
|
| 366 |
full_response = "Model generated same text as input. Try adjusting parameters."
|
|
|
|
|
|
|
| 367 |
|
| 368 |
except Exception as e:
|
| 369 |
full_response = f"Error: {str(e)}"
|
| 370 |
-
|
| 371 |
-
|
| 372 |
|
| 373 |
# ChatInterface
|
| 374 |
chat_interface = gr.ChatInterface(
|
|
|
|
| 101 |
return f"Error during generation: {str(e)}"
|
| 102 |
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
# Default model - recent Qwen3 series
|
| 105 |
DEFAULT_MODEL = "Qwen/Qwen3-30B-A3B-Thinking-2507"
|
| 106 |
|
|
|
|
| 226 |
):
|
| 227 |
"""Handle chat response with streaming."""
|
| 228 |
if not message.strip():
|
| 229 |
+
yield [(user_msg, "Please enter a message.") for user_msg, _ in history] + [(message, "Please enter a message.")]
|
| 230 |
return
|
| 231 |
|
| 232 |
# Add user message to history
|
| 233 |
+
history.append((message, ""))
|
| 234 |
+
yield history
|
| 235 |
|
| 236 |
# Generate response
|
| 237 |
try:
|
|
|
|
| 241 |
# Build prompt from history
|
| 242 |
prompt = message
|
| 243 |
for user_msg, assistant_msg in history[:-1]:
|
| 244 |
+
prompt = f"User: {user_msg}\nAssistant: {assistant_msg}\n" + prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
prompt = prompt + "\nAssistant:"
|
| 247 |
|
|
|
|
| 281 |
for text in streamer:
|
| 282 |
full_response += text
|
| 283 |
# Update the last message (assistant response)
|
| 284 |
+
history[-1] = (message, full_response)
|
| 285 |
+
yield history
|
| 286 |
|
| 287 |
thread.join()
|
| 288 |
|
| 289 |
if not full_response.strip():
|
| 290 |
full_response = "Model generated same text as input. Try adjusting parameters."
|
| 291 |
+
history[-1] = (message, full_response)
|
| 292 |
+
yield history
|
| 293 |
|
| 294 |
except Exception as e:
|
| 295 |
full_response = f"Error: {str(e)}"
|
| 296 |
+
history[-1] = (message, full_response)
|
| 297 |
+
yield history
|
| 298 |
|
| 299 |
# ChatInterface
|
| 300 |
chat_interface = gr.ChatInterface(
|