CamiloVega commited on
Commit
9c9c33d
verified
1 Parent(s): 5e275fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -12
app.py CHANGED
@@ -86,20 +86,20 @@ def generate_response(user_input, chat_history):
86
  logger.info("Generating response for user input...")
87
  global total_water_consumption
88
 
89
- # Calcula el consumo de agua para la entrada
90
  input_water_consumption = calculate_water_consumption(user_input, True)
91
  total_water_consumption += input_water_consumption
92
 
93
- # Construye el historial de conversaci贸n sin etiquetas `[INST]`
94
  conversation_history = ""
95
  if chat_history:
96
  for message in chat_history:
 
97
  user_msg = message[0].replace("[INST]", "").replace("[/INST]", "").strip()
98
  assistant_msg = message[1].replace("[INST]", "").replace("[/INST]", "").strip()
99
- conversation_history += f"{user_msg} {assistant_msg} "
100
-
101
- # Crea el prompt sin las etiquetas `[INST]`
102
- prompt = f"{system_message}\n\n{conversation_history}{user_input}"
103
 
104
  logger.info("Generating model response...")
105
  outputs = model_gen(
@@ -107,20 +107,41 @@ def generate_response(user_input, chat_history):
107
  max_new_tokens=256,
108
  return_full_text=False,
109
  pad_token_id=tokenizer.eos_token_id,
 
 
 
 
110
  )
111
  logger.info("Model response generated successfully")
112
 
113
- # Limpia la salida del modelo
114
- assistant_response = outputs[0]['generated_text'].replace("[INST]", "").replace("[/INST]", "").strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
- # Calcula el consumo de agua para la salida
117
  output_water_consumption = calculate_water_consumption(assistant_response, False)
118
  total_water_consumption += output_water_consumption
119
 
120
- # Actualiza el historial de chat
121
  chat_history.append([user_input, assistant_response])
122
 
123
- # Prepara el mensaje de consumo de agua
124
  water_message = f"""
125
  <div style="position: fixed; top: 20px; right: 20px;
126
  background-color: white; padding: 15px;
@@ -143,7 +164,6 @@ def generate_response(user_input, chat_history):
143
  chat_history.append([user_input, error_message])
144
  return chat_history, show_water
145
 
146
-
147
  # Constants for water consumption calculation
148
  WATER_PER_TOKEN = {
149
  "input_training": 0.0000309,
 
86
  logger.info("Generating response for user input...")
87
  global total_water_consumption
88
 
89
+ # Calculate water consumption for input
90
  input_water_consumption = calculate_water_consumption(user_input, True)
91
  total_water_consumption += input_water_consumption
92
 
93
+ # Create prompt with Llama 2 chat format
94
  conversation_history = ""
95
  if chat_history:
96
  for message in chat_history:
97
+ # Remove any [INST] tags from the history
98
  user_msg = message[0].replace("[INST]", "").replace("[/INST]", "").strip()
99
  assistant_msg = message[1].replace("[INST]", "").replace("[/INST]", "").strip()
100
+ conversation_history += f"[INST] {user_msg} [/INST] {assistant_msg} "
101
+
102
+ prompt = f"<s>[INST] {system_message}\n\n{conversation_history}[INST] {user_input} [/INST]"
 
103
 
104
  logger.info("Generating model response...")
105
  outputs = model_gen(
 
107
  max_new_tokens=256,
108
  return_full_text=False,
109
  pad_token_id=tokenizer.eos_token_id,
110
+ do_sample=True,
111
+ temperature=0.7,
112
+ top_p=0.9,
113
+ repetition_penalty=1.1
114
  )
115
  logger.info("Model response generated successfully")
116
 
117
+ # Clean up the response by removing any [INST] tags and trimming
118
+ assistant_response = outputs[0]['generated_text'].strip()
119
+ assistant_response = assistant_response.replace("[INST]", "").replace("[/INST]", "").strip()
120
+
121
+ # If the response is too short, try to generate a more detailed one
122
+ if len(assistant_response.split()) < 10:
123
+ prompt += "\nPlease provide a more detailed answer with context and explanation."
124
+ outputs = model_gen(
125
+ prompt,
126
+ max_new_tokens=256,
127
+ return_full_text=False,
128
+ pad_token_id=tokenizer.eos_token_id,
129
+ do_sample=True,
130
+ temperature=0.7,
131
+ top_p=0.9,
132
+ repetition_penalty=1.1
133
+ )
134
+ assistant_response = outputs[0]['generated_text'].strip()
135
+ assistant_response = assistant_response.replace("[INST]", "").replace("[/INST]", "").strip()
136
 
137
+ # Calculate water consumption for output
138
  output_water_consumption = calculate_water_consumption(assistant_response, False)
139
  total_water_consumption += output_water_consumption
140
 
141
+ # Update chat history with the cleaned messages
142
  chat_history.append([user_input, assistant_response])
143
 
144
+ # Prepare water consumption message
145
  water_message = f"""
146
  <div style="position: fixed; top: 20px; right: 20px;
147
  background-color: white; padding: 15px;
 
164
  chat_history.append([user_input, error_message])
165
  return chat_history, show_water
166
 
 
167
  # Constants for water consumption calculation
168
  WATER_PER_TOKEN = {
169
  "input_training": 0.0000309,