Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,116 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import tempfile
|
|
|
4 |
from io import BytesIO
|
|
|
|
|
5 |
|
6 |
def process_woocommerce_data_in_memory(netcom_file):
|
7 |
"""
|
8 |
Reads the uploaded NetCom CSV file in-memory, processes it to the WooCommerce format,
|
9 |
and returns the resulting CSV as bytes, suitable for download.
|
10 |
"""
|
11 |
-
# Define the brand-to-logo mapping
|
12 |
brand_logo_map = {
|
13 |
-
"Amazon Web Services": "
|
14 |
-
"Cisco": "
|
15 |
-
"Microsoft": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
}
|
17 |
|
|
|
|
|
|
|
18 |
# 1. Read the uploaded CSV into a DataFrame
|
19 |
netcom_df = pd.read_csv(netcom_file.name, encoding='latin1')
|
20 |
netcom_df.columns = netcom_df.columns.str.strip() # standardize column names
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# 2. Create aggregated dates and times for each Course ID
|
|
|
|
|
|
|
23 |
date_agg = (
|
24 |
netcom_df.groupby('Course ID')['Course Start Date']
|
25 |
.apply(lambda x: ','.join(x.astype(str).unique()))
|
@@ -53,12 +144,10 @@ def process_woocommerce_data_in_memory(netcom_file):
|
|
53 |
'Name': parent_products['Course Name'],
|
54 |
'Published': 1,
|
55 |
'Visibility in catalog': 'visible',
|
56 |
-
'Short description': parent_products['
|
57 |
-
'Description': parent_products['
|
58 |
'Tax status': 'taxable',
|
59 |
'In stock?': 1,
|
60 |
-
'Stock': 1,
|
61 |
-
'Sold individually?': 1,
|
62 |
'Regular price': parent_products['SRP Pricing'].replace('[\$,]', '', regex=True),
|
63 |
'Categories': 'courses',
|
64 |
'Images': parent_products['Vendor'].map(brand_logo_map).fillna(''),
|
@@ -76,13 +165,13 @@ def process_woocommerce_data_in_memory(netcom_file):
|
|
76 |
'Attribute 3 value(s)': parent_products['Aggregated_Times'],
|
77 |
'Attribute 3 visible': 'visible',
|
78 |
'Attribute 3 global': 1,
|
79 |
-
'Meta: outline': parent_products['
|
80 |
'Meta: days': parent_products['Duration'],
|
81 |
'Meta: location': 'Virtual',
|
82 |
'Meta: overview': parent_products['Target Audience'],
|
83 |
-
'Meta: objectives': parent_products['
|
84 |
-
'Meta: prerequisites': parent_products['
|
85 |
-
'Meta: agenda': parent_products['
|
86 |
})
|
87 |
|
88 |
# 6. Create child (variation) products
|
@@ -92,12 +181,10 @@ def process_woocommerce_data_in_memory(netcom_file):
|
|
92 |
'Name': netcom_df['Course Name'],
|
93 |
'Published': 1,
|
94 |
'Visibility in catalog': 'visible',
|
95 |
-
'Short description': netcom_df['
|
96 |
-
'Description': netcom_df['
|
97 |
'Tax status': 'taxable',
|
98 |
'In stock?': 1,
|
99 |
-
'Stock': 1,
|
100 |
-
'Sold individually?': 1,
|
101 |
'Regular price': netcom_df['SRP Pricing'].replace('[\$,]', '', regex=True),
|
102 |
'Categories': 'courses',
|
103 |
'Images': netcom_df['Vendor'].map(brand_logo_map).fillna(''),
|
@@ -117,23 +204,23 @@ def process_woocommerce_data_in_memory(netcom_file):
|
|
117 |
),
|
118 |
'Attribute 3 visible': 'visible',
|
119 |
'Attribute 3 global': 1,
|
120 |
-
'Meta: outline': netcom_df['
|
121 |
'Meta: days': netcom_df['Duration'],
|
122 |
'Meta: location': 'Virtual',
|
123 |
'Meta: overview': netcom_df['Target Audience'],
|
124 |
-
'Meta: objectives': netcom_df['
|
125 |
-
'Meta: prerequisites': netcom_df['
|
126 |
-
'Meta: agenda': netcom_df['
|
127 |
})
|
128 |
|
129 |
# 7. Combine parent + child
|
130 |
woo_final_df = pd.concat([woo_parent_df, woo_child_df], ignore_index=True)
|
131 |
|
132 |
-
# 8. Desired column order
|
133 |
column_order = [
|
134 |
'Type', 'SKU', 'Name', 'Published', 'Visibility in catalog',
|
135 |
'Short description', 'Description', 'Tax status', 'In stock?',
|
136 |
-
'
|
137 |
'Parent', 'Brands', 'Attribute 1 name', 'Attribute 1 value(s)', 'Attribute 1 visible',
|
138 |
'Attribute 1 global', 'Attribute 2 name', 'Attribute 2 value(s)', 'Attribute 2 visible',
|
139 |
'Attribute 2 global', 'Attribute 3 name', 'Attribute 3 value(s)', 'Attribute 3 visible',
|
@@ -174,4 +261,5 @@ app = gr.Interface(
|
|
174 |
)
|
175 |
|
176 |
if __name__ == "__main__":
|
|
|
177 |
app.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import tempfile
|
4 |
+
import os
|
5 |
from io import BytesIO
|
6 |
+
import re
|
7 |
+
import openai
|
8 |
|
9 |
def process_woocommerce_data_in_memory(netcom_file):
|
10 |
"""
|
11 |
Reads the uploaded NetCom CSV file in-memory, processes it to the WooCommerce format,
|
12 |
and returns the resulting CSV as bytes, suitable for download.
|
13 |
"""
|
14 |
+
# Define the brand-to-logo mapping with updated URLs
|
15 |
brand_logo_map = {
|
16 |
+
"Amazon Web Services": "/wp-content/uploads/2025/04/aws.png",
|
17 |
+
"Cisco": "/wp-content/uploads/2025/04/cisco-e1738593292198-1.webp",
|
18 |
+
"Microsoft": "/wp-content/uploads/2025/04/Microsoft-e1737494120985-1.png",
|
19 |
+
"Google Cloud": "/wp-content/uploads/2025/04/Google_Cloud.png",
|
20 |
+
"EC Council": "/wp-content/uploads/2025/04/Ec_Council.png",
|
21 |
+
"ITIL": "/wp-content/uploads/2025/04/ITIL.webp",
|
22 |
+
"PMI": "/wp-content/uploads/2025/04/PMI.png",
|
23 |
+
"Comptia": "/wp-content/uploads/2025/04/Comptia.png",
|
24 |
+
"Autodesk": "/wp-content/uploads/2025/04/autodesk.png",
|
25 |
+
"ISC2": "/wp-content/uploads/2025/04/ISC2.png",
|
26 |
+
"AICerts": "/wp-content/uploads/2025/04/aicerts-logo-1.png"
|
27 |
}
|
28 |
|
29 |
+
# Default prerequisite text for courses without prerequisites
|
30 |
+
default_prerequisite = "No specific prerequisites are required for this course. Basic computer literacy and familiarity with fundamental concepts in the subject area are recommended for the best learning experience."
|
31 |
+
|
32 |
# 1. Read the uploaded CSV into a DataFrame
|
33 |
netcom_df = pd.read_csv(netcom_file.name, encoding='latin1')
|
34 |
netcom_df.columns = netcom_df.columns.str.strip() # standardize column names
|
35 |
|
36 |
+
# Initialize OpenAI client
|
37 |
+
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
38 |
+
|
39 |
+
# Process descriptions in batches of 500
|
40 |
+
def process_text_with_ai(texts, instruction):
|
41 |
+
"""Process text with GPT-4o-mini"""
|
42 |
+
if not texts:
|
43 |
+
return []
|
44 |
+
|
45 |
+
results = []
|
46 |
+
batch_size = 500
|
47 |
+
|
48 |
+
for i in range(0, len(texts), batch_size):
|
49 |
+
batch = texts[i:i+batch_size]
|
50 |
+
batch_prompts = [f"{instruction}\n\nText: {text}" for text in batch]
|
51 |
+
|
52 |
+
batch_results = []
|
53 |
+
for prompt in batch_prompts:
|
54 |
+
response = client.chat.completions.create(
|
55 |
+
model="gpt-4o-mini",
|
56 |
+
messages=[{"role": "user", "content": prompt}],
|
57 |
+
temperature=0
|
58 |
+
)
|
59 |
+
batch_results.append(response.choices[0].message.content)
|
60 |
+
|
61 |
+
results.extend(batch_results)
|
62 |
+
|
63 |
+
return results
|
64 |
+
|
65 |
+
# Prepare descriptions for AI processing
|
66 |
+
descriptions = netcom_df['Decription'].fillna("").tolist()
|
67 |
+
objectives = netcom_df['Objectives'].fillna("").tolist()
|
68 |
+
prerequisites = netcom_df['RequiredPrerequisite'].fillna("").tolist()
|
69 |
+
agendas = netcom_df['Outline'].fillna("").tolist()
|
70 |
+
|
71 |
+
# Process with AI
|
72 |
+
short_descriptions = process_text_with_ai(
|
73 |
+
descriptions,
|
74 |
+
"Create a concise 250-character summary of this course description:"
|
75 |
+
)
|
76 |
+
|
77 |
+
condensed_descriptions = process_text_with_ai(
|
78 |
+
descriptions,
|
79 |
+
"Condense this description to maximum 750 characters in paragraph format, with clean formatting:"
|
80 |
+
)
|
81 |
+
|
82 |
+
formatted_objectives = process_text_with_ai(
|
83 |
+
objectives,
|
84 |
+
"Format these objectives into a bullet list format with clean formatting. Start each bullet with '• ':"
|
85 |
+
)
|
86 |
+
|
87 |
+
formatted_prerequisites = []
|
88 |
+
for prereq in prerequisites:
|
89 |
+
if not prereq or pd.isna(prereq) or prereq.strip() == "":
|
90 |
+
formatted_prerequisites.append(default_prerequisite)
|
91 |
+
else:
|
92 |
+
formatted_prereq = process_text_with_ai(
|
93 |
+
[prereq],
|
94 |
+
"Format these prerequisites into a bullet list format with clean formatting. Start each bullet with '• ':"
|
95 |
+
)[0]
|
96 |
+
formatted_prerequisites.append(formatted_prereq)
|
97 |
+
|
98 |
+
formatted_agendas = process_text_with_ai(
|
99 |
+
agendas,
|
100 |
+
"Format this agenda into a bullet list format with clean formatting. Start each bullet with '• ':"
|
101 |
+
)
|
102 |
+
|
103 |
+
# Add processed text to dataframe
|
104 |
+
netcom_df['Short_Description'] = short_descriptions
|
105 |
+
netcom_df['Condensed_Description'] = condensed_descriptions
|
106 |
+
netcom_df['Formatted_Objectives'] = formatted_objectives
|
107 |
+
netcom_df['Formatted_Prerequisites'] = formatted_prerequisites
|
108 |
+
netcom_df['Formatted_Agenda'] = formatted_agendas
|
109 |
+
|
110 |
# 2. Create aggregated dates and times for each Course ID
|
111 |
+
# Sort by Course ID and date first
|
112 |
+
netcom_df = netcom_df.sort_values(['Course ID', 'Course Start Date'])
|
113 |
+
|
114 |
date_agg = (
|
115 |
netcom_df.groupby('Course ID')['Course Start Date']
|
116 |
.apply(lambda x: ','.join(x.astype(str).unique()))
|
|
|
144 |
'Name': parent_products['Course Name'],
|
145 |
'Published': 1,
|
146 |
'Visibility in catalog': 'visible',
|
147 |
+
'Short description': parent_products['Short_Description'],
|
148 |
+
'Description': parent_products['Condensed_Description'],
|
149 |
'Tax status': 'taxable',
|
150 |
'In stock?': 1,
|
|
|
|
|
151 |
'Regular price': parent_products['SRP Pricing'].replace('[\$,]', '', regex=True),
|
152 |
'Categories': 'courses',
|
153 |
'Images': parent_products['Vendor'].map(brand_logo_map).fillna(''),
|
|
|
165 |
'Attribute 3 value(s)': parent_products['Aggregated_Times'],
|
166 |
'Attribute 3 visible': 'visible',
|
167 |
'Attribute 3 global': 1,
|
168 |
+
'Meta: outline': parent_products['Formatted_Agenda'],
|
169 |
'Meta: days': parent_products['Duration'],
|
170 |
'Meta: location': 'Virtual',
|
171 |
'Meta: overview': parent_products['Target Audience'],
|
172 |
+
'Meta: objectives': parent_products['Formatted_Objectives'],
|
173 |
+
'Meta: prerequisites': parent_products['Formatted_Prerequisites'],
|
174 |
+
'Meta: agenda': parent_products['Formatted_Agenda']
|
175 |
})
|
176 |
|
177 |
# 6. Create child (variation) products
|
|
|
181 |
'Name': netcom_df['Course Name'],
|
182 |
'Published': 1,
|
183 |
'Visibility in catalog': 'visible',
|
184 |
+
'Short description': netcom_df['Short_Description'],
|
185 |
+
'Description': netcom_df['Condensed_Description'],
|
186 |
'Tax status': 'taxable',
|
187 |
'In stock?': 1,
|
|
|
|
|
188 |
'Regular price': netcom_df['SRP Pricing'].replace('[\$,]', '', regex=True),
|
189 |
'Categories': 'courses',
|
190 |
'Images': netcom_df['Vendor'].map(brand_logo_map).fillna(''),
|
|
|
204 |
),
|
205 |
'Attribute 3 visible': 'visible',
|
206 |
'Attribute 3 global': 1,
|
207 |
+
'Meta: outline': netcom_df['Formatted_Agenda'],
|
208 |
'Meta: days': netcom_df['Duration'],
|
209 |
'Meta: location': 'Virtual',
|
210 |
'Meta: overview': netcom_df['Target Audience'],
|
211 |
+
'Meta: objectives': netcom_df['Formatted_Objectives'],
|
212 |
+
'Meta: prerequisites': netcom_df['Formatted_Prerequisites'],
|
213 |
+
'Meta: agenda': netcom_df['Formatted_Agenda']
|
214 |
})
|
215 |
|
216 |
# 7. Combine parent + child
|
217 |
woo_final_df = pd.concat([woo_parent_df, woo_child_df], ignore_index=True)
|
218 |
|
219 |
+
# 8. Desired column order (removed Stock and Sold individually?)
|
220 |
column_order = [
|
221 |
'Type', 'SKU', 'Name', 'Published', 'Visibility in catalog',
|
222 |
'Short description', 'Description', 'Tax status', 'In stock?',
|
223 |
+
'Regular price', 'Categories', 'Images',
|
224 |
'Parent', 'Brands', 'Attribute 1 name', 'Attribute 1 value(s)', 'Attribute 1 visible',
|
225 |
'Attribute 1 global', 'Attribute 2 name', 'Attribute 2 value(s)', 'Attribute 2 visible',
|
226 |
'Attribute 2 global', 'Attribute 3 name', 'Attribute 3 value(s)', 'Attribute 3 visible',
|
|
|
261 |
)
|
262 |
|
263 |
if __name__ == "__main__":
|
264 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
265 |
app.launch()
|