anaspro
commited on
Commit
·
6da46a3
1
Parent(s):
bd45f32
update
Browse files
app.py
CHANGED
|
@@ -96,6 +96,33 @@ def format_conversation_history(chat_history):
|
|
| 96 |
|
| 97 |
@spaces.GPU()
|
| 98 |
def generate_response(input_data, chat_history, max_new_tokens, temperature, top_p, top_k, repetition_penalty):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
# Build messages for Llama chat template
|
| 100 |
messages = [{"role": "system", "content": DEFAULT_SYSTEM_PROMPT}]
|
| 101 |
|
|
@@ -113,6 +140,7 @@ def generate_response(input_data, chat_history, max_new_tokens, temperature, top
|
|
| 113 |
# محاولة استخدام chat template
|
| 114 |
if hasattr(tokenizer, 'apply_chat_template') and tokenizer.chat_template is not None:
|
| 115 |
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
|
|
|
| 116 |
else:
|
| 117 |
# Fallback format
|
| 118 |
prompt = f"System: {DEFAULT_SYSTEM_PROMPT}\n\n"
|
|
@@ -122,8 +150,12 @@ def generate_response(input_data, chat_history, max_new_tokens, temperature, top
|
|
| 122 |
elif msg["role"] == "assistant":
|
| 123 |
prompt += f"Assistant: {msg['content']}\n"
|
| 124 |
prompt += "Assistant:"
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
|
| 127 |
|
| 128 |
# استخدام generate مع parameters أساسية وآمنة
|
| 129 |
with torch.no_grad():
|
|
@@ -138,18 +170,30 @@ def generate_response(input_data, chat_history, max_new_tokens, temperature, top
|
|
| 138 |
output_scores=False,
|
| 139 |
)
|
| 140 |
|
|
|
|
|
|
|
|
|
|
| 141 |
response = tokenizer.decode(outputs.sequences[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 142 |
response = response.strip()
|
| 143 |
|
|
|
|
|
|
|
|
|
|
| 144 |
if not response:
|
| 145 |
-
response
|
|
|
|
| 146 |
|
| 147 |
yield response
|
| 148 |
|
| 149 |
except Exception as e:
|
| 150 |
error_msg = f"خطأ في التوليد: {str(e)}"
|
| 151 |
print(error_msg)
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
demo = gr.ChatInterface(
|
| 155 |
fn=generate_response,
|
|
|
|
| 96 |
|
| 97 |
@spaces.GPU()
|
| 98 |
def generate_response(input_data, chat_history, max_new_tokens, temperature, top_p, top_k, repetition_penalty):
|
| 99 |
+
# Test بسيط أولاً
|
| 100 |
+
try:
|
| 101 |
+
# رسالة test بسيطة
|
| 102 |
+
test_prompt = "السلام عليكم"
|
| 103 |
+
inputs = tokenizer(test_prompt, return_tensors="pt").to(model.device)
|
| 104 |
+
|
| 105 |
+
print(f"Input shape: {inputs.input_ids.shape}") # Debug
|
| 106 |
+
print(f"Input tokens: {inputs.input_ids[0][:10]}") # Debug
|
| 107 |
+
|
| 108 |
+
with torch.no_grad():
|
| 109 |
+
outputs = model.generate(
|
| 110 |
+
**inputs,
|
| 111 |
+
max_new_tokens=50, # قصير للاختبار
|
| 112 |
+
do_sample=False,
|
| 113 |
+
num_beams=1,
|
| 114 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 115 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
test_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 119 |
+
print(f"Test response: {test_response}") # Debug
|
| 120 |
+
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print(f"Test failed: {e}")
|
| 123 |
+
import traceback
|
| 124 |
+
print(traceback.format_exc())
|
| 125 |
+
|
| 126 |
# Build messages for Llama chat template
|
| 127 |
messages = [{"role": "system", "content": DEFAULT_SYSTEM_PROMPT}]
|
| 128 |
|
|
|
|
| 140 |
# محاولة استخدام chat template
|
| 141 |
if hasattr(tokenizer, 'apply_chat_template') and tokenizer.chat_template is not None:
|
| 142 |
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 143 |
+
print(f"Using chat template. Prompt length: {len(prompt)}") # Debug
|
| 144 |
else:
|
| 145 |
# Fallback format
|
| 146 |
prompt = f"System: {DEFAULT_SYSTEM_PROMPT}\n\n"
|
|
|
|
| 150 |
elif msg["role"] == "assistant":
|
| 151 |
prompt += f"Assistant: {msg['content']}\n"
|
| 152 |
prompt += "Assistant:"
|
| 153 |
+
print(f"Using fallback format. Prompt length: {len(prompt)}") # Debug
|
| 154 |
+
|
| 155 |
+
print(f"Final prompt: {prompt[:200]}...") # Debug first 200 chars
|
| 156 |
|
| 157 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 158 |
+
print(f"Tokenized input shape: {inputs.input_ids.shape}") # Debug
|
| 159 |
|
| 160 |
# استخدام generate مع parameters أساسية وآمنة
|
| 161 |
with torch.no_grad():
|
|
|
|
| 170 |
output_scores=False,
|
| 171 |
)
|
| 172 |
|
| 173 |
+
print(f"Generated sequence shape: {outputs.sequences.shape}") # Debug
|
| 174 |
+
print(f"Input length: {inputs.input_ids.shape[1]}") # Debug
|
| 175 |
+
|
| 176 |
response = tokenizer.decode(outputs.sequences[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 177 |
response = response.strip()
|
| 178 |
|
| 179 |
+
print(f"Generated response length: {len(response)}") # Debug
|
| 180 |
+
print(f"Response preview: {response[:100]}...") # Debug
|
| 181 |
+
|
| 182 |
if not response:
|
| 183 |
+
print("Empty response, using fallback") # Debug
|
| 184 |
+
response = "أهلاً! أنا أليكس مساعد خدمة العملاء. كيف أقدر أساعدك اليوم؟"
|
| 185 |
|
| 186 |
yield response
|
| 187 |
|
| 188 |
except Exception as e:
|
| 189 |
error_msg = f"خطأ في التوليد: {str(e)}"
|
| 190 |
print(error_msg)
|
| 191 |
+
print(f"Error type: {type(e)}") # Debug
|
| 192 |
+
import traceback
|
| 193 |
+
print("Traceback:")
|
| 194 |
+
print(traceback.format_exc()) # Debug
|
| 195 |
+
|
| 196 |
+
yield "أهلاً! أنا أليكس مساعد خدمة العملاء. كيف أقدر أساعدك اليوم؟"
|
| 197 |
|
| 198 |
demo = gr.ChatInterface(
|
| 199 |
fn=generate_response,
|