huggingface112 commited on
Commit
d2b313b
·
1 Parent(s): 1c9d24a

add compressedContentViewer

Browse files
Files changed (2) hide show
  1. Dockerfile +1 -1
  2. compressedContentViewer.py +208 -0
Dockerfile CHANGED
@@ -8,7 +8,7 @@ RUN python3 -m pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
 
9
  COPY . .
10
 
11
- CMD ["panel", "serve", "/code/app.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "*"]
12
 
13
  RUN mkdir /.cache
14
  RUN chmod 777 /.cache
 
8
 
9
  COPY . .
10
 
11
+ CMD ["panel", "serve", "/code/compressedContentViewer.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "*"]
12
 
13
  RUN mkdir /.cache
14
  RUN chmod 777 /.cache
compressedContentViewer.py ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import panel as pn
2
+ import lz4.block
3
+ import xml.sax.saxutils
4
+ import html
5
+ import io
6
+ import xml.dom.minidom
7
+
8
+ # Initialize Panel with Bootstrap design
9
+ pn.extension(design="bootstrap", sizing_mode="stretch_width")
10
+
11
+ # Social media links and icons
12
+ ICON_URLS = {
13
+ "brand-github": "https://github.com/yourusername/compressed-content-viewer",
14
+ "message-circle": "https://discourse.holoviz.org/",
15
+ }
16
+
17
+ # Create a function to decompress using the working method from binary_to_text.py
18
+
19
+
20
+ def decompress_CompressContent(data):
21
+ """
22
+ 解压缩Msg:CompressContent内容
23
+ :param data:
24
+ :return:
25
+ """
26
+ if data is None or not isinstance(data, bytes):
27
+ return ""
28
+ try:
29
+ dst = lz4.block.decompress(data, uncompressed_size=len(data) << 10)
30
+ decoded_string = dst.decode().replace("\x00", "") # Remove any null characters
31
+ except Exception as e:
32
+ return f"Decompression failed: {str(e)}"
33
+ return decoded_string
34
+
35
+ # Function to unescape XML using proper XML tools
36
+
37
+
38
+ def unescape_xml(text):
39
+ # First handle standard XML entities
40
+ text = xml.sax.saxutils.unescape(text)
41
+
42
+ # Then handle HTML entities that might be present
43
+ text = html.unescape(text)
44
+
45
+ return text
46
+
47
+
48
+ # Function to format XML with proper indentation using lxml
49
+ def format_xml(xml_string):
50
+ try:
51
+ # Parse the XML string
52
+ dom = xml.dom.minidom.parseString(xml_string)
53
+ # Get the pretty-printed XML
54
+ return dom.toprettyxml(indent=" ")
55
+ except Exception as e:
56
+ # If parsing fails, return the original string with the error
57
+ return f"XML formatting error: {str(e)}\n\nOriginal XML:\n{xml_string}"
58
+
59
+
60
+ # Create the UI with modern styling
61
+ file_input = pn.widgets.FileInput(
62
+ accept='.bin',
63
+ height=50,
64
+ sizing_mode="stretch_width"
65
+ )
66
+
67
+ status = pn.widgets.StaticText(
68
+ value="##### 📁 Upload a .bin file to see its decompressed XML content",
69
+ align="center"
70
+ )
71
+
72
+ # Use a TextAreaInput with modern styling
73
+ result_area = pn.widgets.TextAreaInput(
74
+ value="",
75
+ height=400,
76
+ sizing_mode="stretch_width",
77
+ disabled=True,
78
+ stylesheets=['body { font-family: monospace; }']
79
+ )
80
+
81
+ # Styled checkbox
82
+ unescape_checkbox = pn.widgets.Checkbox(
83
+ name="Unescape XML entities",
84
+ value=True,
85
+ align="center",
86
+ margin=(10, 10)
87
+ )
88
+
89
+ # Function to get the XML content for download
90
+
91
+
92
+ def get_xml_content():
93
+ if processed_xml:
94
+ return io.StringIO(processed_xml)
95
+ return None
96
+
97
+
98
+ # Modern styled download button
99
+ download_button = pn.widgets.FileDownload(
100
+ callback=get_xml_content,
101
+ filename="decompressed.xml",
102
+ button_type="primary",
103
+ label="📥 Download XML",
104
+ disabled=True,
105
+ align="center",
106
+ height=35
107
+ )
108
+
109
+ # Global variable to store the processed XML
110
+ processed_xml = None
111
+
112
+ # Connect the function to the file input
113
+
114
+
115
+ def process_file(event):
116
+ global processed_xml
117
+ if event.new is not None:
118
+ status.value = "##### ⚙️ Processing file..."
119
+ try:
120
+ # Use the working decompression method
121
+ xml_string = decompress_CompressContent(event.new)
122
+
123
+ if xml_string and not xml_string.startswith("Decompression failed"):
124
+ # Try to format as XML
125
+ try:
126
+ # Unescape XML entities if checkbox is checked
127
+ if unescape_checkbox.value:
128
+ xml_string = unescape_xml(xml_string)
129
+
130
+ # Format XML with proper indentation using lxml
131
+ pretty_xml = format_xml(xml_string)
132
+
133
+ result_area.value = pretty_xml
134
+ processed_xml = pretty_xml
135
+
136
+ # Update the download filename based on the input file
137
+ if hasattr(file_input, 'filename') and file_input.filename:
138
+ base_name = file_input.filename.rsplit('.', 1)[0]
139
+ download_button.filename = f"{base_name}.xml"
140
+
141
+ status.value = "##### ✅ File processed successfully!"
142
+ download_button.disabled = False
143
+ except Exception as xml_error:
144
+ # If XML parsing fails, just show the raw text
145
+ result_area.value = xml_string
146
+ processed_xml = xml_string
147
+ status.value = f"##### ⚠️ Decompressed, but XML parsing failed: {str(xml_error)}"
148
+ download_button.disabled = False
149
+ else:
150
+ result_area.value = xml_string
151
+ status.value = "##### ❌ Decompression failed"
152
+ download_button.disabled = True
153
+ processed_xml = None
154
+ except Exception as e:
155
+ status.value = f"##### ❌ Error: {str(e)}"
156
+ result_area.value = ""
157
+ download_button.disabled = True
158
+ processed_xml = None
159
+
160
+
161
+ file_input.param.watch(process_file, 'value')
162
+
163
+ # Add a callback for the checkbox to reprocess the current file when toggled
164
+
165
+
166
+ def toggle_unescape(event):
167
+ if file_input.value is not None:
168
+ process_file(pn.param.ParamEvent(obj=file_input, name='value', old=None, new=file_input.value))
169
+
170
+
171
+ unescape_checkbox.param.watch(toggle_unescape, 'value')
172
+
173
+ # Add footer with social links
174
+ footer_row = pn.Row(pn.Spacer(), align="center")
175
+ for icon, url in ICON_URLS.items():
176
+ href_button = pn.widgets.Button(icon=icon, width=35, height=35)
177
+ href_button.js_on_click(code=f"window.open('{url}')")
178
+ footer_row.append(href_button)
179
+ footer_row.append(pn.Spacer())
180
+
181
+ # Create main content
182
+ main = pn.WidgetBox(
183
+ pn.Column(
184
+ pn.pane.Markdown("##### 📁 Drop your .bin file here or click to upload"),
185
+ file_input,
186
+ pn.Row(
187
+ unescape_checkbox,
188
+ download_button,
189
+ align="center"
190
+ ),
191
+ status,
192
+ pn.pane.Markdown("##### 📄 Decompressed XML Content"),
193
+ result_area,
194
+ footer_row,
195
+ sizing_mode="stretch_width"
196
+ )
197
+ )
198
+
199
+ # Create the template
200
+ title = "Compressed Content Viewer"
201
+ template = pn.template.BootstrapTemplate(
202
+ title=title,
203
+ main=main,
204
+ main_max_width="min(80%, 1000px)",
205
+ header_background="#F08080",
206
+ )
207
+
208
+ template.servable(title=title)