srgtuszy commited on
Commit
b60875d
1 Parent(s): b37ceb1

XML to PSD

Browse files
Files changed (2) hide show
  1. app.py +43 -2
  2. requirements.txt +1 -1
app.py CHANGED
@@ -6,6 +6,9 @@ import logging
6
  from openai import OpenAI
7
  import json
8
  import textwrap
 
 
 
9
 
10
  openAiClient = OpenAI(
11
  api_key=os.getenv("OPENAI_API_KEY")
@@ -52,7 +55,44 @@ def remove_markdown(text):
52
 
53
  return text.strip()
54
 
55
- # Fetch PCC-3 information
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  def fetch_pcc3_info(case_purpose):
57
  logger.info("Fetching PCC information to formulate questions")
58
  system_message = textwrap.dedent(f"""
@@ -66,6 +106,7 @@ def fetch_pcc3_info(case_purpose):
66
  {{
67
  "form_description": "A brief description of the PCC form",
68
  "xsd_file": "Direct link to the concrete XSD file that was used to create the questions. This link should not point to anything else than XSD file. ",
 
69
  "form_id": "ID of the form that should be used to fill in the answers",
70
  "required_fields": [
71
  {{
@@ -77,7 +118,7 @@ def fetch_pcc3_info(case_purpose):
77
  ]
78
  }}
79
  Odpowiedz tylko JSON'em i niczym więcej. Nie stosuj zadnego formatowania.
80
- Nie stosuj zadnego wstępu. Wybierz najnowszy plik xsd i uzyj go do stworzenia pytan do uzytkownika.
81
  Pytania z formularza powinny miec takie same nazwy pola jak pola w pliku xsd.
82
  Uzyj nastepujacej strony w celu znalezienia odpowiedniego formularza do wypelnienia: https://www.podatki.gov.pl/pcc-sd/e-deklaracje-pcc-sd/formularze-pcc/
83
  """)
 
6
  from openai import OpenAI
7
  import json
8
  import textwrap
9
+ import requests
10
+ import xml.etree.ElementTree as ET
11
+ from lxml import etree
12
 
13
  openAiClient = OpenAI(
14
  api_key=os.getenv("OPENAI_API_KEY")
 
55
 
56
  return text.strip()
57
 
58
+
59
+ def generate_xml_from_xsd(xsd_url, answers):
60
+ response = requests.get(xsd_url)
61
+ if response.status_code != 200:
62
+ raise Exception(f"Failed to fetch XSD file from {xsd_url}")
63
+
64
+ xsd_root = etree.fromstring(response.content)
65
+ schema = etree.XMLSchema(xsd_root)
66
+ parser = etree.XMLParser(schema=schema)
67
+ root = ET.Element(xsd_root.find(".//xs:element", namespaces={"xs": "http://www.w3.org/2001/XMLSchema"}).get("name"))
68
+
69
+ for field, value in answers.items():
70
+ element = ET.SubElement(root, field)
71
+ element.text = str(value)
72
+
73
+ try:
74
+ schema.assertValid(root)
75
+ except etree.DocumentInvalid as e:
76
+ print(f"Generated XML is not valid according to the schema: {e}")
77
+
78
+ xml_string = ET.tostring(root, encoding="unicode", method="xml")
79
+
80
+ return xml_string
81
+
82
+ def generate_pcc3_xml():
83
+ global pcc3_structure, answers
84
+ if pcc3_structure and 'xsd_file' in pcc3_structure:
85
+ try:
86
+ xml_string = generate_xml_from_xsd(pcc3_structure['xsd_file'], answers)
87
+ return xml_string
88
+ except Exception as e:
89
+ logger.error(f"Error generating XML: {e}")
90
+ return None
91
+ else:
92
+ logger.error("PCC3 structure or XSD file URL not available")
93
+ return None
94
+
95
+
96
  def fetch_pcc3_info(case_purpose):
97
  logger.info("Fetching PCC information to formulate questions")
98
  system_message = textwrap.dedent(f"""
 
106
  {{
107
  "form_description": "A brief description of the PCC form",
108
  "xsd_file": "Direct link to the concrete XSD file that was used to create the questions. This link should not point to anything else than XSD file. ",
109
+ "xsd_file_content": "Content of the XSD file. This should be a string that contains the content of the XSD file used to create the questions.",
110
  "form_id": "ID of the form that should be used to fill in the answers",
111
  "required_fields": [
112
  {{
 
118
  ]
119
  }}
120
  Odpowiedz tylko JSON'em i niczym więcej. Nie stosuj zadnego formatowania.
121
+ Nie stosuj zadnego wstępu. Wybierz plik xsd ktory najbardziej pasuje do podanego przez uzytkownika zadania i uzyj go do stworzenia pytan do uzytkownika.
122
  Pytania z formularza powinny miec takie same nazwy pola jak pola w pliku xsd.
123
  Uzyj nastepujacej strony w celu znalezienia odpowiedniego formularza do wypelnienia: https://www.podatki.gov.pl/pcc-sd/e-deklaracje-pcc-sd/formularze-pcc/
124
  """)
requirements.txt CHANGED
@@ -2,4 +2,4 @@ gradio==4.44.0
2
  openai==1.50.2
3
  requests==2.32.3
4
  beautifulsoup4==4.12.3
5
- git+https://github.com/nathanrchn/perplexityai.git
 
2
  openai==1.50.2
3
  requests==2.32.3
4
  beautifulsoup4==4.12.3
5
+ lxml==4.9.3