Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- .gitignore +1 -0
- README.md +3 -9
- app.py +164 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji: 📉
|
4 |
-
colorFrom: pink
|
5 |
-
colorTo: yellow
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 5.13.0
|
8 |
app_file: app.py
|
9 |
-
|
|
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: WEF_Hackathon_Group2
|
|
|
|
|
|
|
|
|
|
|
3 |
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
sdk_version: 4.37.2
|
6 |
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import pandas as pd
|
4 |
+
from openai import OpenAI
|
5 |
+
import logging
|
6 |
+
|
7 |
+
# Set up logging
|
8 |
+
logging.basicConfig(
|
9 |
+
level=logging.INFO,
|
10 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
11 |
+
handlers=[
|
12 |
+
logging.FileHandler('app.log'),
|
13 |
+
logging.StreamHandler()
|
14 |
+
]
|
15 |
+
)
|
16 |
+
|
17 |
+
logger = logging.getLogger(__name__)
|
18 |
+
|
19 |
+
|
20 |
+
def upload_file(xsl_file):
|
21 |
+
if xsl_file is None:
|
22 |
+
return "Please upload a file first"
|
23 |
+
try:
|
24 |
+
df = pd.read_excel(xsl_file.name)
|
25 |
+
return df
|
26 |
+
except Exception as e:
|
27 |
+
return f"Error reading file: {str(e)}"
|
28 |
+
|
29 |
+
def process_query(xsl_file, query):
|
30 |
+
# Process the file and generate responses
|
31 |
+
logger.info("Starting query processing")
|
32 |
+
api_key = os.getenv('OPENAI_API_KEY')
|
33 |
+
client = OpenAI()
|
34 |
+
model = "gpt-4o"
|
35 |
+
responses = {}
|
36 |
+
|
37 |
+
# Convert DataFrame to JSON
|
38 |
+
if xsl_file is not None:
|
39 |
+
logger.info(f"Reading Excel file: {xsl_file.name}")
|
40 |
+
try:
|
41 |
+
df = pd.read_excel(xsl_file.name)
|
42 |
+
json_data = df.to_json(orient='records')
|
43 |
+
responses['data'] = json_data
|
44 |
+
logger.info("Successfully converted Excel to JSON")
|
45 |
+
except Exception as e:
|
46 |
+
logger.error(f"Error converting Excel to JSON: {str(e)}")
|
47 |
+
raise
|
48 |
+
|
49 |
+
try:
|
50 |
+
logger.info("Sending request to OpenAI API")
|
51 |
+
response = client.chat.completions.create(
|
52 |
+
model=model,
|
53 |
+
messages=[
|
54 |
+
{"role": "system", "content": os.getenv('system_prompt')},
|
55 |
+
{"role": "user", "content": f"Examples: {os.getenv('context_prompt')}\n\n Data: {json_data}\n\n Query: {query} \n\n INSTRUCTIONS: You must absolutely return the sql between <SQL></SQL> and the XML between <XML></XML>. If the user query is incorrect, add the disclaimer between the SQL/XML tags as well. \n\n Output:"}
|
56 |
+
]
|
57 |
+
)
|
58 |
+
|
59 |
+
responses['query'] = query
|
60 |
+
responses['response'] = response.choices[0].message.content
|
61 |
+
logger.info(f"Raw response content: {response.choices[0].message.content}")
|
62 |
+
|
63 |
+
# Add validation before splitting
|
64 |
+
if '<SQL>' not in response.choices[0].message.content or '<XML>' not in response.choices[0].message.content:
|
65 |
+
logger.error("Response missing SQL or XML tags")
|
66 |
+
raise ValueError("Response format invalid - missing SQL or XML tags")
|
67 |
+
|
68 |
+
try:
|
69 |
+
responses['SQL'] = response.choices[0].message.content.split('<SQL>')[1].split('</SQL>')[0]
|
70 |
+
logger.info("Successfully extracted SQL query")
|
71 |
+
except IndexError as e:
|
72 |
+
logger.error(f"Error extracting SQL query: {str(e)}")
|
73 |
+
responses['SQL'] = "Error extracting SQL query"
|
74 |
+
|
75 |
+
try:
|
76 |
+
responses['XML'] = response.choices[0].message.content.split('<XML>')[1].split('</XML>')[0]
|
77 |
+
logger.info("Successfully extracted XML query")
|
78 |
+
except IndexError as e:
|
79 |
+
logger.error(f"Error extracting XML query: {str(e)}")
|
80 |
+
responses['XML'] = "Error extracting XML query"
|
81 |
+
|
82 |
+
logger.info("Successfully processed OpenAI response")
|
83 |
+
|
84 |
+
except Exception as e:
|
85 |
+
print(f"Error occurred: {e}")
|
86 |
+
responses['error'] = str(e)
|
87 |
+
|
88 |
+
try:
|
89 |
+
df = pd.read_excel(xsl_file.name)
|
90 |
+
return responses['SQL'], responses['XML'], responses
|
91 |
+
except Exception as e:
|
92 |
+
return f"Error reading file: {str(e)}", None, None
|
93 |
+
|
94 |
+
# Create the Gradio interface
|
95 |
+
with gr.Blocks() as demo:
|
96 |
+
gr.Markdown("# WEF Hackathon Demo")
|
97 |
+
|
98 |
+
|
99 |
+
# Top section - XLS file upload
|
100 |
+
with gr.Row():
|
101 |
+
xsl_upload = gr.File(
|
102 |
+
label="Upload XLS File",
|
103 |
+
file_types=[".xls"]
|
104 |
+
)
|
105 |
+
|
106 |
+
# Display section for uploaded data
|
107 |
+
data_display = gr.Dataframe(
|
108 |
+
label="Uploaded Data Preview",
|
109 |
+
interactive=False,
|
110 |
+
wrap=True
|
111 |
+
)
|
112 |
+
|
113 |
+
# Bottom section - 3 columns
|
114 |
+
with gr.Row():
|
115 |
+
# First column - Natural language input
|
116 |
+
with gr.Column():
|
117 |
+
query_input = gr.Textbox(
|
118 |
+
label="Natural Language Query",
|
119 |
+
placeholder="Enter your query here...",
|
120 |
+
lines=5
|
121 |
+
)
|
122 |
+
|
123 |
+
# Second column - SQL output
|
124 |
+
with gr.Column():
|
125 |
+
sql_output = gr.Textbox(
|
126 |
+
label="LLM SQL Response",
|
127 |
+
interactive=False
|
128 |
+
)
|
129 |
+
|
130 |
+
# Third column - XML output
|
131 |
+
with gr.Column():
|
132 |
+
xml_output = gr.Textbox(
|
133 |
+
label="LLM XML Response",
|
134 |
+
interactive=False
|
135 |
+
)
|
136 |
+
|
137 |
+
# Submit button
|
138 |
+
submit_btn = gr.Button("Process Query")
|
139 |
+
cached_examples = os.getenv('examples')
|
140 |
+
examples = gr.Examples(
|
141 |
+
examples=[
|
142 |
+
'''Join LFA1 (general vendor data) with LFB1 (company code data) by matching LIFNR. Filter on a specific vendor (e.g., LIFNR = "100000"), then retrieve the vendor’s number, name, company code, payment block, and payment terms.''',
|
143 |
+
'''Match LFA1’s address number (ADRNR) to ADR6’s address reference (ADDRNUMBER) to get e-mail data. For a given vendor (e.g., LIFNR = "100000"), select the vendor’s number, name, e-mail address, and validity dates.''',
|
144 |
+
'''Join LFM1 and LFM2 on both vendor number (LIFNR) and purchasing organization (EKORG). Retrieve data like the vendor’s credit group and blocking status for a given vendor (LIFNR) and purchasing org (EKORG).'''
|
145 |
+
'''Link LFBK (vendor’s bank details) to BNKA (bank master) by matching bank key and account (e.g., LFBK.BANKL = BNKA.BANKL and LFBK.BANKN = BNKA.BANKN). For a vendor (LIFNR = "100000"), return their bank account plus the bank’s name and country.''',
|
146 |
+
'''Join BSIK (open vendor items) with LFA1 (vendor data) using LIFNR. Filter on a specific vendor and list open items (document number, amount) alongside the vendor’s name.''',
|
147 |
+
'''Combine CDHDR/CDPOS (change documents) with LFA1 (vendors). Match CDHDR.OBJECTID = LFA1.LIFNR (and ensure CDHDR.OBJECTCLAS = "LFA1"), then join CDHDR.CHANGENR = CDPOS.CHANGENR to display what fields changed, along with old/new values, for a specific vendor.'''
|
148 |
+
],
|
149 |
+
inputs=query_input
|
150 |
+
)
|
151 |
+
|
152 |
+
xsl_upload.change(fn=upload_file,
|
153 |
+
inputs=[xsl_upload],
|
154 |
+
outputs=data_display)
|
155 |
+
|
156 |
+
# Handle submission
|
157 |
+
submit_btn.click(
|
158 |
+
fn=process_query,
|
159 |
+
inputs=[xsl_upload, query_input],
|
160 |
+
outputs=[sql_output, xml_output]
|
161 |
+
)
|
162 |
+
|
163 |
+
if __name__ == "__main__":
|
164 |
+
demo.launch()
|