Spaces:
Build error
Build error
Update base.py
Browse files
base.py
CHANGED
|
@@ -1,12 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
-
from groq import Groq
|
| 3 |
import xml.etree.ElementTree as ET
|
| 4 |
-
from
|
| 5 |
|
| 6 |
# Initialize Groq Client
|
| 7 |
-
client = Groq(
|
| 8 |
-
api_key=os.environ.get("GROQ_API_KEY"),
|
| 9 |
-
)
|
| 10 |
|
| 11 |
def extract_uavariable_chunks(xml_file):
|
| 12 |
"""
|
|
@@ -41,51 +38,28 @@ def send_to_llm(client, prompt, chunk):
|
|
| 41 |
)
|
| 42 |
return response.choices[0].message.content.strip()
|
| 43 |
|
| 44 |
-
def
|
| 45 |
-
"""
|
| 46 |
-
Generates a PDF from the output object containing the natural language descriptions.
|
| 47 |
-
Writes the PDF into a BytesIO buffer instead of a file.
|
| 48 |
-
"""
|
| 49 |
-
pdf = FPDF()
|
| 50 |
-
pdf.set_auto_page_break(auto=True, margin=15)
|
| 51 |
-
pdf.add_page()
|
| 52 |
-
pdf.set_font("Arial", size=12)
|
| 53 |
-
|
| 54 |
-
for idx, (original, natural_language) in enumerate(output_object, 1):
|
| 55 |
-
pdf.set_font("Arial", style='B', size=12)
|
| 56 |
-
pdf.cell(0, 10, f"UAVariable {idx}", ln=True)
|
| 57 |
-
pdf.set_font("Arial", size=10)
|
| 58 |
-
pdf.multi_cell(0, 10, f"Original XML:\n{original}", align="L")
|
| 59 |
-
pdf.ln(5)
|
| 60 |
-
pdf.set_font("Arial", style='B', size=12)
|
| 61 |
-
pdf.cell(0, 10, "Natural Language Conversion:", ln=True)
|
| 62 |
-
pdf.set_font("Arial", size=10)
|
| 63 |
-
pdf.multi_cell(0, 10, natural_language, align="L")
|
| 64 |
-
pdf.ln(10)
|
| 65 |
-
|
| 66 |
-
# Write the PDF to the buffer
|
| 67 |
-
pdf.output(pdf_buffer)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
def process_xml_to_pdf(xml_file, output_pdf):
|
| 71 |
"""
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
"""
|
| 75 |
prompt = ("Convert this xml chunk to natural language. The hierarchy should be taken into account "
|
| 76 |
"and no values should be missed. Do not provide extra text. Just the conversion of xml to natural language.")
|
| 77 |
|
| 78 |
uavariables = extract_uavariable_chunks(xml_file)
|
| 79 |
output_object = []
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
natural_language = send_to_llm(client, prompt, chunk)
|
| 84 |
-
output_object.append(
|
| 85 |
except Exception as e:
|
| 86 |
print(f"Error processing chunk: {e}")
|
| 87 |
continue
|
| 88 |
|
| 89 |
-
|
| 90 |
-
generate_pdf(output_object, output_pdf)
|
| 91 |
-
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import xml.etree.ElementTree as ET
|
| 3 |
+
from groq import Groq
|
| 4 |
|
| 5 |
# Initialize Groq Client
|
| 6 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def extract_uavariable_chunks(xml_file):
|
| 9 |
"""
|
|
|
|
| 38 |
)
|
| 39 |
return response.choices[0].message.content.strip()
|
| 40 |
|
| 41 |
+
def process_xml_and_generate_natural_language(xml_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
"""
|
| 43 |
+
Processes the XML file, extracts UAVariable chunks, sends them to LLaMA model,
|
| 44 |
+
and returns the natural language description.
|
| 45 |
+
Limits the number of UAVariable chunks to 15.
|
| 46 |
"""
|
| 47 |
prompt = ("Convert this xml chunk to natural language. The hierarchy should be taken into account "
|
| 48 |
"and no values should be missed. Do not provide extra text. Just the conversion of xml to natural language.")
|
| 49 |
|
| 50 |
uavariables = extract_uavariable_chunks(xml_file)
|
| 51 |
output_object = []
|
| 52 |
+
limit = 15 # Set a limit for the number of variables to be processed
|
| 53 |
+
|
| 54 |
+
for idx, chunk in enumerate(uavariables):
|
| 55 |
+
if idx >= limit:
|
| 56 |
+
break # Stop processing after 15 UAVariables
|
| 57 |
+
|
| 58 |
try:
|
| 59 |
natural_language = send_to_llm(client, prompt, chunk)
|
| 60 |
+
output_object.append(natural_language)
|
| 61 |
except Exception as e:
|
| 62 |
print(f"Error processing chunk: {e}")
|
| 63 |
continue
|
| 64 |
|
| 65 |
+
return output_object
|
|
|
|
|
|