beermen commited on
Commit
3c3c67b
·
verified ·
1 Parent(s): e7f84ea

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -408
app.py DELETED
@@ -1,408 +0,0 @@
1
- import io
2
- import re
3
- import zipfile
4
- import tempfile
5
- from datetime import datetime
6
- from typing import List, Dict
7
- import os
8
-
9
- import streamlit as st
10
- import pandas as pd
11
- from pypdf import PdfReader
12
-
13
- APP_TITLE = "📑 SAP KB Artikel Chunker"
14
-
15
- # -----------------------------
16
- # SAP KB Parsing
17
- # -----------------------------
18
-
19
- class SAPKBArticle:
20
- """Repräsentiert einen SAP Knowledge Base Artikel"""
21
-
22
- def __init__(self, text: str):
23
- self.raw_text = text
24
- self.note_id = ""
25
- self.title = ""
26
- self.component = ""
27
- self.version = ""
28
- self.release_date = ""
29
- self.sections = {}
30
- self._parse()
31
-
32
- def _parse(self):
33
- """Extrahiert Metadaten und Sektionen aus dem Artikel"""
34
- lines = self.raw_text.split('\n')
35
-
36
- # Extrahiere Note ID und Title aus der ersten Zeile
37
- first_line = lines[0] if lines else ""
38
- match = re.match(r'(\d+)\s*-\s*(.+)', first_line)
39
- if match:
40
- self.note_id = match.group(1)
41
- self.title = match.group(2).strip()
42
-
43
- # Extrahiere Component, Version, Release Date
44
- for line in lines[:10]:
45
- if 'Component:' in line:
46
- comp_match = re.search(r'Component:\s*([^,]+)', line)
47
- if comp_match:
48
- self.component = comp_match.group(1).strip()
49
- if 'Version:' in line:
50
- ver_match = re.search(r'Version:\s*(\d+)', line)
51
- if ver_match:
52
- self.version = ver_match.group(1)
53
- if 'Released On:' in line:
54
- date_match = re.search(r'Released On:\s*(.+)', line)
55
- if date_match:
56
- self.release_date = date_match.group(1).strip()
57
-
58
- # Extrahiere Sektionen
59
- self._extract_sections()
60
-
61
- def _extract_sections(self):
62
- """Extrahiert die verschiedenen Sektionen des Artikels"""
63
- text = self.raw_text
64
-
65
- # Symptom
66
- symptom_match = re.search(r'Symptom\s*\n(.*?)(?=\n\s*Environment|\n\s*Reproducing|$)', text, re.DOTALL | re.IGNORECASE)
67
- if symptom_match:
68
- self.sections['symptom'] = self._clean_text(symptom_match.group(1))
69
-
70
- # Environment
71
- env_match = re.search(r'Environment\s*\n(.*?)(?=\n\s*Reproducing|\n\s*Cause|$)', text, re.DOTALL | re.IGNORECASE)
72
- if env_match:
73
- self.sections['environment'] = self._clean_text(env_match.group(1))
74
-
75
- # Reproducing the Issue
76
- repro_match = re.search(r'Reproducing the Issue\s*\n(.*?)(?=\n\s*Cause|\n\s*Resolution|$)', text, re.DOTALL | re.IGNORECASE)
77
- if repro_match:
78
- self.sections['reproducing'] = self._clean_text(repro_match.group(1))
79
-
80
- # Cause
81
- cause_match = re.search(r'Cause\s*\n(.*?)(?=\n\s*Resolution|\n\s*Keywords|$)', text, re.DOTALL | re.IGNORECASE)
82
- if cause_match:
83
- self.sections['cause'] = self._clean_text(cause_match.group(1))
84
-
85
- # Resolution
86
- resolution_match = re.search(r'Resolution\s*\n(.*?)(?=\n\s*Keywords|\n\s*Attributes|$)', text, re.DOTALL | re.IGNORECASE)
87
- if resolution_match:
88
- self.sections['resolution'] = self._clean_text(resolution_match.group(1))
89
-
90
- # Products/Versions
91
- products_match = re.search(r'Products\s*\n.*?\n(.*?)$', text, re.DOTALL | re.IGNORECASE)
92
- if products_match:
93
- self.sections['products'] = self._clean_text(products_match.group(1))
94
-
95
- # Other Components
96
- other_comp = []
97
- for match in re.finditer(r'Other Components\s+(.+)', text):
98
- other_comp.append(match.group(1).strip())
99
- if other_comp:
100
- self.sections['other_components'] = ' | '.join(other_comp)
101
-
102
- def _clean_text(self, text: str) -> str:
103
- """Bereinigt und komprimiert Text"""
104
- text = re.sub(r'\n\s*\n\s*\n+', '\n\n', text)
105
- text = re.sub(r' +', ' ', text)
106
- return text.strip()
107
-
108
- def create_chunks(self) -> List[Dict[str, str]]:
109
- """Erstellt semantische Chunks im gewünschten Format"""
110
- chunks = []
111
-
112
- # 1. METADATA_SUMMARY
113
- metadata_content = f"This note documents an issue in SAP Business One"
114
- if self.title:
115
- metadata_content += f" regarding {self.title.lower()}"
116
- metadata_content += f". ID: {self.note_id}"
117
- if self.component:
118
- metadata_content += f", Component: {self.component}"
119
- if self.version:
120
- metadata_content += f", Version: {self.version}"
121
- if self.release_date:
122
- metadata_content += f", Released: {self.release_date}"
123
- metadata_content += "."
124
-
125
- chunks.append({
126
- 'section': 'METADATA_SUMMARY',
127
- 'content': metadata_content
128
- })
129
-
130
- # 2. SYMPTOM_SCENARIO
131
- if 'symptom' in self.sections or 'reproducing' in self.sections:
132
- symptom_content = ""
133
- if 'symptom' in self.sections:
134
- symptom_content = self.sections['symptom']
135
- if 'reproducing' in self.sections:
136
- if symptom_content:
137
- symptom_content += " "
138
- symptom_content += "Scenario: " + self.sections['reproducing']
139
-
140
- chunks.append({
141
- 'section': 'SYMPTOM_SCENARIO',
142
- 'content': symptom_content
143
- })
144
-
145
- # 3. CAUSE_PREREQUISITES
146
- if 'cause' in self.sections:
147
- chunks.append({
148
- 'section': 'CAUSE_PREREQUISITES',
149
- 'content': self.sections['cause']
150
- })
151
-
152
- # 4. SOLUTION_PATCH
153
- if 'resolution' in self.sections:
154
- chunks.append({
155
- 'section': 'SOLUTION_PATCH',
156
- 'content': self.sections['resolution']
157
- })
158
-
159
- # 5. AFFECTED_VERSIONS
160
- if 'products' in self.sections or 'environment' in self.sections or 'other_components' in self.sections:
161
- affected_content = "This issue affects "
162
- if 'products' in self.sections:
163
- affected_content += self.sections['products']
164
- elif 'environment' in self.sections:
165
- affected_content += self.sections['environment']
166
-
167
- if 'other_components' in self.sections:
168
- affected_content += f". Relevant components include {self.sections['other_components']}"
169
- affected_content += "."
170
-
171
- chunks.append({
172
- 'section': 'AFFECTED_VERSIONS',
173
- 'content': affected_content
174
- })
175
-
176
- return chunks
177
-
178
- def format_chunk(self, chunk: Dict[str, str]) -> str:
179
- """Formatiert einen Chunk im gewünschten Output-Format"""
180
- prefix = f"[Note {self.note_id} - {self.title}]"
181
- return f"{prefix} - Sektion [{chunk['section']}]: {chunk['content']}"
182
-
183
-
184
- def extract_text_from_pdf(file_data: bytes) -> str:
185
- """Extrahiert Text aus PDF"""
186
- reader = PdfReader(io.BytesIO(file_data))
187
- pages_text = []
188
- for page in reader.pages:
189
- pages_text.append(page.extract_text() or "")
190
- return "\n".join(pages_text)
191
-
192
-
193
- # -----------------------------
194
- # Streamlit UI
195
- # -----------------------------
196
- st.set_page_config(page_title=APP_TITLE, layout="wide", page_icon="📑")
197
-
198
- st.title(APP_TITLE)
199
-
200
- st.markdown("""
201
- Diese App verarbeitet **SAP Knowledge Base Artikel** (PDFs) und teilt sie in semantische Chunks auf.
202
-
203
- **Unterstützte Sektionen:**
204
- - 📋 `METADATA_SUMMARY` - Grundinformationen (Note ID, Component, Version, etc.)
205
- - 🔍 `SYMPTOM_SCENARIO` - Problem & Reproduktionsszenario
206
- - 💡 `CAUSE_PREREQUISITES` - Ursache & Voraussetzungen
207
- - ✅ `SOLUTION_PATCH` - Lösung & Workaround
208
- - 🏷️ `AFFECTED_VERSIONS` - Betroffene Versionen & Komponenten
209
- """)
210
-
211
- st.divider()
212
-
213
- # File Upload
214
- st.subheader("📄 SAP KB Artikel hochladen")
215
- uploaded_file = st.file_uploader(
216
- "PDF-Datei hochladen",
217
- type=["pdf"],
218
- help="SAP Knowledge Base Artikel als PDF (z.B. von SAP Support Launchpad)"
219
- )
220
-
221
- # Optional: Text direkt eingeben
222
- with st.expander("📝 Oder: Text direkt eingeben"):
223
- manual_text = st.text_area(
224
- "SAP KB Artikel Text",
225
- height=200,
226
- placeholder="Text des SAP KB Artikels hier einfügen..."
227
- )
228
-
229
- # Source URL
230
- source_url = st.text_input(
231
- "Quelle / URL (optional)",
232
- placeholder="https://launchpad.support.sap.com/#/notes/...",
233
- help="URL zum Original-Artikel (optional, wird in Export aufgenommen)"
234
- )
235
-
236
- st.divider()
237
-
238
- # Process Button
239
- process_btn = st.button("🔄 Artikel verarbeiten", type="primary", use_container_width=True)
240
-
241
- # -----------------------------
242
- # Verarbeitung
243
- # -----------------------------
244
- if process_btn:
245
- text_content = ""
246
-
247
- if uploaded_file is not None:
248
- try:
249
- file_data = uploaded_file.read()
250
- text_content = extract_text_from_pdf(file_data)
251
- st.success(f"✅ PDF erfolgreich gelesen ({len(text_content)} Zeichen)")
252
- except Exception as e:
253
- st.error(f"❌ Fehler beim Lesen der PDF: {str(e)}")
254
- st.stop()
255
- elif manual_text.strip():
256
- text_content = manual_text
257
- st.info("ℹ️ Text-Eingabe wird verwendet")
258
- else:
259
- st.error("❌ Bitte laden Sie eine PDF hoch oder geben Sie Text ein.")
260
- st.stop()
261
-
262
- if not text_content.strip():
263
- st.error("❌ Kein Text gefunden.")
264
- st.stop()
265
-
266
- try:
267
- article = SAPKBArticle(text_content)
268
- chunks_data = article.create_chunks()
269
-
270
- if not chunks_data:
271
- st.warning("⚠️ Es wurden keine Chunks erzeugt.")
272
- st.stop()
273
-
274
- st.success(f"✅ **{len(chunks_data)} Chunks erfolgreich erstellt**")
275
-
276
- # Artikel-Info
277
- with st.expander("📋 Artikel-Informationen", expanded=True):
278
- col_a, col_b = st.columns(2)
279
- with col_a:
280
- st.write(f"**Note ID:** {article.note_id}")
281
- st.write(f"**Titel:** {article.title}")
282
- with col_b:
283
- st.write(f"**Component:** {article.component}")
284
- if article.version:
285
- st.write(f"**Version:** {article.version}")
286
- if article.release_date:
287
- st.write(f"**Released:** {article.release_date}")
288
-
289
- st.divider()
290
-
291
- # Zeige Chunks
292
- st.subheader("📦 Generierte Chunks")
293
-
294
- section_emojis = {
295
- 'METADATA_SUMMARY': '📋',
296
- 'SYMPTOM_SCENARIO': '🔍',
297
- 'CAUSE_PREREQUISITES': '💡',
298
- 'SOLUTION_PATCH': '✅',
299
- 'AFFECTED_VERSIONS': '🏷️'
300
- }
301
-
302
- formatted_chunks = []
303
- for i, chunk_data in enumerate(chunks_data, start=1):
304
- formatted = article.format_chunk(chunk_data)
305
- formatted_chunks.append({
306
- 'note_id': article.note_id,
307
- 'title': article.title,
308
- 'section': chunk_data['section'],
309
- 'content': chunk_data['content'],
310
- 'formatted': formatted,
311
- 'source_url': source_url
312
- })
313
-
314
- emoji = section_emojis.get(chunk_data['section'], '📄')
315
- with st.expander(f"{emoji} Chunk {i}: {chunk_data['section']} ({len(formatted)} Zeichen)"):
316
- st.code(formatted, language=None)
317
- st.caption(f"Content: {chunk_data['content'][:200]}..." if len(chunk_data['content']) > 200 else chunk_data['content'])
318
-
319
- st.divider()
320
-
321
- # Export-Optionen
322
- st.subheader("💾 Export-Optionen")
323
-
324
- # DataFrame für CSV
325
- df = pd.DataFrame({
326
- "chunk_id": list(range(1, len(formatted_chunks) + 1)),
327
- "note_id": [c['note_id'] for c in formatted_chunks],
328
- "title": [c['title'] for c in formatted_chunks],
329
- "section": [c['section'] for c in formatted_chunks],
330
- "chunk": [c['formatted'] for c in formatted_chunks],
331
- "content": [c['content'] for c in formatted_chunks],
332
- "chars": [len(c['formatted']) for c in formatted_chunks],
333
- "source_url": [c['source_url'] for c in formatted_chunks]
334
- })
335
-
336
- col1, col2, col3 = st.columns(3)
337
-
338
- with col1:
339
- csv_bytes = df.to_csv(index=False).encode("utf-8")
340
- st.download_button(
341
- "📥 CSV herunterladen",
342
- data=csv_bytes,
343
- file_name=f"sap_kb_{article.note_id}_chunks.csv",
344
- mime="text/csv",
345
- use_container_width=True
346
- )
347
-
348
- with col2:
349
- all_txt = ""
350
- if source_url:
351
- all_txt += f"Quelle: {source_url}\n"
352
- all_txt += f"SAP KB Note: {article.note_id}\n"
353
- all_txt += f"Titel: {article.title}\n\n"
354
- all_txt += ("=" * 80) + "\n\n"
355
- all_txt += "\n\n---\n\n".join([c['formatted'] for c in formatted_chunks])
356
-
357
- st.download_button(
358
- "📥 TXT (alle Chunks)",
359
- data=all_txt,
360
- file_name=f"sap_kb_{article.note_id}_all.txt",
361
- mime="text/plain",
362
- use_container_width=True
363
- )
364
-
365
- with col3:
366
- export_time = datetime.now()
367
- with tempfile.TemporaryDirectory() as tmpdir:
368
- zip_path = os.path.join(tmpdir, "chunks.zip")
369
- with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as zf:
370
- for i, chunk in enumerate(formatted_chunks, start=1):
371
- timestamp_sum = (
372
- export_time.day + export_time.month + export_time.year +
373
- export_time.hour + export_time.minute + export_time.second + i
374
- )
375
- fname = f"chunk_{article.note_id}_{chunk['section']}_{timestamp_sum}.txt"
376
-
377
- content = ""
378
- if source_url:
379
- content += f"Quelle: {source_url}\n"
380
- content += f"SAP KB Note: {article.note_id}\n"
381
- content += f"Sektion: {chunk['section']}\n\n"
382
- content += chunk['formatted']
383
-
384
- fpath = os.path.join(tmpdir, fname)
385
- with open(fpath, "w", encoding="utf-8") as fh:
386
- fh.write(content)
387
- zf.write(fpath, arcname=fname)
388
-
389
- with open(zip_path, "rb") as fh:
390
- st.download_button(
391
- "📥 ZIP (einzeln)",
392
- data=fh.read(),
393
- file_name=f"sap_kb_{article.note_id}_chunks.zip",
394
- mime="application/zip",
395
- use_container_width=True
396
- )
397
-
398
- except Exception as e:
399
- st.error(f"❌ Fehler bei der Verarbeitung: {str(e)}")
400
- st.exception(e)
401
-
402
- # Footer
403
- st.divider()
404
- st.markdown("""
405
- <div style='text-align: center; color: #666; font-size: 0.9em;'>
406
- SAP KB Artikel Chunker v1.0 | Entwickelt für SAP Business One Knowledge Base Artikel
407
- </div>
408
- """, unsafe_allow_html=True)