Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Install necessary packages\n",
"Langchain supports many LLM inference providers, including Fireworks."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install langchain\n",
"!pip install python-dotenv\n",
"!pip install langchain-fireworks"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"from langchain.chains import LLMChain\n",
"from langchain_core.output_parsers import StrOutputParser\n",
"from langchain_fireworks import Fireworks \n",
"\n",
"load_dotenv()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### API KEY\n",
"* register and get api key from : https://fireworks.ai/api-keys\n",
"* put the key in the file .env file in FIREWORKS_API_KEY variable"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"api_key = os.getenv(\"FIREWORKS_API_KEY\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"# maximum number of tokens to generate by the model\n",
"max_tokens = {}\n",
"max_tokens[0] = 1000\n",
"max_tokens[1] = 1000\n",
"max_tokens[2] = 2000"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Prompting Models"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"models = [\n",
" 'accounts/fireworks/models/starcoder-7b', \n",
" 'accounts/fireworks/models/starcoder-16b', \n",
" 'accounts/fireworks/models/llama-v2-13b-code-instruct', \n",
" 'accounts/fireworks/models/llama-v2-34b-code-instruct',\n",
" 'accounts/fireworks/models/llama-v2-70b-code-instruct',\n",
" 'accounts/fireworks/models/mixtral-8x7b-instruct',\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Processing shot: 0\n",
"Processing model: starcoder-7b\n",
"Processing model: starcoder-16b\n",
"Processing model: llama-v2-13b-code-instruct\n",
"Processing model: llama-v2-34b-code-instruct\n",
"Processing model: llama-v2-70b-code-instruct\n",
"Processing model: mixtral-8x7b-instruct\n",
"Processing shot: 1\n",
"Processing model: starcoder-7b\n",
"Processing model: starcoder-16b\n",
"Processing model: llama-v2-13b-code-instruct\n",
"Processing model: llama-v2-34b-code-instruct\n",
"Processing model: llama-v2-70b-code-instruct\n",
"Processing model: mixtral-8x7b-instruct\n",
"Processing shot: 2\n",
"Processing model: starcoder-7b\n",
"Processing model: starcoder-16b\n",
"Processing model: llama-v2-13b-code-instruct\n",
"Processing model: llama-v2-34b-code-instruct\n",
"Processing model: llama-v2-70b-code-instruct\n",
"Processing model: mixtral-8x7b-instruct\n"
]
}
],
"source": [
"shots = [0,1,2]\n",
"\n",
"for shot in shots:\n",
" print(f'Processing shot: {shot}')\n",
" \n",
" base_path = f'data/prompts/{shot}-shot'\n",
" prompt = open(f'{base_path}/prompt.txt', 'r').read()\n",
" \n",
" for model in models:\n",
" model_name = model.split('/')[-1]\n",
" print(f'Processing model: {model_name}')\n",
" \n",
" results_dir = f'{base_path}/results'\n",
" \n",
" if not os.path.exists(results_dir):\n",
" os.makedirs(results_dir)\n",
"\n",
" \n",
" file_path = f'{results_dir}/{model_name}.fireworks.ai.txt'\n",
" \n",
" # Check if the result file already exists\n",
" if os.path.exists(file_path):\n",
" print('Skipping...')\n",
" continue\n",
"\n",
" llm = Fireworks(\n",
"\t\t\tfireworks_api_key=api_key,\n",
"\t\t\tmodel=model,\n",
"\t\t\tmax_tokens=max_tokens[shot])\n",
" result = llm.invoke(prompt)\n",
" \n",
" with open(file_path, 'w') as file:\n",
" file.write(result) \n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import json\n",
"\n",
"def send_fireworks_chat_request(model,messages, api_key, max_tokens=4096, \n",
" temperature=0.6, top_p=1,top_k=40, \n",
" frequency_penalty=0, \n",
" presence_penalty=0, \n",
" ):\n",
"\n",
" url = \"https://api.fireworks.ai/inference/v1/chat/completions\"\n",
" payload = {\n",
" \"model\": model,\n",
" \"messages\": messages,\n",
" \"max_tokens\": max_tokens,\n",
" \"temperature\": temperature,\n",
" \"top_p\": top_p,\n",
" \"frequency_penalty\": frequency_penalty,\n",
" \"presence_penalty\": presence_penalty,\n",
" \"top_k\": top_k,\n",
" }\n",
" headers = {\n",
" \"Accept\": \"application/json\",\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": f\"Bearer {api_key}\"\n",
" }\n",
" \n",
" response = requests.post(url, json=payload, headers=headers)\n",
" return response.json()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"system_message = \"\"\"\n",
"You are AKN4EU XML formation converter. \n",
"You receive plain text documents and convert them to XML. \n",
"Return only the final XML document converted from the text.\n",
"The user gives some examples of XML documents.\n",
"\"\"\"\n",
"\n",
"\n",
"assistant_message = \"\"\"\n",
"EUROPEAN COMMISSION\n",
"Brussels, 21.12.2016\n",
"2016/0411 (COD)\n",
"Proposal for a\n",
"REGULATION OF THE EUROPEAN PARLIAMENT AND OF THE\n",
"COUNCIL\n",
"amending Regulation (EC)\n",
"No 1008/2008 on common rules for the operation of air services in the\n",
"Community\n",
"EN\n",
"\n",
"\"\"\"\n",
"\n",
"user_message_1 = \"\"\"\n",
"Convert plain text of following coverpage to AKN4EU XML format.\n",
"\n",
"\n",
"EUROPEAN COMMISSION\n",
"Brussels, 21.12.2016\n",
"2016/0411 (COD)\n",
"Proposal for a\n",
"REGULATION OF THE EUROPEAN PARLIAMENT AND OF THE\n",
"COUNCIL\n",
"amending Regulation (EC)\n",
"No 1008/2008 on common rules for the operation of air services in the\n",
"Community\n",
"EN\n",
"\"\"\"\n",
"\n",
"assistant_message = \"\"\"\n",
"<coverPage>\n",
"\t<container name=\"logo\">\n",
"\t\t<p><img src=\"EC.png\" alt=\"EUROPEAN COMMISSION\"/></p>\n",
"\t</container>\n",
"\t<container name=\"actingEntity\">\n",
"\t\t<p><organization refersTo=\"~_COM\">EUROPEAN COMMISSION</organization></p>\n",
"\t</container>\n",
"\t<container name=\"mainDoc\">\n",
"\t\t<block name=\"placeAndDate\">\n",
"\t\t\t<location refersTo=\"~_BEL_BRU\">Brussels</location>, <date date=\"2016-12-21\">21.12.2016</date>\n",
"\t\t</block>\n",
"\t</container>\n",
"\t<container name=\"procedureIdentifier\">\n",
"\t\t<p><docketNumber refersTo=\"~_procedure_2016_411\">2016/0411 (COD)</docketNumber></p>\n",
"\t</container>\n",
"\t<longTitle>\n",
"\t\t<p><docStage>Proposal for a</docStage>\n",
"\t\t\t<docType refersTo=\"~_REG\">REGULATION OF THE EUROPEAN PARLIAMENT AND OF THE\n",
"\t\t\t\tCOUNCIL</docType>\n",
"\t\t\t<docPurpose>amending <ref href=\"http://data.europa.eu/eli/reg/2008/1008\">Regulation (EC)\n",
"\t\t\t\t\tNo 1008/2008 on common rules for the operation of air services in the\n",
"\t\t\t\tCommunity</ref></docPurpose></p>\n",
"\t</longTitle>\n",
"\t<container name=\"mainDocLanguage\">\n",
"\t\t<p><inline name=\"language\" refersTo=\"~_FRBRlanguage\">EN</inline></p>\n",
"\t</container>\n",
"</coverPage>\n",
"\"\"\"\n",
"\n",
"user_message_2 = \"\"\"\n",
"Convert plain text of following coverpage to AKN4EU XML format.\n",
"\n",
"EUROPEAN COMMISSION\n",
"Brussels, 21.12.2017\n",
"2012/0412 (COD)\n",
"Proposal for a\n",
"REGULATION OF THE EUROPEAN PARLIAMENT AND OF THE COUNCIL amending Regulation (EC)\n",
"No 1009/2009 on common rules for the operation of air services in the Community\n",
"EN\n",
"\"\"\"\n",
"\n",
"messages = [\n",
" {\"role\": \"system\", \"content\": system_message},\n",
" {\"role\": \"user\", \"content\": user_message_1},\n",
" {\"role\": \"assistant\", \"content\": assistant_message},\n",
" {\"role\": \"user\", \"content\": user_message_2},\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'id': '16132b15-b1f0-4b85-b330-be64b145fcbe', 'object': 'chat.completion', 'created': 1712076650, 'model': 'accounts/fireworks/models/mixtral-8x7b-instruct', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': '<coverPage>\\n\\t<container name=\"logo\">\\n\\t\\t<p><img src=\"EC.png\" alt=\"EUROPEAN COMMISSION\"/></p>\\n\\t</container>\\n\\t<container name=\"actingEntity\">\\n\\t\\t<p><organization refersTo=\"~_COM\">EUROPEAN COMMISSION</organization></p>\\n\\t</container>\\n\\t<container name=\"mainDoc\">\\n\\t\\t<block name=\"placeAndDate\">\\n\\t\\t\\t<location refersTo=\"~_BEL_BRU\">Brussels</location>, <date date=\"2017-12-21\">21.12.2017</date>\\n\\t\\t</block>\\n\\t</container>\\n\\t<container name=\"procedureIdentifier\">\\n\\t\\t<p><docketNumber refersTo=\"~_procedure_2012_412\">2012/0412 (COD)</docketNumber></p>\\n\\t</container>\\n\\t<longTitle>\\n\\t\\t<p><docStage>Proposal for a</docStage>\\n\\t\\t\\t<docType refersTo=\"~_REG\">REGULATION OF THE EUROPEAN PARLIAMENT AND OF THE COUNCIL</docType>\\n\\t\\t\\t<docPurpose>amending <ref href=\"http://data.europa.eu/eli/reg/2009/1009\">Regulation (EC)\\n\\t\\t\\t\\tNo 1009/2009 on common rules for the operation of air services in the Community</ref></docPurpose></p>\\n\\t</longTitle>\\n\\t<container name=\"mainDocLanguage\">\\n\\t\\t<p><inline name=\"language\" refersTo=\"~_FRBRlanguage\">EN</inline></p>\\n\\t</container>\\n</coverPage>'}, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 744, 'total_tokens': 1171, 'completion_tokens': 427}}\n"
]
}
],
"source": [
"response = send_fireworks_chat_request(\"accounts/fireworks/models/mixtral-8x7b-instruct\",messages, api_key)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"def send_fireworks_completion_request(model,prompt, api_key, max_tokens=4096, \n",
" temperature=0.6, top_p=1,top_k=40, \n",
" frequency_penalty=0, \n",
" presence_penalty=0,\n",
" ):\n",
"\n",
" url = \"https://api.fireworks.ai/inference/v1/completions\"\n",
" payload = {\n",
" \"model\": model, \n",
" \"max_tokens\": max_tokens,\n",
" \"temperature\": temperature,\n",
" \"top_p\": top_p,\n",
" \"top_k\": top_k,\n",
" \"presence_penalty\": presence_penalty,\n",
" \"frequency_penalty\": frequency_penalty,\n",
" \"prompt\": prompt,\n",
" }\n",
" headers = {\n",
" \"Accept\": \"application/json\",\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": f\"Bearer {api_key}\"\n",
" }\n",
" \n",
" response = requests.post(url, json=payload, headers=headers)\n",
" return response.json()"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"\n",
"prompt = \"\"\"\n",
"You are AKN4EU XML formation converter. \n",
"You receive plain text documents and convert them to XML. \n",
"Return only the final XML document converted from the text.\n",
"The user gives some examples of XML documents.\n",
"\n",
"\n",
"#######################################\n",
"\n",
"# PLAIN TEXT EXAMPLE #\n",
"\n",
"\n",
"EUROPEAN COMMISSION\n",
"Brussels, 21.12.2016\n",
"2016/0411 (COD)\n",
"Proposal for a\n",
"REGULATION OF THE EUROPEAN PARLIAMENT AND OF THE\n",
"COUNCIL\n",
"amending Regulation (EC)\n",
"No 1008/2008 on common rules for the operation of air services in the\n",
"Community\n",
"EN\n",
"\n",
"\n",
"#######################################\n",
"\n",
"# XML OF TEXT EXAMPLE #\n",
"\n",
"<coverPage>\n",
"\t<container name=\"logo\">\n",
"\t\t<p><img src=\"EC.png\" alt=\"EUROPEAN COMMISSION\"/></p>\n",
"\t</container>\n",
"\t<container name=\"actingEntity\">\n",
"\t\t<p><organization refersTo=\"~_COM\">EUROPEAN COMMISSION</organization></p>\n",
"\t</container>\n",
"\t<container name=\"mainDoc\">\n",
"\t\t<block name=\"placeAndDate\">\n",
"\t\t\t<location refersTo=\"~_BEL_BRU\">Brussels</location>, <date date=\"2016-12-21\">21.12.2016</date>\n",
"\t\t</block>\n",
"\t</container>\n",
"\t<container name=\"procedureIdentifier\">\n",
"\t\t<p><docketNumber refersTo=\"~_procedure_2016_411\">2016/0411 (COD)</docketNumber></p>\n",
"\t</container>\n",
"\t<longTitle>\n",
"\t\t<p><docStage>Proposal for a</docStage>\n",
"\t\t\t<docType refersTo=\"~_REG\">REGULATION OF THE EUROPEAN PARLIAMENT AND OF THE\n",
"\t\t\t\tCOUNCIL</docType>\n",
"\t\t\t<docPurpose>amending <ref href=\"http://data.europa.eu/eli/reg/2008/1008\">Regulation (EC)\n",
"\t\t\t\t\tNo 1008/2008 on common rules for the operation of air services in the\n",
"\t\t\t\tCommunity</ref></docPurpose></p>\n",
"\t</longTitle>\n",
"\t<container name=\"mainDocLanguage\">\n",
"\t\t<p><inline name=\"language\" refersTo=\"~_FRBRlanguage\">EN</inline></p>\n",
"\t</container>\n",
"</coverPage>\n",
"\n",
"\n",
"#############\n",
"\n",
"# TEXT TO CONVERT #\n",
"\n",
"EUROPEAN COMMISSION\n",
"Brussels, 21.12.2017\n",
"2012/0412 (COD)\n",
"Proposal for a\n",
"REGULATION OF THE EUROPEAN PARLIAMENT AND OF THE COUNCIL amending Regulation (EC)\n",
"No 1009/2009 on common rules for the operation of air services in the Community\n",
"EN\n",
"\n",
"\n",
"#######################################\n",
"\n",
"# XML OF COVER PAGE PLAIN TEXT #\n",
"\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'id': '4eeb7a9c-bf52-43c7-94eb-a9ed14feaba3', 'object': 'text_completion', 'created': 1712078198, 'model': 'accounts/fireworks/models/mixtral-8x7b-instruct', 'choices': [{'index': 0, 'text': '<coverPage>\\n\\t<container name=\"logo\">\\n\\t\\t<p><img src=\"EC.png\" alt=\"EUROPEAN COMMISSION\"/></p>\\n\\t</container>\\n\\t<container name=\"actingEntity\">\\n\\t\\t<p><organization refersTo=\"~_COM\">EUROPEAN COMMISSION</organization></p>\\n\\t</container>\\n\\t<container name=\"mainDoc\">\\n\\t\\t<block name=\"placeAndDate\">\\n\\t\\t\\t<location refersTo=\"~_BEL_BRU\">Brussels</location>, <date date=\"2017-12-21\">21.12.2017</date>\\n\\t\\t</block>\\n\\t</container>\\n\\t<container name=\"procedureIdentifier\">\\n\\t\\t<p><docketNumber refersTo=\"~_procedure_2012_412\">2012/0412 (COD)</docketNumber></p>\\n\\t</container>\\n\\t<longTitle>\\n\\t\\t<p><docStage>Proposal for a</docStage>\\n\\t\\t\\t<docType refersTo=\"~_REG\">REGULATION OF THE EUROPEAN PARLIAMENT AND OF THE\\n\\t\\t\\t\\tCOUNCIL</docType>\\n\\t\\t\\t<docPurpose>amending <ref href=\"http://data.europa.eu/eli/reg/2009/1009\">Regulation (EC)\\n\\t\\t\\t\\t\\tNo 1009/2009 on common rules for the operation of air services in the\\n\\t\\t\\t\\tCommunity</ref></docPurpose></p>\\n\\t</longTitle>\\n\\t<container name=\"mainDocLanguage\">\\n\\t\\t<p><inline name=\"language\" refersTo=\"~_FRBRlanguage\">EN</inline></p>\\n\\t</container>\\n</coverPage>\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\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n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n', 'logprobs': None, 'finish_reason': 'length'}], 'usage': {'prompt_tokens': 744, 'total_tokens': 4840, 'completion_tokens': 4096}}\n"
]
}
],
"source": [
"response = send_fireworks_completion_request(\"accounts/fireworks/models/mixtral-8x7b-instruct\",prompt, api_key)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### XML Extraction from results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import re\n",
"\n",
"base_dir = 'data/prompts'\n",
"shots = [0, 1, 2]\n",
"\n",
"for shot in shots:\n",
" results_path = os.path.join(base_dir, f'{shot}-shot', 'results')\n",
" results_xml_path = os.path.join(base_dir, f'{shot}-shot', 'results-xml')\n",
"\n",
" # Ensure the results-xml directory exists\n",
" if not os.path.exists(results_xml_path):\n",
" os.makedirs(results_xml_path)\n",
" \n",
" # Loop through each result file in the results directory\n",
" if os.path.exists(results_path) and os.path.isdir(results_path):\n",
" for result_file in os.listdir(results_path):\n",
" file_path = os.path.join(results_path, result_file)\n",
" if file_path.endswith('.fireworks.ai.txt'):\n",
" with open(file_path, 'r') as file:\n",
" result_content = file.read()\n",
" \n",
" # Regular expression to find content enclosed by <coverPage>...</coverPage>\n",
" # This pattern ignores any text outside the XML tags\n",
" start_tag = \"<coverPage>\"\n",
" end_tag = \"</coverPage>\"\n",
" \n",
" # Finding the last occurrence of the start_tag and the last occurrence of the end_tag\n",
" start = result_content.rfind(start_tag)\n",
" end = result_content.rfind(end_tag) + len(end_tag)\n",
" \n",
" # If the start tag or end tag is not found, return an empty string or a specific message\n",
" if start == -1 or end == -1:\n",
" print(f\"No XML content found in {result_file}\")\n",
" \n",
" xml_content = result_content[start:end]\n",
" \n",
" \n",
" # Prepares the filename and path for saving the extracted XML\n",
" xml_file_name = result_file.replace('.txt', '.xml')\n",
" xml_file_path = os.path.join(results_xml_path, xml_file_name)\n",
" \n",
" # Writes the XML content to a new file in the results-xml directory\n",
" with open(xml_file_path, 'w') as xml_file:\n",
" xml_file.write(xml_content)\n",
" print(f'Extracted and saved XML for {xml_file_name}')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}