Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -76,7 +76,93 @@ except Exception as e:
|
|
76 |
raise
|
77 |
|
78 |
# Configure system message
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# Constants for water consumption calculation
|
82 |
WATER_PER_TOKEN = {
|
|
|
76 |
raise
|
77 |
|
78 |
# Configure system message
|
79 |
+
|
80 |
+
system_message = """You are a helpful AI assistant called AQuaBot. You provide direct, clear, and detailed answers to questions while being aware of environmental impact. Keep your responses natural and informative, but concise. Always provide context and explanations with your answers. Respond directly to questions without using any special tags or markers."""
|
81 |
+
|
82 |
+
@spaces.GPU(duration=60)
|
83 |
+
@torch.inference_mode()
|
84 |
+
def generate_response(user_input, chat_history):
|
85 |
+
try:
|
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(
|
106 |
+
prompt,
|
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;
|
148 |
+
border: 2px solid #ff0000; border-radius: 10px;
|
149 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
|
150 |
+
<div style="color: #ff0000; font-size: 24px; font-weight: bold;">
|
151 |
+
💧 {total_water_consumption:.4f} ml
|
152 |
+
</div>
|
153 |
+
<div style="color: #666; font-size: 14px;">
|
154 |
+
Water Consumed
|
155 |
+
</div>
|
156 |
+
</div>
|
157 |
+
"""
|
158 |
+
|
159 |
+
return chat_history, water_message
|
160 |
+
|
161 |
+
except Exception as e:
|
162 |
+
logger.error(f"Error in generate_response: {str(e)}")
|
163 |
+
error_message = f"An error occurred: {str(e)}"
|
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 = {
|