aigurletov commited on
Commit
ee4ec14
·
1 Parent(s): 313d10b
Files changed (1) hide show
  1. app.py +25 -16
app.py CHANGED
@@ -3,22 +3,22 @@ import pandas as pd
3
  import yaml
4
  import io
5
  import random
 
6
  from collections import deque
7
 
8
  # HELPER FUNCTIONS TO PARSE .asset FILES
9
- def parse_asset_file(file):
10
- """Parses a YAML-like .asset file, skipping the initial %YAML and %TAG lines."""
11
- if file is None:
12
  return None
13
  try:
14
- content = file.decode('utf-8')
15
  # Find the first '---' which indicates the start of the YAML document
16
- yaml_start = content.find('---')
17
  if yaml_start == -1:
18
  raise ValueError("Not a valid Unity .asset file (missing '---')")
19
 
20
  # Clean up the content for standard YAML parsing
21
- yaml_content = content[yaml_start:]
22
  # Remove the MonoBehaviour header which is not valid YAML
23
  lines = yaml_content.split('\n')
24
  cleaned_lines = [line for line in lines if not line.strip().startswith('MonoBehaviour:')]
@@ -34,7 +34,7 @@ def parse_asset_file(file):
34
  # A simplified approach to extract key-value pairs for now
35
  # This will need to be much more robust based on the actual file structure.
36
  config_data = {}
37
- for line in content.split('\n'):
38
  if ':' in line:
39
  parts = line.split(':', 1)
40
  key = parts[0].strip()
@@ -127,7 +127,8 @@ def run_simulation_interface(ruleset_file, chain_files, iteration_count, initial
127
  df.to_csv(csv_buffer, index=False)
128
  csv_bytes = csv_buffer.getvalue().encode('utf-8')
129
 
130
- return df, stats_report, gr.File.update(value=csv_bytes, visible=True, name=df_path), gr.File.update(value=stats_report.encode('utf-8'), visible=True, name=report_path)
 
131
 
132
  # --- COST CALCULATION LOGIC ---
133
  def run_cost_calculation_logic(chain_files, item_files):
@@ -176,11 +177,11 @@ def view_config_file(file):
176
  if file is None:
177
  return "Загрузите файл для просмотра.", ""
178
 
179
- raw_content = file.decode('utf-8')
 
 
180
 
181
- # A simple display of the raw content.
182
- # In a real scenario, we would parse this into editable components.
183
- return raw_content, os.path.basename(file.name)
184
 
185
  # --- GRADIO UI ---
186
  with gr.Blocks(theme=gr.themes.Soft(), title="Инструмент Балансировки Merge-2") as demo:
@@ -203,8 +204,8 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Инструмент Баланс
203
  gr.Markdown("### Сводный отчет")
204
  sim_stats_report = gr.Textbox(label="Статистика", lines=10)
205
  with gr.Row():
206
- sim_download_csv = gr.File(label="Скачать CSV", visible=False)
207
- sim_download_report = gr.File(label="Скачать отчет", visible=False)
208
 
209
  # --- COST CALCULATION TAB ---
210
  with gr.TabItem("Расчет стоимости цепочек"):
@@ -243,14 +244,22 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Инструмент Баланс
243
  outputs=[cost_results_df]
244
  )
245
 
 
 
 
 
 
 
 
246
  config_upload.upload(
247
- fn=lambda x: x.name if x else "",
248
  inputs=config_upload,
249
  outputs=config_file_name
250
  )
251
 
 
252
  config_upload.upload(
253
- fn=lambda file: file.decode('utf-8') if file else "",
254
  inputs=config_upload,
255
  outputs=config_content_viewer
256
  )
 
3
  import yaml
4
  import io
5
  import random
6
+ import os
7
  from collections import deque
8
 
9
  # HELPER FUNCTIONS TO PARSE .asset FILES
10
+ def parse_asset_file(file_content):
11
+ """Parses a YAML-like .asset file from a string, skipping the initial %YAML and %TAG lines."""
12
+ if file_content is None:
13
  return None
14
  try:
 
15
  # Find the first '---' which indicates the start of the YAML document
16
+ yaml_start = file_content.find('---')
17
  if yaml_start == -1:
18
  raise ValueError("Not a valid Unity .asset file (missing '---')")
19
 
20
  # Clean up the content for standard YAML parsing
21
+ yaml_content = file_content[yaml_start:]
22
  # Remove the MonoBehaviour header which is not valid YAML
23
  lines = yaml_content.split('\n')
24
  cleaned_lines = [line for line in lines if not line.strip().startswith('MonoBehaviour:')]
 
34
  # A simplified approach to extract key-value pairs for now
35
  # This will need to be much more robust based on the actual file structure.
36
  config_data = {}
37
+ for line in file_content.split('\n'):
38
  if ':' in line:
39
  parts = line.split(':', 1)
40
  key = parts[0].strip()
 
127
  df.to_csv(csv_buffer, index=False)
128
  csv_bytes = csv_buffer.getvalue().encode('utf-8')
129
 
130
+ # FIX: Use gr.update() to update component properties
131
+ return df, stats_report, gr.update(value=csv_bytes, visible=True, name=df_path), gr.update(value=stats_report.encode('utf-8'), visible=True, name=report_path)
132
 
133
  # --- COST CALCULATION LOGIC ---
134
  def run_cost_calculation_logic(chain_files, item_files):
 
177
  if file is None:
178
  return "Загрузите файл для просмотра.", ""
179
 
180
+ # The 'file' object from gr.File is a string path to a temporary file
181
+ with open(file, 'r', encoding='utf-8') as f:
182
+ raw_content = f.read()
183
 
184
+ return raw_content, os.path.basename(file)
 
 
185
 
186
  # --- GRADIO UI ---
187
  with gr.Blocks(theme=gr.themes.Soft(), title="Инструмент Балансировки Merge-2") as demo:
 
204
  gr.Markdown("### Сводный отчет")
205
  sim_stats_report = gr.Textbox(label="Статистика", lines=10)
206
  with gr.Row():
207
+ sim_download_csv = gr.File(label="Скачать CSV", visible=False, interactive=False)
208
+ sim_download_report = gr.File(label="Скачать отчет", visible=False, interactive=False)
209
 
210
  # --- COST CALCULATION TAB ---
211
  with gr.TabItem("Расчет стоимости цепочек"):
 
244
  outputs=[cost_results_df]
245
  )
246
 
247
+ def get_file_path_and_content(file_obj):
248
+ if not file_obj:
249
+ return "", ""
250
+ with open(file_obj.name, 'r', encoding='utf-8') as f:
251
+ content = f.read()
252
+ return file_obj.name, content
253
+
254
  config_upload.upload(
255
+ fn=lambda file_obj: os.path.basename(file_obj.name) if file_obj else "",
256
  inputs=config_upload,
257
  outputs=config_file_name
258
  )
259
 
260
+ # FIX: The input 'file' is a temporary file object. We need to open and read it.
261
  config_upload.upload(
262
+ fn=lambda file_obj: open(file_obj.name, 'r', encoding='utf-8').read() if file_obj else "",
263
  inputs=config_upload,
264
  outputs=config_content_viewer
265
  )