Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ import torch
|
|
10 |
import gradio as gr
|
11 |
from transformers import (
|
12 |
AutoModelForCausalLM,
|
|
|
13 |
GenerationConfig
|
14 |
)
|
15 |
|
@@ -46,7 +47,7 @@ def optimize_memory():
|
|
46 |
###############################################################################
|
47 |
# Model Loading with Error Handling
|
48 |
###############################################################################
|
49 |
-
def
|
50 |
"""Load the model with comprehensive error handling and logging"""
|
51 |
logger.info(f"Loading model: {MODEL_ID}")
|
52 |
logger.info(f"Transformers version: {transformers.__version__}")
|
@@ -60,6 +61,20 @@ def load_model():
|
|
60 |
}
|
61 |
logger.info(f"Device information: {device_info}")
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
# Load model with detailed error logging
|
64 |
try:
|
65 |
logger.info("Loading model...")
|
@@ -107,10 +122,10 @@ def load_model():
|
|
107 |
logger.error(traceback.format_exc())
|
108 |
raise RuntimeError(f"Failed to load model: {str(e)}")
|
109 |
|
110 |
-
return model
|
111 |
|
112 |
###############################################################################
|
113 |
-
#
|
114 |
###############################################################################
|
115 |
def format_prompt(messages, system_prompt=DEFAULT_SYSTEM_PROMPT):
|
116 |
"""
|
@@ -141,19 +156,17 @@ def format_prompt(messages, system_prompt=DEFAULT_SYSTEM_PROMPT):
|
|
141 |
logger.info(f"Formatted prompt (length: {len(prompt)})")
|
142 |
return prompt
|
143 |
|
144 |
-
def generate_text(model, prompt, temperature=0.7, top_p=0.9, max_new_tokens=256):
|
145 |
"""
|
146 |
-
Generate text using the pipeline
|
147 |
-
This is a simplified approach that doesn't rely on tokenizers.
|
148 |
"""
|
149 |
logger.info(f"Generating text with temp={temperature}, top_p={top_p}, max_tokens={max_new_tokens}")
|
150 |
|
151 |
-
# Create a simple text-generation pipeline
|
152 |
try:
|
153 |
-
#
|
154 |
-
|
155 |
|
156 |
-
#
|
157 |
gen_config = {
|
158 |
"temperature": temperature,
|
159 |
"top_p": top_p,
|
@@ -161,19 +174,48 @@ def generate_text(model, prompt, temperature=0.7, top_p=0.9, max_new_tokens=256)
|
|
161 |
"max_new_tokens": max_new_tokens,
|
162 |
"repetition_penalty": 1.1,
|
163 |
}
|
164 |
-
|
165 |
-
# Log what we're doing
|
166 |
-
logger.info(f"Input prompt length: {len(prompt)}")
|
167 |
logger.info(f"Generation config: {gen_config}")
|
168 |
|
169 |
-
#
|
170 |
-
|
171 |
-
"
|
172 |
-
|
173 |
-
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
-
#
|
177 |
generation_start = time.time()
|
178 |
outputs = pipe(
|
179 |
prompt,
|
@@ -181,7 +223,7 @@ def generate_text(model, prompt, temperature=0.7, top_p=0.9, max_new_tokens=256)
|
|
181 |
**gen_config
|
182 |
)
|
183 |
generation_time = time.time() - generation_start
|
184 |
-
logger.info(f"
|
185 |
|
186 |
# Extract the generated text
|
187 |
generated_text = outputs[0]["generated_text"]
|
@@ -195,12 +237,22 @@ def generate_text(model, prompt, temperature=0.7, top_p=0.9, max_new_tokens=256)
|
|
195 |
except Exception as e:
|
196 |
logger.error(f"Error in generate_text: {e}")
|
197 |
logger.error(traceback.format_exc())
|
198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
###############################################################################
|
201 |
# Gradio Interface
|
202 |
###############################################################################
|
203 |
-
def build_gradio_interface(model):
|
204 |
"""Build and launch the Gradio interface"""
|
205 |
logger.info("Building Gradio interface")
|
206 |
|
@@ -239,29 +291,30 @@ def build_gradio_interface(model):
|
|
239 |
# Generate response
|
240 |
assistant_response = generate_text(
|
241 |
model,
|
|
|
242 |
prompt,
|
243 |
temperature=temp,
|
244 |
top_p=top_p,
|
245 |
max_new_tokens=max_tokens
|
246 |
)
|
247 |
|
248 |
-
#
|
249 |
-
|
250 |
-
|
|
|
|
|
|
|
251 |
for msg in formatted_history:
|
252 |
if msg["role"] == "system":
|
253 |
-
continue # Skip system messages
|
254 |
-
role
|
255 |
-
updated_history.append((role, msg["content"]))
|
256 |
|
257 |
-
# Add assistant response
|
258 |
-
updated_history.append(("assistant", assistant_response))
|
259 |
logger.info(f"Added assistant response (length: {len(assistant_response)})")
|
260 |
|
261 |
# Optimize memory after generation
|
262 |
optimize_memory()
|
263 |
|
264 |
-
return
|
265 |
|
266 |
except Exception as e:
|
267 |
logger.error(f"Error in user_submit: {str(e)}")
|
@@ -270,21 +323,39 @@ def build_gradio_interface(model):
|
|
270 |
# Return original message history plus error message
|
271 |
error_msg = "I encountered an error processing your request. Please try again."
|
272 |
|
273 |
-
#
|
274 |
if message_history is None:
|
275 |
-
return [
|
|
|
|
|
|
|
276 |
else:
|
277 |
-
#
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
288 |
|
289 |
def clear_chat():
|
290 |
"""Clear the chat history"""
|
@@ -388,11 +459,30 @@ def main():
|
|
388 |
logger.info("Starting DamageScan 8B Instruct application")
|
389 |
logger.info(f"Environment: CUDA_VISIBLE_DEVICES={os.environ.get('CUDA_VISIBLE_DEVICES', 'Not set')}")
|
390 |
|
391 |
-
# Load model
|
392 |
-
model =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
393 |
|
394 |
# Build and launch Gradio interface
|
395 |
-
demo = build_gradio_interface(model)
|
396 |
|
397 |
# Launch the app
|
398 |
logger.info("Launching Gradio interface")
|
|
|
10 |
import gradio as gr
|
11 |
from transformers import (
|
12 |
AutoModelForCausalLM,
|
13 |
+
AutoTokenizer,
|
14 |
GenerationConfig
|
15 |
)
|
16 |
|
|
|
47 |
###############################################################################
|
48 |
# Model Loading with Error Handling
|
49 |
###############################################################################
|
50 |
+
def load_model_and_tokenizer():
|
51 |
"""Load the model with comprehensive error handling and logging"""
|
52 |
logger.info(f"Loading model: {MODEL_ID}")
|
53 |
logger.info(f"Transformers version: {transformers.__version__}")
|
|
|
61 |
}
|
62 |
logger.info(f"Device information: {device_info}")
|
63 |
|
64 |
+
# First try to load a base tokenizer for the pipeline - doesn't need to be perfect
|
65 |
+
try:
|
66 |
+
logger.info("Loading base Llama tokenizer for pipeline...")
|
67 |
+
# Use the base model's tokenizer, which should be compatible
|
68 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
69 |
+
"meta-llama/Llama-3.1-8B-Instruct",
|
70 |
+
trust_remote_code=True
|
71 |
+
)
|
72 |
+
logger.info(f"Base tokenizer loaded: {type(tokenizer).__name__}")
|
73 |
+
except Exception as e:
|
74 |
+
logger.warning(f"Could not load base tokenizer: {str(e)}")
|
75 |
+
logger.warning("Will try to initialize pipeline without explicit tokenizer")
|
76 |
+
tokenizer = None
|
77 |
+
|
78 |
# Load model with detailed error logging
|
79 |
try:
|
80 |
logger.info("Loading model...")
|
|
|
122 |
logger.error(traceback.format_exc())
|
123 |
raise RuntimeError(f"Failed to load model: {str(e)}")
|
124 |
|
125 |
+
return model, tokenizer
|
126 |
|
127 |
###############################################################################
|
128 |
+
# Direct Text Generation
|
129 |
###############################################################################
|
130 |
def format_prompt(messages, system_prompt=DEFAULT_SYSTEM_PROMPT):
|
131 |
"""
|
|
|
156 |
logger.info(f"Formatted prompt (length: {len(prompt)})")
|
157 |
return prompt
|
158 |
|
159 |
+
def generate_text(model, tokenizer, prompt, temperature=0.7, top_p=0.9, max_new_tokens=256):
|
160 |
"""
|
161 |
+
Generate text using the pipeline with explicit tokenizer.
|
|
|
162 |
"""
|
163 |
logger.info(f"Generating text with temp={temperature}, top_p={top_p}, max_tokens={max_new_tokens}")
|
164 |
|
|
|
165 |
try:
|
166 |
+
# Log what we're doing
|
167 |
+
logger.info(f"Input prompt length: {len(prompt)}")
|
168 |
|
169 |
+
# Generation config
|
170 |
gen_config = {
|
171 |
"temperature": temperature,
|
172 |
"top_p": top_p,
|
|
|
174 |
"max_new_tokens": max_new_tokens,
|
175 |
"repetition_penalty": 1.1,
|
176 |
}
|
|
|
|
|
|
|
177 |
logger.info(f"Generation config: {gen_config}")
|
178 |
|
179 |
+
# Create pipeline with explicit tokenizer if available
|
180 |
+
if tokenizer:
|
181 |
+
logger.info("Creating pipeline with explicit tokenizer")
|
182 |
+
pipe = transformers.pipeline(
|
183 |
+
"text-generation",
|
184 |
+
model=model,
|
185 |
+
tokenizer=tokenizer,
|
186 |
+
device_map=model.device_map if hasattr(model, "device_map") else "auto"
|
187 |
+
)
|
188 |
+
else:
|
189 |
+
# Fallback approach - try to create a direct generate function
|
190 |
+
logger.info("No tokenizer available, using direct model.generate")
|
191 |
+
|
192 |
+
# Simple direct generation
|
193 |
+
generation_start = time.time()
|
194 |
+
|
195 |
+
# Encode input with default settings
|
196 |
+
inputs = model.tokenize_using_default(prompt)
|
197 |
+
inputs = {k: v.to(model.device) if torch.is_tensor(v) else v for k, v in inputs.items()}
|
198 |
+
|
199 |
+
# Generate with model directly
|
200 |
+
with torch.no_grad():
|
201 |
+
outputs = model.generate(
|
202 |
+
**inputs,
|
203 |
+
**gen_config
|
204 |
+
)
|
205 |
+
|
206 |
+
# Decode using model's default
|
207 |
+
generated_text = model.decode_using_default(outputs[0])
|
208 |
+
|
209 |
+
generation_time = time.time() - generation_start
|
210 |
+
logger.info(f"Direct generation completed in {generation_time:.2f} seconds")
|
211 |
+
|
212 |
+
# Extract just the new text
|
213 |
+
response = generated_text[len(prompt):].strip()
|
214 |
+
logger.info(f"Generated response length: {len(response)}")
|
215 |
+
|
216 |
+
return response
|
217 |
|
218 |
+
# Normal pipeline-based generation
|
219 |
generation_start = time.time()
|
220 |
outputs = pipe(
|
221 |
prompt,
|
|
|
223 |
**gen_config
|
224 |
)
|
225 |
generation_time = time.time() - generation_start
|
226 |
+
logger.info(f"Pipeline generation completed in {generation_time:.2f} seconds")
|
227 |
|
228 |
# Extract the generated text
|
229 |
generated_text = outputs[0]["generated_text"]
|
|
|
237 |
except Exception as e:
|
238 |
logger.error(f"Error in generate_text: {e}")
|
239 |
logger.error(traceback.format_exc())
|
240 |
+
|
241 |
+
# Try one more fallback approach with manual text generation
|
242 |
+
try:
|
243 |
+
logger.info("Trying fallback manual text generation approach")
|
244 |
+
|
245 |
+
# Very minimal approach - just return a message
|
246 |
+
return "I'm having trouble generating a response right now. Please try again with different parameters or a different question."
|
247 |
+
|
248 |
+
except Exception as e2:
|
249 |
+
logger.error(f"Fallback approach also failed: {e2}")
|
250 |
+
return "I encountered an error while generating a response. Please try again."
|
251 |
|
252 |
###############################################################################
|
253 |
# Gradio Interface
|
254 |
###############################################################################
|
255 |
+
def build_gradio_interface(model, tokenizer):
|
256 |
"""Build and launch the Gradio interface"""
|
257 |
logger.info("Building Gradio interface")
|
258 |
|
|
|
291 |
# Generate response
|
292 |
assistant_response = generate_text(
|
293 |
model,
|
294 |
+
tokenizer,
|
295 |
prompt,
|
296 |
temperature=temp,
|
297 |
top_p=top_p,
|
298 |
max_new_tokens=max_tokens
|
299 |
)
|
300 |
|
301 |
+
# Add assistant message to formatted history
|
302 |
+
formatted_history.append({"role": "assistant", "content": assistant_response})
|
303 |
+
|
304 |
+
# Convert back to format expected by Gradio's Chatbot with type="messages"
|
305 |
+
# For type="messages", we need a list of dicts with role/content keys
|
306 |
+
display_history = []
|
307 |
for msg in formatted_history:
|
308 |
if msg["role"] == "system":
|
309 |
+
continue # Skip system messages
|
310 |
+
display_history.append({"role": msg["role"], "content": msg["content"]})
|
|
|
311 |
|
|
|
|
|
312 |
logger.info(f"Added assistant response (length: {len(assistant_response)})")
|
313 |
|
314 |
# Optimize memory after generation
|
315 |
optimize_memory()
|
316 |
|
317 |
+
return display_history, ""
|
318 |
|
319 |
except Exception as e:
|
320 |
logger.error(f"Error in user_submit: {str(e)}")
|
|
|
323 |
# Return original message history plus error message
|
324 |
error_msg = "I encountered an error processing your request. Please try again."
|
325 |
|
326 |
+
# Create error messages in the correct format
|
327 |
if message_history is None:
|
328 |
+
return [
|
329 |
+
{"role": "user", "content": user_text},
|
330 |
+
{"role": "assistant", "content": error_msg}
|
331 |
+
], ""
|
332 |
else:
|
333 |
+
# Try to safely convert to message format
|
334 |
+
try:
|
335 |
+
# If already in dict format, just append
|
336 |
+
if message_history and isinstance(message_history[0], dict):
|
337 |
+
message_history.append({"role": "user", "content": user_text})
|
338 |
+
message_history.append({"role": "assistant", "content": error_msg})
|
339 |
+
# If in tuple format, convert to dict format
|
340 |
+
else:
|
341 |
+
new_history = []
|
342 |
+
for msg in message_history:
|
343 |
+
if isinstance(msg, tuple):
|
344 |
+
role = "user" if msg[0] == "user" else "assistant"
|
345 |
+
new_history.append({"role": role, "content": msg[1]})
|
346 |
+
else:
|
347 |
+
new_history.append(msg)
|
348 |
+
new_history.append({"role": "user", "content": user_text})
|
349 |
+
new_history.append({"role": "assistant", "content": error_msg})
|
350 |
+
message_history = new_history
|
351 |
+
|
352 |
+
return message_history, ""
|
353 |
+
except:
|
354 |
+
# Last resort fallback
|
355 |
+
return [
|
356 |
+
{"role": "user", "content": user_text},
|
357 |
+
{"role": "assistant", "content": error_msg}
|
358 |
+
], ""
|
359 |
|
360 |
def clear_chat():
|
361 |
"""Clear the chat history"""
|
|
|
459 |
logger.info("Starting DamageScan 8B Instruct application")
|
460 |
logger.info(f"Environment: CUDA_VISIBLE_DEVICES={os.environ.get('CUDA_VISIBLE_DEVICES', 'Not set')}")
|
461 |
|
462 |
+
# Load model and tokenizer
|
463 |
+
model, tokenizer = load_model_and_tokenizer()
|
464 |
+
|
465 |
+
# Add manual tokenization methods to model if they don't exist
|
466 |
+
if not hasattr(model, "tokenize_using_default"):
|
467 |
+
logger.info("Adding default tokenization methods to model")
|
468 |
+
|
469 |
+
def tokenize_using_default(text):
|
470 |
+
"""Very basic tokenization that just returns a dummy"""
|
471 |
+
logger.info("Using minimal default tokenization")
|
472 |
+
# Return dummy input_ids - this is a last resort
|
473 |
+
return {"input_ids": torch.tensor([[1]]).to(model.device)}
|
474 |
+
|
475 |
+
def decode_using_default(token_ids):
|
476 |
+
"""Very basic decoding that just returns a message"""
|
477 |
+
logger.info("Using minimal default decoding")
|
478 |
+
return "I'm having trouble generating a proper response."
|
479 |
+
|
480 |
+
# Add methods to model
|
481 |
+
model.tokenize_using_default = tokenize_using_default
|
482 |
+
model.decode_using_default = decode_using_default
|
483 |
|
484 |
# Build and launch Gradio interface
|
485 |
+
demo = build_gradio_interface(model, tokenizer)
|
486 |
|
487 |
# Launch the app
|
488 |
logger.info("Launching Gradio interface")
|