Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -71,13 +71,24 @@ except Exception as e:
|
|
71 |
logger.error(f"Error during initialization: {str(e)}")
|
72 |
raise
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
@spaces.GPU(duration=60)
|
83 |
@torch.inference_mode()
|
@@ -90,14 +101,21 @@ def generate_response(user_input, chat_history):
|
|
90 |
input_water_consumption = calculate_water_consumption(user_input, True)
|
91 |
total_water_consumption += input_water_consumption
|
92 |
|
93 |
-
#
|
94 |
-
|
95 |
if chat_history:
|
96 |
-
for
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
|
102 |
logger.info("Generating model response...")
|
103 |
outputs = model_gen(
|
@@ -105,12 +123,23 @@ def generate_response(user_input, chat_history):
|
|
105 |
max_new_tokens=512,
|
106 |
return_full_text=False,
|
107 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
|
|
|
|
|
|
108 |
)
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
assistant_response =
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
# Calculate water consumption for output
|
116 |
output_water_consumption = calculate_water_consumption(assistant_response, False)
|
@@ -119,7 +148,7 @@ def generate_response(user_input, chat_history):
|
|
119 |
# Update chat history
|
120 |
chat_history.append([user_input, assistant_response])
|
121 |
|
122 |
-
#
|
123 |
water_message = f"""
|
124 |
<div style="position: fixed; top: 20px; right: 20px;
|
125 |
background-color: white; padding: 15px;
|
@@ -142,6 +171,19 @@ def generate_response(user_input, chat_history):
|
|
142 |
chat_history.append([user_input, error_message])
|
143 |
return chat_history, show_water
|
144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
# Constants for water consumption calculation
|
147 |
WATER_PER_TOKEN = {
|
|
|
71 |
logger.error(f"Error during initialization: {str(e)}")
|
72 |
raise
|
73 |
|
74 |
+
|
75 |
+
|
76 |
+
@spaces.GPU(duration=60)
|
77 |
+
@torch.inference_mode()
|
78 |
+
def clean_response(text):
|
79 |
+
"""Limpia la respuesta del modelo eliminando etiquetas y texto no deseado"""
|
80 |
+
# Eliminar etiquetas INST y wikipedia references
|
81 |
+
text = text.replace('[INST]', '').replace('[/INST]', '')
|
82 |
+
text = text.replace('(You can find more about it at wikipedia)', '')
|
83 |
+
|
84 |
+
# Eliminar cualquier texto que comience con "User:" o "Assistant:"
|
85 |
+
lines = text.split('\n')
|
86 |
+
cleaned_lines = []
|
87 |
+
for line in lines:
|
88 |
+
if not line.strip().startswith(('User:', 'Assistant:', 'Human:', 'AI:')):
|
89 |
+
cleaned_lines.append(line)
|
90 |
+
|
91 |
+
return '\n'.join(cleaned_lines).strip()
|
92 |
|
93 |
@spaces.GPU(duration=60)
|
94 |
@torch.inference_mode()
|
|
|
101 |
input_water_consumption = calculate_water_consumption(user_input, True)
|
102 |
total_water_consumption += input_water_consumption
|
103 |
|
104 |
+
# Format conversation history without using INST tags
|
105 |
+
formatted_history = ""
|
106 |
if chat_history:
|
107 |
+
for prev_input, prev_response in chat_history:
|
108 |
+
formatted_history += f"Question: {prev_input}\nAnswer: {prev_response}\n\n"
|
109 |
+
|
110 |
+
# Create prompt using a más natural format
|
111 |
+
prompt = f"""
|
112 |
+
{system_message}
|
113 |
+
|
114 |
+
Previous conversation:
|
115 |
+
{formatted_history}
|
116 |
|
117 |
+
Question: {user_input}
|
118 |
+
Answer:"""
|
119 |
|
120 |
logger.info("Generating model response...")
|
121 |
outputs = model_gen(
|
|
|
123 |
max_new_tokens=512,
|
124 |
return_full_text=False,
|
125 |
pad_token_id=tokenizer.eos_token_id,
|
126 |
+
do_sample=True,
|
127 |
+
temperature=0.7,
|
128 |
+
top_p=0.9,
|
129 |
+
repetition_penalty=1.1
|
130 |
)
|
131 |
+
|
132 |
+
# Limpiar y procesar la respuesta
|
133 |
+
assistant_response = outputs[0]['generated_text']
|
134 |
+
assistant_response = clean_response(assistant_response)
|
135 |
+
|
136 |
+
# Si la respuesta sigue conteniendo texto no deseado, intentar extraer solo la parte relevante
|
137 |
+
if 'Question:' in assistant_response or 'Answer:' in assistant_response:
|
138 |
+
parts = assistant_response.split('Answer:')
|
139 |
+
if len(parts) > 1:
|
140 |
+
assistant_response = parts[1].split('Question:')[0].strip()
|
141 |
+
|
142 |
+
logger.info("Response cleaned and processed")
|
143 |
|
144 |
# Calculate water consumption for output
|
145 |
output_water_consumption = calculate_water_consumption(assistant_response, False)
|
|
|
148 |
# Update chat history
|
149 |
chat_history.append([user_input, assistant_response])
|
150 |
|
151 |
+
# Update water consumption display
|
152 |
water_message = f"""
|
153 |
<div style="position: fixed; top: 20px; right: 20px;
|
154 |
background-color: white; padding: 15px;
|
|
|
171 |
chat_history.append([user_input, error_message])
|
172 |
return chat_history, show_water
|
173 |
|
174 |
+
# Actualizar el system message para ser más específico sobre el formato
|
175 |
+
system_message = """You are AQuaBot, an AI assistant focused on providing accurate and environmentally conscious information.
|
176 |
+
|
177 |
+
Guidelines for your responses:
|
178 |
+
1. Provide direct, clear answers without any special tags or markers
|
179 |
+
2. Do not reference external sources like Wikipedia in your responses
|
180 |
+
3. Stay focused on the question asked
|
181 |
+
4. Be concise but informative
|
182 |
+
5. Be mindful of environmental impact
|
183 |
+
6. Use a natural, conversational tone
|
184 |
+
|
185 |
+
Remember: Never include [INST] tags or other technical markers in your responses."""
|
186 |
+
|
187 |
|
188 |
# Constants for water consumption calculation
|
189 |
WATER_PER_TOKEN = {
|