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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -13
app.py CHANGED
@@ -1,24 +1,46 @@
1
  import gradio as gr
2
  import bz2
3
- import os
4
 
5
- def unpack_cps(file):
6
  if file is None:
7
- return ""
 
8
  try:
9
- with bz2.BZ2File(file.name, 'rb') as f:
10
- decompressed_data = f.read()
11
- return decompressed_data.decode('utf-8')
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  except Exception as e:
13
- return f"Ошибка при распаковке: {str(e)}"
14
 
15
  with gr.Blocks() as demo:
16
- gr.Markdown("## Загрузите .cps файл для распаковки")
17
-
18
  file_input = gr.File(label="Загрузите .cps файл", file_types=[".cps"])
19
- output_text = gr.Textbox(label="Распакованный текст", lines=10)
20
-
21
- file_input.change(fn=unpack_cps, inputs=file_input, outputs=output_text)
22
- file_input.clear(fn=lambda: "", outputs=output_text)
 
 
 
 
 
 
 
 
 
23
 
24
  demo.launch()
 
1
  import gradio as gr
2
  import bz2
 
3
 
4
+ def analyze_cps(file):
5
  if file is None:
6
+ return "", ""
7
+
8
  try:
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
+
25
  except Exception as e:
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
+ )
40
+
41
+ file_input.clear(
42
+ fn=lambda: ("", ""),
43
+ outputs=[hex_output, text_output]
44
+ )
45
 
46
  demo.launch()