SnowFlash383935 commited on
Commit
a8aad4b
·
verified ·
1 Parent(s): d31f41b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -8
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import bz2
3
 
4
- def analyze_cps(file):
5
  if file is None:
6
  return "", ""
7
 
@@ -9,16 +9,20 @@ def analyze_cps(file):
9
  with open(file.name, "rb") as f:
10
  raw_data = f.read()
11
 
12
- # Показываем первые 256 байт в HEX
13
  hex_dump = ' '.join(f'{byte:02x}' for byte in raw_data[:256])
14
  hex_display = f"HEX дамп (первые 256 байт):\n{hex_dump}"
15
 
16
- # Пробуем распаковать через bz2
 
 
 
 
 
17
  try:
18
- decompressed = bz2.decompress(raw_data)
19
  text_output = decompressed.decode("utf-8", errors="replace")
20
  except Exception as e:
21
- text_output = f"[Не удалось распаковать как bzip2]: {e}"
22
 
23
  return hex_display, text_output
24
 
@@ -26,14 +30,14 @@ def analyze_cps(file):
26
  return f"Ошибка чтения файла: {e}", ""
27
 
28
  with gr.Blocks() as demo:
29
- gr.Markdown("## Анализ .cps файла")
30
 
31
  file_input = gr.File(label="Загрузите .cps файл", file_types=[".cps"])
32
  hex_output = gr.Textbox(label="HEX дамп файла", lines=5)
33
- text_output = gr.Textbox(label="Распакованный текст / ошибка", lines=10)
34
 
35
  file_input.change(
36
- fn=analyze_cps,
37
  inputs=file_input,
38
  outputs=[hex_output, text_output]
39
  )
 
1
  import gradio as gr
2
  import bz2
3
 
4
+ def analyze_and_unpack_cps(file):
5
  if file is None:
6
  return "", ""
7
 
 
9
  with open(file.name, "rb") as f:
10
  raw_data = f.read()
11
 
 
12
  hex_dump = ' '.join(f'{byte:02x}' for byte in raw_data[:256])
13
  hex_display = f"HEX дамп (первые 256 байт):\n{hex_dump}"
14
 
15
+ # Проверяем заголовок
16
+ if raw_data.startswith(b'OPS1'):
17
+ compressed_data = raw_data[4:] # Пропускаем OPS1
18
+ else:
19
+ compressed_data = raw_data
20
+
21
  try:
22
+ decompressed = bz2.decompress(compressed_data)
23
  text_output = decompressed.decode("utf-8", errors="replace")
24
  except Exception as e:
25
+ text_output = f"[Ошибка распаковки]: {e}"
26
 
27
  return hex_display, text_output
28
 
 
30
  return f"Ошибка чтения файла: {e}", ""
31
 
32
  with gr.Blocks() as demo:
33
+ gr.Markdown("## Распаковка .cps файла (OPS1 + BZIP2)")
34
 
35
  file_input = gr.File(label="Загрузите .cps файл", file_types=[".cps"])
36
  hex_output = gr.Textbox(label="HEX дамп файла", lines=5)
37
+ text_output = gr.Textbox(label="Распакованный текст", lines=15)
38
 
39
  file_input.change(
40
+ fn=analyze_and_unpack_cps,
41
  inputs=file_input,
42
  outputs=[hex_output, text_output]
43
  )