anaspro
commited on
Commit
·
2f06f2b
1
Parent(s):
1592223
updatE
Browse files
app.py
CHANGED
|
@@ -34,11 +34,27 @@ pipeline_model = pipeline(
|
|
| 34 |
def generate_with_pipeline(messages, max_new_tokens=256, temperature=0.7, top_p=0.9, top_k=50, repetition_penalty=1.0):
|
| 35 |
"""Generate response using the pipeline with messages format"""
|
| 36 |
# Apply chat template
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
outputs = pipeline_model(
|
| 44 |
prompt,
|
|
@@ -64,26 +80,34 @@ def generate_response(message, history, max_new_tokens, temperature, top_p, top_
|
|
| 64 |
max_new_tokens, temperature, top_p, top_k, repetition_penalty: Generation parameters
|
| 65 |
"""
|
| 66 |
try:
|
| 67 |
-
# Build messages list
|
| 68 |
-
messages = [
|
| 69 |
-
|
| 70 |
-
# Add
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
if history:
|
| 73 |
for msg in history:
|
| 74 |
if isinstance(msg, dict) and 'role' in msg and 'content' in msg:
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# Add current user message
|
| 78 |
if isinstance(message, dict):
|
| 79 |
current_message = message.get("text", "") or message.get("content", "")
|
| 80 |
else:
|
| 81 |
current_message = str(message)
|
| 82 |
-
|
| 83 |
messages.append({"role": "user", "content": current_message})
|
| 84 |
|
| 85 |
# Debug: print messages structure
|
| 86 |
print(f"Messages sent to model: {len(messages)} messages")
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Generate response
|
| 89 |
response = generate_with_pipeline(
|
|
|
|
| 34 |
def generate_with_pipeline(messages, max_new_tokens=256, temperature=0.7, top_p=0.9, top_k=50, repetition_penalty=1.0):
|
| 35 |
"""Generate response using the pipeline with messages format"""
|
| 36 |
# Apply chat template
|
| 37 |
+
try:
|
| 38 |
+
prompt = pipeline_model.tokenizer.apply_chat_template(
|
| 39 |
+
messages,
|
| 40 |
+
tokenize=False,
|
| 41 |
+
add_generation_prompt=True
|
| 42 |
+
)
|
| 43 |
+
except Exception as template_error:
|
| 44 |
+
print(f"Template application error: {template_error}")
|
| 45 |
+
# Fallback: manually format messages
|
| 46 |
+
prompt = ""
|
| 47 |
+
for msg in messages:
|
| 48 |
+
if msg['role'] == 'system':
|
| 49 |
+
prompt += f"System: {msg['content']}\n\n"
|
| 50 |
+
elif msg['role'] == 'user':
|
| 51 |
+
prompt += f"User: {msg['content']}\n"
|
| 52 |
+
elif msg['role'] == 'assistant':
|
| 53 |
+
prompt += f"Assistant: {msg['content']}\n"
|
| 54 |
+
prompt += "Assistant: "
|
| 55 |
+
|
| 56 |
+
# Debug: print final prompt
|
| 57 |
+
print(f"Final prompt preview: {prompt[:200]}...")
|
| 58 |
|
| 59 |
outputs = pipeline_model(
|
| 60 |
prompt,
|
|
|
|
| 80 |
max_new_tokens, temperature, top_p, top_k, repetition_penalty: Generation parameters
|
| 81 |
"""
|
| 82 |
try:
|
| 83 |
+
# Build messages list - Gemma template expects alternating user/assistant after system
|
| 84 |
+
messages = []
|
| 85 |
+
|
| 86 |
+
# Add system message first (will be handled specially by the template)
|
| 87 |
+
messages.append({"role": "system", "content": DEFAULT_SYSTEM_PROMPT})
|
| 88 |
+
|
| 89 |
+
# Add conversation history (ensure alternating user/assistant)
|
| 90 |
if history:
|
| 91 |
for msg in history:
|
| 92 |
if isinstance(msg, dict) and 'role' in msg and 'content' in msg:
|
| 93 |
+
# Convert 'assistant' role to 'model' for Gemma template if needed
|
| 94 |
+
role = msg['role']
|
| 95 |
+
if role == 'assistant':
|
| 96 |
+
role = 'assistant' # Keep as assistant, template converts to 'model'
|
| 97 |
+
messages.append({"role": role, "content": msg['content']})
|
| 98 |
+
|
| 99 |
# Add current user message
|
| 100 |
if isinstance(message, dict):
|
| 101 |
current_message = message.get("text", "") or message.get("content", "")
|
| 102 |
else:
|
| 103 |
current_message = str(message)
|
| 104 |
+
|
| 105 |
messages.append({"role": "user", "content": current_message})
|
| 106 |
|
| 107 |
# Debug: print messages structure
|
| 108 |
print(f"Messages sent to model: {len(messages)} messages")
|
| 109 |
+
for i, msg in enumerate(messages):
|
| 110 |
+
print(f" Message {i}: role={msg['role']}, content_preview={msg['content'][:50]}...")
|
| 111 |
|
| 112 |
# Generate response
|
| 113 |
response = generate_with_pipeline(
|