artificialguybr commited on
Commit
70a5709
1 Parent(s): acc1fb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -22
app.py CHANGED
@@ -32,36 +32,40 @@ def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetit
32
  history = history or []
33
 
34
  # A última mensagem do usuário
35
- user_prompt = history[-1][0]
36
 
37
- # Prepare the messages
38
- messages = [
39
- {"role": "system", "content": system_message.strip()},
40
- {"role": "user", "content": user_prompt}
41
- ]
 
 
 
42
 
43
- # Apply the chat template
44
- gen_input = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True)
45
-
46
- # Debug: Print the shape of gen_input
47
- print("Shape of gen_input:", gen_input.shape if hasattr(gen_input, 'shape') else type(gen_input))
48
-
49
- # Extract input_ids based on the type of gen_input
50
- input_ids = gen_input['input_ids'] if isinstance(gen_input, dict) else gen_input
51
-
52
- # Generate the output
53
- output = model.generate(input_ids=input_ids, temperature=temperature, do_sample=True, top_p=top_p, top_k=top_k, max_length=max_tokens)
54
 
55
- # Decode the output
56
- decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
 
57
 
58
- # Update the history
59
- history[-1][1] += decoded_output.split('\n')[-1] # Only take the assistant's last response
 
 
 
60
 
61
  return history, history, ""
62
 
63
 
64
-
65
  start_message = ""
66
 
67
  CSS ="""
 
32
  history = history or []
33
 
34
  # A última mensagem do usuário
35
+ user_prompt = history[-1][0] if history else ""
36
 
37
+ # Preparar a entrada para o modelo
38
+ prompt_template = f'''system
39
+ {system_message.strip()}
40
+ user
41
+ {user_prompt}
42
+ assistant
43
+ '''
44
+ input_ids = tokenizer(prompt_template, return_tensors='pt').input_ids
45
 
46
+ # Gerar a saída
47
+ output = model.generate(
48
+ input_ids=input_ids,
49
+ max_length=max_tokens,
50
+ temperature=temperature,
51
+ top_p=top_p,
52
+ top_k=top_k,
53
+ repetition_penalty=repetition_penalty
54
+ )
 
 
55
 
56
+ # Decodificar a saída
57
+ decoded_output = tokenizer.decode(output[0])
58
+ assistant_response = decoded_output.split('assistant')[-1].strip() # Pegar apenas a última resposta do assistente
59
 
60
+ # Atualizar o histórico
61
+ if history:
62
+ history[-1][1] += assistant_response
63
+ else:
64
+ history.append(["", assistant_response])
65
 
66
  return history, history, ""
67
 
68
 
 
69
  start_message = ""
70
 
71
  CSS ="""