DeepMount00 commited on
Commit
16cd8af
1 Parent(s): 31fe49a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -36,11 +36,19 @@ h1 {
36
  def chat_llama3_8b(message: str, history: list, temperature: float, max_new_tokens: int) -> str:
37
  # Initialize the conversation with a system prompt
38
  conversation = [{"role": "system", "content": "Sei un assistente specializzato nella lingua italiana."}]
39
-
40
- # Add historical conversation
41
- for user, assistant in history:
42
- conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
43
-
 
 
 
 
 
 
 
 
44
  # Add the current user message to the conversation
45
  conversation.append({"role": "user", "content": message})
46
 
@@ -59,13 +67,11 @@ def chat_llama3_8b(message: str, history: list, temperature: float, max_new_toke
59
  temperature=real_temperature,
60
  eos_token_id=tokenizer.eos_token_id
61
  )
 
 
 
62
 
63
- # Decode the generated tokens
64
- decoded = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
65
- prompt_end_index = decoded[0].find(message) + len(message)
66
- final_response = decoded[0][prompt_end_index:] if prompt_end_index != -1 else decoded[0]
67
-
68
- final_response = final_response.strip("assistant")
69
  if final_response.startswith(':'):
70
  final_response = final_response.lstrip(':').strip()
71
 
 
36
  def chat_llama3_8b(message: str, history: list, temperature: float, max_new_tokens: int) -> str:
37
  # Initialize the conversation with a system prompt
38
  conversation = [{"role": "system", "content": "Sei un assistente specializzato nella lingua italiana."}]
39
+
40
+ flat_history = [item for sublist in history for item in sublist]
41
+
42
+ if len(flat_history) > 16:
43
+ flat_history = flat_history[-16:]
44
+
45
+ # Rebuild the conversation from the trimmed history
46
+ for i in range(0, len(flat_history), 2):
47
+ conversation.extend([
48
+ {"role": "user", "content": flat_history[i]},
49
+ {"role": "assistant", "content": flat_history[i + 1]}
50
+ ])
51
+
52
  # Add the current user message to the conversation
53
  conversation.append({"role": "user", "content": message})
54
 
 
67
  temperature=real_temperature,
68
  eos_token_id=tokenizer.eos_token_id
69
  )
70
+ input_length = input_ids.size(1)
71
+ new_tokens = generated_ids[:, input_length:]
72
+ decoded = tokenizer.batch_decode(new_tokens, skip_special_tokens=True)[0]
73
 
74
+ final_response = decoded.strip("assistant")
 
 
 
 
 
75
  if final_response.startswith(':'):
76
  final_response = final_response.lstrip(':').strip()
77