fruitpicker01 commited on
Commit
47b1ac4
·
verified ·
1 Parent(s): d9f3465

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -35
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" # Or 'messages.xlsx' if you prefer Excel
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
- if file_path.endswith('.xlsx'):
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
- # Filter the DataFrame based on the personalization parameters
291
- filter_condition = (
292
- (df["Пол"] == gender) &
293
- (df["Поколение"] == generation) &
294
- (df["Психотип"] == psychotype) &
295
- (df["Стадия бизнеса"] == business_stage) &
296
- (df["Отрасль"] == industry) &
297
- (df["ОПФ"] == legal_form)
298
- )
299
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  filtered_df = df[filter_condition]
301
-
302
  if filtered_df.empty:
303
- # No previous messages with these parameters
304
  return None
305
-
306
- # Sort the filtered DataFrame by timestamp in descending order
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
- # Get the "Откорректированное сообщение" if it exists, else "Персонализированное сообщение"
313
- if pd.notnull(latest_row["Откорректированное сообщение"]) and latest_row["Откорректированное сообщение"].strip():
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,