fruitpicker01 commited on
Commit
0747fb0
·
verified ·
1 Parent(s): 06d93a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py CHANGED
@@ -13,6 +13,7 @@ import pymorphy2
13
  import re
14
  import string
15
  import io
 
16
 
17
  morph = pymorphy2.MorphAnalyzer()
18
 
@@ -59,6 +60,74 @@ for sheet_name, df in data.items():
59
  current_request_index = -1 # Изначально указывает на последний запрос
60
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def correct_dash_usage(text):
63
  # Step 1: Replace any dash with long dash if surrounded by spaces
64
  text = re.sub(r'\s[-–—]\s', ' — ', text)
@@ -1781,6 +1850,10 @@ with gr.Blocks() as demo:
1781
 
1782
  gr.Markdown("---")
1783
 
 
 
 
 
1784
  # Очистка всех полей кроме prompt_display
1785
  description_input.change(
1786
  fn=clear_on_change, # Сначала вызываем функцию очистки полей
@@ -2442,5 +2515,10 @@ with gr.Blocks() as demo:
2442
  outputs=[save_meta_llama_405b_btn]
2443
  )
2444
 
 
 
 
 
 
2445
 
2446
  demo.launch()
 
13
  import re
14
  import string
15
  import io
16
+ from datetime import datetime
17
 
18
  morph = pymorphy2.MorphAnalyzer()
19
 
 
60
  current_request_index = -1 # Изначально указывает на последний запрос
61
 
62
 
63
+ def download_current_message_database():
64
+ # GitHub repositories and authors mapping
65
+ repos = {
66
+ 'Storage_1': 'Редакторы',
67
+ 'Storage_Ira': 'Ира',
68
+ 'Storage_Kate': 'Катя',
69
+ 'Storage_Sveta': 'Света'
70
+ }
71
+
72
+ # Base GitHub API URL
73
+ base_url = 'https://api.github.com/repos/fruitpicker01/{repo}/contents'
74
+
75
+ data_list = []
76
+
77
+ headers = {
78
+ "Authorization": f"token {token}", # 'token' is from your environment variables
79
+ "Content-Type": "application/json"
80
+ }
81
+
82
+ for repo_name, author in repos.items():
83
+ url = base_url.format(repo=repo_name)
84
+ response = requests.get(url, headers=headers)
85
+ if response.status_code == 200:
86
+ files = response.json()
87
+ # Filter files starting with 'file'
88
+ json_files = [file for file in files if file['name'].startswith("file") and file['name'].endswith('.json')]
89
+ for file_info in json_files:
90
+ file_name = file_info['name']
91
+ file_url = file_info['download_url']
92
+
93
+ # Extract timestamp from filename
94
+ try:
95
+ timestamp = file_name.split('_')[1].split('.')[0]
96
+ save_date = datetime.utcfromtimestamp(int(timestamp)).strftime('%Y-%m-%d %H:%M:%S')
97
+ except:
98
+ save_date = None # or set to some default
99
+
100
+ # Download and decode file content
101
+ file_response = requests.get(file_url)
102
+ if file_response.status_code == 200:
103
+ data = json.loads(file_response.text)
104
+ normalized_data = pd.json_normalize(data)
105
+ normalized_data['Дата сохранения'] = save_date
106
+ normalized_data['Автор'] = author
107
+ data_list.append(normalized_data)
108
+ else:
109
+ print(f"Error downloading file {file_name} from repo {repo_name}: {file_response.status_code}")
110
+ else:
111
+ print(f"Error accessing repo {repo_name}: {response.status_code}")
112
+
113
+ if data_list:
114
+ df = pd.concat(data_list, ignore_index=True)
115
+ # Convert 'Дата сохранения' to datetime
116
+ df['Дата сохранения'] = pd.to_datetime(df['Дата сохранения'], format='%Y-%m-%d %H:%M:%S', errors='coerce')
117
+ # Drop duplicates
118
+ df.drop_duplicates(inplace=True)
119
+ # Sort by 'Дата сохранения' descending
120
+ df.sort_values(by='Дата сохранения', ascending=False, inplace=True)
121
+ # Create Excel file in memory
122
+ output = io.BytesIO()
123
+ df.to_excel(output, index=False)
124
+ output.seek(0) # Reset pointer
125
+ # Return as a tuple with the filename
126
+ return ('messages.xlsx', output)
127
+ else:
128
+ return None
129
+
130
+
131
  def correct_dash_usage(text):
132
  # Step 1: Replace any dash with long dash if surrounded by spaces
133
  text = re.sub(r'\s[-–—]\s', ' — ', text)
 
1850
 
1851
  gr.Markdown("---")
1852
 
1853
+ with gr.Row():
1854
+ download_btn = gr.Button("Выгрузить актуальную базу сообщений")
1855
+ download_file = gr.File(label="Скачать базу сообщений")
1856
+
1857
  # Очистка всех полей кроме prompt_display
1858
  description_input.change(
1859
  fn=clear_on_change, # Сначала вызываем функцию очистки полей
 
2515
  outputs=[save_meta_llama_405b_btn]
2516
  )
2517
 
2518
+ download_btn.click(
2519
+ fn=download_current_message_database,
2520
+ inputs=[],
2521
+ outputs=download_file
2522
+ )
2523
 
2524
  demo.launch()