Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -258,65 +258,82 @@ def load_previous_user_request_from_github():
|
|
258 |
|
259 |
|
260 |
def get_reference_message(gender, generation, psychotype, business_stage, industry, legal_form):
|
261 |
-
# Import io if not already done
|
262 |
import io
|
263 |
-
|
264 |
-
# Define the repository and file path
|
265 |
repo = "fruitpicker01/Storage_dev"
|
266 |
-
file_path = "messages.csv"
|
267 |
-
|
268 |
-
# Get the file from the repository
|
269 |
url = f"https://api.github.com/repos/{repo}/contents/{file_path}"
|
270 |
headers = {
|
271 |
"Authorization": f"token {token}",
|
272 |
"Content-Type": "application/json"
|
273 |
}
|
274 |
-
|
275 |
response = requests.get(url, headers=headers)
|
276 |
-
|
277 |
if response.status_code == 200:
|
278 |
-
# File exists, download and load it
|
279 |
content = response.json()
|
280 |
file_content = base64.b64decode(content['content'])
|
281 |
-
|
282 |
-
df = pd.read_excel(io.BytesIO(file_content))
|
283 |
-
else:
|
284 |
-
df = pd.read_csv(io.StringIO(file_content.decode('utf-8')))
|
285 |
else:
|
286 |
-
# File does not exist or error
|
287 |
print(f"Error accessing the file: {response.status_code}")
|
288 |
return None
|
289 |
-
|
290 |
-
#
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
300 |
filtered_df = df[filter_condition]
|
301 |
-
|
302 |
if filtered_df.empty:
|
303 |
-
|
304 |
return None
|
305 |
-
|
306 |
-
# Sort
|
307 |
filtered_df = filtered_df.sort_values(by="Timestamp", ascending=False)
|
308 |
-
|
309 |
# Get the latest message
|
310 |
latest_row = filtered_df.iloc[0]
|
311 |
-
|
312 |
-
#
|
313 |
-
if pd.notnull(latest_row
|
314 |
reference_message = latest_row["Откорректированное сообщение"]
|
315 |
else:
|
316 |
-
reference_message = latest_row
|
317 |
-
|
318 |
return reference_message
|
319 |
|
|
|
320 |
def adapt_messages_to_best_example(
|
321 |
personalized_gigachat_pro,
|
322 |
personalized_gigachat_lite,
|
|
|
258 |
|
259 |
|
260 |
def get_reference_message(gender, generation, psychotype, business_stage, industry, legal_form):
|
|
|
261 |
import io
|
262 |
+
|
|
|
263 |
repo = "fruitpicker01/Storage_dev"
|
264 |
+
file_path = "messages.csv"
|
265 |
+
|
|
|
266 |
url = f"https://api.github.com/repos/{repo}/contents/{file_path}"
|
267 |
headers = {
|
268 |
"Authorization": f"token {token}",
|
269 |
"Content-Type": "application/json"
|
270 |
}
|
271 |
+
|
272 |
response = requests.get(url, headers=headers)
|
273 |
+
|
274 |
if response.status_code == 200:
|
|
|
275 |
content = response.json()
|
276 |
file_content = base64.b64decode(content['content'])
|
277 |
+
df = pd.read_csv(io.StringIO(file_content.decode('utf-8')))
|
|
|
|
|
|
|
278 |
else:
|
|
|
279 |
print(f"Error accessing the file: {response.status_code}")
|
280 |
return None
|
281 |
+
|
282 |
+
# Normalize the DataFrame columns
|
283 |
+
for col in ["Пол", "Поколение", "Психотип", "Стадия бизнеса", "Отрасль", "ОПФ"]:
|
284 |
+
df[col] = df[col].astype(str).str.strip().str.lower()
|
285 |
+
|
286 |
+
# Normalize the input parameters
|
287 |
+
params = {
|
288 |
+
"Пол": str(gender).strip().lower() if gender else None,
|
289 |
+
"Поколение": str(generation).strip().lower() if generation else None,
|
290 |
+
"Психотип": str(psychotype).strip().lower() if psychotype else None,
|
291 |
+
"Стадия бизнеса": str(business_stage).strip().lower() if business_stage else None,
|
292 |
+
"Отрасль": str(industry).strip().lower() if industry else None,
|
293 |
+
"ОПФ": str(legal_form).strip().lower() if legal_form else None
|
294 |
+
}
|
295 |
+
|
296 |
+
# Debugging: Print DataFrame and parameters
|
297 |
+
print("DataFrame columns:", df.columns)
|
298 |
+
print("DataFrame head:\n", df.head())
|
299 |
+
print("Personalization parameters:", params)
|
300 |
+
|
301 |
+
# Build the filter conditions dynamically
|
302 |
+
filter_conditions = []
|
303 |
+
for col, value in params.items():
|
304 |
+
if value and value.lower() != 'none':
|
305 |
+
filter_conditions.append(df[col] == value)
|
306 |
+
|
307 |
+
if not filter_conditions:
|
308 |
+
print("No personalization parameters provided.")
|
309 |
+
return None
|
310 |
+
|
311 |
+
# Combine filter conditions
|
312 |
+
filter_condition = filter_conditions[0]
|
313 |
+
for condition in filter_conditions[1:]:
|
314 |
+
filter_condition &= condition
|
315 |
+
|
316 |
filtered_df = df[filter_condition]
|
317 |
+
|
318 |
if filtered_df.empty:
|
319 |
+
print("No messages found with the given personalization parameters.")
|
320 |
return None
|
321 |
+
|
322 |
+
# Sort by Timestamp descending
|
323 |
filtered_df = filtered_df.sort_values(by="Timestamp", ascending=False)
|
324 |
+
|
325 |
# Get the latest message
|
326 |
latest_row = filtered_df.iloc[0]
|
327 |
+
|
328 |
+
# Choose the appropriate message
|
329 |
+
if pd.notnull(latest_row.get("Откорректированное сообщение", None)) and latest_row["Откорректированное сообщение"].strip():
|
330 |
reference_message = latest_row["Откорректированное сообщение"]
|
331 |
else:
|
332 |
+
reference_message = latest_row.get("Персонализированное сообщение", "")
|
333 |
+
|
334 |
return reference_message
|
335 |
|
336 |
+
|
337 |
def adapt_messages_to_best_example(
|
338 |
personalized_gigachat_pro,
|
339 |
personalized_gigachat_lite,
|