create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
import json
|
5 |
+
from together import Together
|
6 |
+
|
7 |
+
# Set environment variables
|
8 |
+
# Note: For Hugging Face Spaces, you'll set these in the Secrets tab
|
9 |
+
# os.environ["TOGETHER_API_KEY"] = "your-api-key-here"
|
10 |
+
|
11 |
+
# Model options for dropdown
|
12 |
+
TOGETHER_MODELS = [
|
13 |
+
"Qwen/Qwen2.5-Coder-32B-Instruct",
|
14 |
+
"nvidia/Llama-3.1-Nemotron-70B-Instruct-HF",
|
15 |
+
"deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
|
16 |
+
"meta-llama/Llama-3.3-70B-Instruct-Turbo-Free"
|
17 |
+
]
|
18 |
+
|
19 |
+
# Initialize Together client
|
20 |
+
def get_together_client():
|
21 |
+
api_key = os.environ.get("TOGETHER_API_KEY")
|
22 |
+
if not api_key:
|
23 |
+
raise ValueError("TOGETHER_API_KEY not found. Please set it in the environment variables.")
|
24 |
+
return Together(api_key=api_key)
|
25 |
+
|
26 |
+
# Function to call LLM
|
27 |
+
def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
|
28 |
+
client = get_together_client()
|
29 |
+
|
30 |
+
# System message for Salesforce expertise
|
31 |
+
system_message = "You are a Salesforce development expert specializing in B2B Commerce migrations, CloudCraze to B2B Lightning Experience conversions, and Apex code optimization. Provide clear, step-by-step guidance and corrected code."
|
32 |
+
|
33 |
+
start_time = time.time()
|
34 |
+
|
35 |
+
try:
|
36 |
+
response = client.chat.completions.create(
|
37 |
+
model=model,
|
38 |
+
messages=[
|
39 |
+
{"role": "system", "content": system_message},
|
40 |
+
{"role": "user", "content": prompt}
|
41 |
+
],
|
42 |
+
temperature=temperature,
|
43 |
+
max_tokens=max_tokens,
|
44 |
+
top_p=0.9
|
45 |
+
)
|
46 |
+
|
47 |
+
completion_text = response.choices[0].message.content
|
48 |
+
|
49 |
+
end_time = time.time()
|
50 |
+
duration = end_time - start_time
|
51 |
+
|
52 |
+
# Add duration to the completion text if needed
|
53 |
+
# completion_text += f"\n\nResponse time: {duration:.2f} seconds"
|
54 |
+
|
55 |
+
return completion_text
|
56 |
+
|
57 |
+
except Exception as e:
|
58 |
+
return f"Error calling API: {str(e)}"
|
59 |
+
|
60 |
+
# For Apex Trigger correction
|
61 |
+
def correct_apex_trigger(model, trigger_code):
|
62 |
+
if not trigger_code.strip():
|
63 |
+
return "Please provide Apex Trigger code to correct."
|
64 |
+
|
65 |
+
prompt = f"""
|
66 |
+
Please analyze and correct the following Apex Trigger code for migration from CloudCraze to B2B Lightning Experience.
|
67 |
+
Identify any issues, optimize it according to best practices, and provide the corrected code.
|
68 |
+
|
69 |
+
```apex
|
70 |
+
{trigger_code}
|
71 |
+
```
|
72 |
+
|
73 |
+
Please return the corrected code with explanations of what was changed and why.
|
74 |
+
"""
|
75 |
+
|
76 |
+
return call_llm(model, prompt)
|
77 |
+
|
78 |
+
# For CloudCraze Object conversion
|
79 |
+
def convert_cc_object(model, cc_object_code):
|
80 |
+
if not cc_object_code.strip():
|
81 |
+
return "Please provide CloudCraze Object code to convert."
|
82 |
+
|
83 |
+
prompt = f"""
|
84 |
+
Please convert the following CloudCraze Object code to B2B Lightning Experience format.
|
85 |
+
Identify the appropriate B2B LEx system object that corresponds to this CloudCraze object,
|
86 |
+
map the fields correctly, and provide the complete definition for the B2B LEx version.
|
87 |
+
|
88 |
+
```
|
89 |
+
{cc_object_code}
|
90 |
+
```
|
91 |
+
|
92 |
+
Please return:
|
93 |
+
1. The equivalent B2B LEx system object name
|
94 |
+
2. Field mapping between CloudCraze and B2B LEx
|
95 |
+
3. The complete definition for the B2B LEx implementation
|
96 |
+
4. Any additional steps needed for proper migration
|
97 |
+
"""
|
98 |
+
|
99 |
+
return call_llm(model, prompt)
|
100 |
+
|
101 |
+
# Gradio Interface
|
102 |
+
with gr.Blocks(title="Salesforce B2B Commerce Migration Assistant") as app:
|
103 |
+
gr.Markdown("# Salesforce B2B Commerce Migration Assistant")
|
104 |
+
gr.Markdown("This tool helps with migrating from CloudCraze to B2B Lightning Experience")
|
105 |
+
|
106 |
+
model_dropdown = gr.Dropdown(
|
107 |
+
choices=TOGETHER_MODELS,
|
108 |
+
value=TOGETHER_MODELS[0],
|
109 |
+
label="Select AI Model"
|
110 |
+
)
|
111 |
+
|
112 |
+
with gr.Tab("Apex Trigger Correction"):
|
113 |
+
gr.Markdown("### Apex Trigger Correction")
|
114 |
+
gr.Markdown("Paste your Apex Trigger code below to correct issues for B2B LEx compatibility")
|
115 |
+
|
116 |
+
trigger_input = gr.Textbox(
|
117 |
+
lines=15,
|
118 |
+
placeholder="Paste your Apex Trigger code here...",
|
119 |
+
label="Apex Trigger Code"
|
120 |
+
)
|
121 |
+
|
122 |
+
trigger_output = gr.Textbox(
|
123 |
+
lines=20,
|
124 |
+
label="Corrected Apex Trigger"
|
125 |
+
)
|
126 |
+
|
127 |
+
trigger_button = gr.Button("Correct Apex Trigger")
|
128 |
+
trigger_button.click(
|
129 |
+
fn=correct_apex_trigger,
|
130 |
+
inputs=[model_dropdown, trigger_input],
|
131 |
+
outputs=trigger_output
|
132 |
+
)
|
133 |
+
|
134 |
+
with gr.Tab("CloudCraze Object Conversion"):
|
135 |
+
gr.Markdown("### CloudCraze to B2B LEx Object Conversion")
|
136 |
+
gr.Markdown("Paste your CloudCraze Object code to convert it to B2B Lightning Experience format")
|
137 |
+
|
138 |
+
object_input = gr.Textbox(
|
139 |
+
lines=15,
|
140 |
+
placeholder="Paste your CloudCraze Object code here...",
|
141 |
+
label="CloudCraze Object Code"
|
142 |
+
)
|
143 |
+
|
144 |
+
object_output = gr.Textbox(
|
145 |
+
lines=20,
|
146 |
+
label="Converted B2B LEx Object"
|
147 |
+
)
|
148 |
+
|
149 |
+
object_button = gr.Button("Convert Object")
|
150 |
+
object_button.click(
|
151 |
+
fn=convert_cc_object,
|
152 |
+
inputs=[model_dropdown, object_input],
|
153 |
+
outputs=object_output
|
154 |
+
)
|
155 |
+
|
156 |
+
gr.Markdown("### About This Tool")
|
157 |
+
gr.Markdown("""
|
158 |
+
This tool uses advanced AI models to assist with Salesforce B2B Commerce migrations.
|
159 |
+
- The Apex Trigger Correction tool helps fix trigger code for B2B Lightning Experience compatibility
|
160 |
+
- The CloudCraze Object Conversion tool maps CC objects to their B2B LEx equivalents
|
161 |
+
|
162 |
+
**Note**: Always review AI-generated output before implementing in production environments.
|
163 |
+
""")
|
164 |
+
|
165 |
+
# Launch the app
|
166 |
+
if __name__ == "__main__":
|
167 |
+
app.launch()
|