anaspro commited on
Commit
dfb1538
·
1 Parent(s): dc0dce5
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -48,18 +48,28 @@ def get_response(text, tokenizer=tokenizer, model=model):
48
  generate_ids = model.generate(
49
  input_ids,
50
  attention_mask=attention_mask,
51
- top_p=0.9,
52
- temperature=0.3,
53
- max_length=2048,
54
  min_length=input_len + 4,
55
- repetition_penalty=1.2,
56
  do_sample=True,
57
- pad_token_id=tokenizer.pad_token_id
 
58
  )
59
  response = tokenizer.batch_decode(
60
  generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
61
  )[0]
62
  response = response.split("### Response :")[-1].lstrip()
 
 
 
 
 
 
 
 
 
63
  return response
64
 
65
  def format_conversation_history(chat_history):
@@ -110,13 +120,13 @@ def generate_response(input_data, chat_history, max_new_tokens, temperature, top
110
 
111
  # Add current user message
112
  conversation_parts.append(f"[|Human|] {input_data}")
113
- conversation_parts.append("[|AI|]")
114
 
115
  # Join conversation
116
  conversation = "\n".join(conversation_parts)
117
 
118
  # Create full prompt using Jais format with our system prompt
119
- full_prompt = f"### Instruction:{system_instruction}\n### Input:{conversation}\n### Response :"
 
120
 
121
  try:
122
  # استخدام دالة get_response من documentation
 
48
  generate_ids = model.generate(
49
  input_ids,
50
  attention_mask=attention_mask,
51
+ top_p=0.8,
52
+ temperature=0.2,
53
+ max_length=input_len + 256, # Limit response length to prevent multiple responses
54
  min_length=input_len + 4,
55
+ repetition_penalty=1.3,
56
  do_sample=True,
57
+ pad_token_id=tokenizer.pad_token_id,
58
+ eos_token_id=tokenizer.eos_token_id # Stop at end of sentence
59
  )
60
  response = tokenizer.batch_decode(
61
  generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
62
  )[0]
63
  response = response.split("### Response :")[-1].lstrip()
64
+
65
+ # Extract only the first AI response to prevent multiple responses
66
+ if "[|AI|]" in response and "[|Human|]" in response:
67
+ # If there are multiple turns, take only the first AI response
68
+ response = response.split("[|Human|]")[0].strip()
69
+ elif "[|AI|]" in response:
70
+ # Remove the [|AI|] marker from the beginning
71
+ response = response.replace("[|AI|]", "").strip()
72
+
73
  return response
74
 
75
  def format_conversation_history(chat_history):
 
120
 
121
  # Add current user message
122
  conversation_parts.append(f"[|Human|] {input_data}")
 
123
 
124
  # Join conversation
125
  conversation = "\n".join(conversation_parts)
126
 
127
  # Create full prompt using Jais format with our system prompt
128
+ # Note: We don't add "[|AI|]" at the end to prevent the model from generating multiple responses
129
+ full_prompt = f"### Instruction:{system_instruction}\n### Input:{conversation}\n[|AI|]\n### Response :"
130
 
131
  try:
132
  # استخدام دالة get_response من documentation