Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
import requests
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Model options for dropdown with both Together AI and Anthropic models
|
| 7 |
together_models = [
|
|
@@ -18,7 +20,19 @@ anthropic_models = [
|
|
| 18 |
|
| 19 |
all_models = together_models + anthropic_models
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def get_api_key(provider):
|
| 23 |
if provider == "together":
|
| 24 |
api_key = os.getenv("TOGETHER_API_KEY")
|
|
@@ -33,7 +47,6 @@ def get_api_key(provider):
|
|
| 33 |
else:
|
| 34 |
raise ValueError(f"Unknown provider: {provider}")
|
| 35 |
|
| 36 |
-
# Determine the provider based on model name
|
| 37 |
def get_provider(model):
|
| 38 |
if model in together_models:
|
| 39 |
return "together"
|
|
@@ -42,14 +55,12 @@ def get_provider(model):
|
|
| 42 |
else:
|
| 43 |
raise ValueError(f"Unknown model: {model}")
|
| 44 |
|
| 45 |
-
# Call Together AI via REST API
|
| 46 |
def call_together_api(model, prompt, temperature=0.7, max_tokens=1500):
|
| 47 |
api_key = get_api_key("together")
|
| 48 |
system_message = (
|
| 49 |
"You are a Salesforce development expert specializing in B2B Commerce migrations,"
|
| 50 |
" CloudCraze to B2B Lightning Experience conversions, and Apex code optimization."
|
| 51 |
)
|
| 52 |
-
start = time.time()
|
| 53 |
try:
|
| 54 |
headers = {
|
| 55 |
"Authorization": f"Bearer {api_key}",
|
|
@@ -78,14 +89,12 @@ def call_together_api(model, prompt, temperature=0.7, max_tokens=1500):
|
|
| 78 |
except Exception as e:
|
| 79 |
return f"Error calling Together AI API: {e}"
|
| 80 |
|
| 81 |
-
# Call Anthropic API
|
| 82 |
def call_anthropic_api(model, prompt, temperature=0.7, max_tokens=1500):
|
| 83 |
api_key = get_api_key("anthropic")
|
| 84 |
system_message = (
|
| 85 |
"You are a Salesforce development expert specializing in B2B Commerce migrations,"
|
| 86 |
" CloudCraze to B2B Lightning Experience conversions, and Apex code optimization."
|
| 87 |
)
|
| 88 |
-
start = time.time()
|
| 89 |
try:
|
| 90 |
headers = {
|
| 91 |
"x-api-key": api_key,
|
|
@@ -114,7 +123,6 @@ def call_anthropic_api(model, prompt, temperature=0.7, max_tokens=1500):
|
|
| 114 |
except Exception as e:
|
| 115 |
return f"Error calling Anthropic API: {e}"
|
| 116 |
|
| 117 |
-
# Unified function to call the appropriate API based on the model
|
| 118 |
def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
|
| 119 |
provider = get_provider(model)
|
| 120 |
if provider == "together":
|
|
@@ -124,7 +132,6 @@ def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
|
|
| 124 |
else:
|
| 125 |
return f"Error: Unknown provider for model {model}"
|
| 126 |
|
| 127 |
-
# Build prompt for trigger correction
|
| 128 |
def correct_apex_trigger(model, trigger_code):
|
| 129 |
if not trigger_code.strip():
|
| 130 |
return "Please provide Apex Trigger code to correct."
|
|
@@ -133,226 +140,4 @@ Please analyze and correct the following Apex Trigger code for migration from Cl
|
|
| 133 |
Identify any issues, optimize it according to best practices, and provide the corrected code.
|
| 134 |
|
| 135 |
```apex
|
| 136 |
-
{trigger_code}
|
| 137 |
-
```
|
| 138 |
-
|
| 139 |
-
Please return the corrected code with explanations of changes.
|
| 140 |
-
"""
|
| 141 |
-
return call_llm(model, prompt)
|
| 142 |
-
|
| 143 |
-
# Build prompt for object conversion
|
| 144 |
-
def convert_cc_object(model, cc_object_code):
|
| 145 |
-
if not cc_object_code.strip():
|
| 146 |
-
return "Please provide CloudCraze Object code to convert."
|
| 147 |
-
prompt = f"""
|
| 148 |
-
Please convert the following CloudCraze Object code to B2B Lightning Experience format.
|
| 149 |
-
Identify the corresponding B2B LEx system object, map fields, and provide the full definition.
|
| 150 |
-
|
| 151 |
-
```
|
| 152 |
-
{cc_object_code}
|
| 153 |
-
```
|
| 154 |
-
|
| 155 |
-
Return:
|
| 156 |
-
1. Equivalent B2B LEx object name
|
| 157 |
-
2. Field mappings
|
| 158 |
-
3. Full implementation code
|
| 159 |
-
4. Additional steps if needed
|
| 160 |
-
"""
|
| 161 |
-
return call_llm(model, prompt)
|
| 162 |
-
|
| 163 |
-
# Validation function for Apex Trigger correction
|
| 164 |
-
def validate_apex_trigger(validation_model, original_code, corrected_code):
|
| 165 |
-
if not validation_model or not original_code.strip() or not corrected_code.strip():
|
| 166 |
-
return "Please provide all required inputs for validation."
|
| 167 |
-
|
| 168 |
-
prompt = f"""
|
| 169 |
-
I need you to validate and review the following Apex Trigger code migration from CloudCraze to B2B Lightning Experience.
|
| 170 |
-
|
| 171 |
-
ORIGINAL CODE:
|
| 172 |
-
```apex
|
| 173 |
-
{original_code}
|
| 174 |
-
```
|
| 175 |
-
|
| 176 |
-
CORRECTED CODE:
|
| 177 |
-
```apex
|
| 178 |
-
{corrected_code}
|
| 179 |
-
```
|
| 180 |
-
|
| 181 |
-
Please review the corrected code and provide:
|
| 182 |
-
1. Is the correction accurate and complete? Highlight any missed issues.
|
| 183 |
-
2. Are there any potential runtime errors or performance concerns?
|
| 184 |
-
3. Does it follow Salesforce best practices for B2B Lightning Experience?
|
| 185 |
-
4. Would you recommend any additional improvements?
|
| 186 |
-
5. Rate the quality of the correction on a scale of 1-10 with explanation.
|
| 187 |
-
|
| 188 |
-
Be thorough and detailed in your assessment.
|
| 189 |
-
"""
|
| 190 |
-
return call_llm(validation_model, prompt)
|
| 191 |
-
|
| 192 |
-
# Validation function for object conversion
|
| 193 |
-
def validate_cc_object_conversion(validation_model, original_object, converted_object):
|
| 194 |
-
if not validation_model or not original_object.strip() or not converted_object.strip():
|
| 195 |
-
return "Please provide all required inputs for validation."
|
| 196 |
-
|
| 197 |
-
prompt = f"""
|
| 198 |
-
I need you to validate and review the following CloudCraze Object conversion to B2B Lightning Experience format.
|
| 199 |
-
|
| 200 |
-
ORIGINAL CLOUDCRAZE OBJECT:
|
| 201 |
-
```
|
| 202 |
-
{original_object}
|
| 203 |
-
```
|
| 204 |
-
|
| 205 |
-
CONVERTED B2B LEX OBJECT:
|
| 206 |
-
```
|
| 207 |
-
{converted_object}
|
| 208 |
-
```
|
| 209 |
-
|
| 210 |
-
Please review the conversion and provide:
|
| 211 |
-
1. Is the object mapping correct and complete? Identify any missed fields or relationships.
|
| 212 |
-
2. Are there any potential data migration issues or concerns?
|
| 213 |
-
3. Does the converted object follow B2B Lightning Experience best practices?
|
| 214 |
-
4. Would you recommend any additional improvements or optimizations?
|
| 215 |
-
5. Rate the quality of the conversion on a scale of 1-10 with explanation.
|
| 216 |
-
|
| 217 |
-
Be thorough and detailed in your assessment.
|
| 218 |
-
"""
|
| 219 |
-
return call_llm(validation_model, prompt)
|
| 220 |
-
|
| 221 |
-
# Gradio App
|
| 222 |
-
def main():
|
| 223 |
-
with gr.Blocks(title="Salesforce B2B Commerce Migration Assistant") as app:
|
| 224 |
-
gr.Markdown("# Salesforce B2B Commerce Migration Assistant")
|
| 225 |
-
gr.Markdown("This tool helps migrate CloudCraze code to B2B Lightning Experience.")
|
| 226 |
-
|
| 227 |
-
# Create model dropdowns
|
| 228 |
-
with gr.Row():
|
| 229 |
-
with gr.Column():
|
| 230 |
-
gr.Markdown("### Primary Model")
|
| 231 |
-
primary_model_dropdown = gr.Dropdown(
|
| 232 |
-
choices=all_models,
|
| 233 |
-
value=anthropic_models[0], # Default to Claude 3.7 Sonnet
|
| 234 |
-
label="Select Primary AI Model for Conversion"
|
| 235 |
-
)
|
| 236 |
-
|
| 237 |
-
with gr.Column():
|
| 238 |
-
gr.Markdown("### Validation Model")
|
| 239 |
-
validation_model_dropdown = gr.Dropdown(
|
| 240 |
-
choices=all_models,
|
| 241 |
-
value=anthropic_models[1], # Default to Claude 3 Haiku
|
| 242 |
-
label="Select Validation AI Model for Review",
|
| 243 |
-
info="This model will validate and review the output from the primary model"
|
| 244 |
-
)
|
| 245 |
-
|
| 246 |
-
with gr.Tab("Apex Trigger Correction"):
|
| 247 |
-
gr.Markdown("### Apex Trigger Correction")
|
| 248 |
-
gr.Markdown("Paste your Apex Trigger code below:")
|
| 249 |
-
|
| 250 |
-
trigger_input = gr.Textbox(
|
| 251 |
-
lines=12,
|
| 252 |
-
placeholder="Paste Apex Trigger code here...",
|
| 253 |
-
label="Apex Trigger Code"
|
| 254 |
-
)
|
| 255 |
-
trigger_output = gr.Textbox(
|
| 256 |
-
lines=15,
|
| 257 |
-
label="Corrected Apex Trigger",
|
| 258 |
-
interactive=True
|
| 259 |
-
)
|
| 260 |
-
trigger_button = gr.Button("Correct Apex Trigger")
|
| 261 |
-
trigger_button.click(
|
| 262 |
-
fn=correct_apex_trigger,
|
| 263 |
-
inputs=[primary_model_dropdown, trigger_input],
|
| 264 |
-
outputs=trigger_output,
|
| 265 |
-
show_progress=True
|
| 266 |
-
)
|
| 267 |
-
|
| 268 |
-
gr.Markdown("### Validation Results")
|
| 269 |
-
trigger_validation_output = gr.Textbox(
|
| 270 |
-
lines=15,
|
| 271 |
-
label="Validation Assessment",
|
| 272 |
-
placeholder="Validation results will appear here after clicking 'Validate Correction'",
|
| 273 |
-
interactive=True
|
| 274 |
-
)
|
| 275 |
-
validate_trigger_button = gr.Button("Validate Correction")
|
| 276 |
-
validate_trigger_button.click(
|
| 277 |
-
fn=validate_apex_trigger,
|
| 278 |
-
inputs=[validation_model_dropdown, trigger_input, trigger_output],
|
| 279 |
-
outputs=trigger_validation_output,
|
| 280 |
-
show_progress=True
|
| 281 |
-
)
|
| 282 |
-
|
| 283 |
-
with gr.Row():
|
| 284 |
-
trigger_clear = gr.Button("Clear Input")
|
| 285 |
-
trigger_clear.click(lambda: "", [], trigger_input)
|
| 286 |
-
|
| 287 |
-
results_clear = gr.Button("Clear Results")
|
| 288 |
-
results_clear.click(
|
| 289 |
-
lambda: ["", ""],
|
| 290 |
-
[],
|
| 291 |
-
[trigger_output, trigger_validation_output]
|
| 292 |
-
)
|
| 293 |
-
|
| 294 |
-
with gr.Tab("CloudCraze Object Conversion"):
|
| 295 |
-
gr.Markdown("### CloudCraze Object Conversion")
|
| 296 |
-
gr.Markdown("Paste your CloudCraze Object code below:")
|
| 297 |
-
|
| 298 |
-
object_input = gr.Textbox(
|
| 299 |
-
lines=12,
|
| 300 |
-
placeholder="Paste CC object code here...",
|
| 301 |
-
label="CloudCraze Object Code"
|
| 302 |
-
)
|
| 303 |
-
object_output = gr.Textbox(
|
| 304 |
-
lines=15,
|
| 305 |
-
label="Converted B2B LEx Object",
|
| 306 |
-
interactive=True
|
| 307 |
-
)
|
| 308 |
-
object_button = gr.Button("Convert Object")
|
| 309 |
-
object_button.click(
|
| 310 |
-
fn=convert_cc_object,
|
| 311 |
-
inputs=[primary_model_dropdown, object_input],
|
| 312 |
-
outputs=object_output,
|
| 313 |
-
show_progress=True
|
| 314 |
-
)
|
| 315 |
-
|
| 316 |
-
gr.Markdown("### Validation Results")
|
| 317 |
-
object_validation_output = gr.Textbox(
|
| 318 |
-
lines=15,
|
| 319 |
-
label="Validation Assessment",
|
| 320 |
-
placeholder="Validation results will appear here after clicking 'Validate Conversion'",
|
| 321 |
-
interactive=True
|
| 322 |
-
)
|
| 323 |
-
validate_object_button = gr.Button("Validate Conversion")
|
| 324 |
-
validate_object_button.click(
|
| 325 |
-
fn=validate_cc_object_conversion,
|
| 326 |
-
inputs=[validation_model_dropdown, object_input, object_output],
|
| 327 |
-
outputs=object_validation_output,
|
| 328 |
-
show_progress=True
|
| 329 |
-
)
|
| 330 |
-
|
| 331 |
-
with gr.Row():
|
| 332 |
-
object_clear = gr.Button("Clear Input")
|
| 333 |
-
object_clear.click(lambda: "", [], object_input)
|
| 334 |
-
|
| 335 |
-
object_results_clear = gr.Button("Clear Results")
|
| 336 |
-
object_results_clear.click(
|
| 337 |
-
lambda: ["", ""],
|
| 338 |
-
[],
|
| 339 |
-
[object_output, object_validation_output]
|
| 340 |
-
)
|
| 341 |
-
|
| 342 |
-
gr.Markdown("### About This Tool")
|
| 343 |
-
gr.Markdown(
|
| 344 |
-
"""
|
| 345 |
-
- **Primary Model**: Performs the initial code conversion or correction.
|
| 346 |
-
- **Validation Model**: Reviews and validates the output from the primary model, identifying potential issues or improvements.
|
| 347 |
-
- **Trigger Correction**: Fixes Apex Triggers for B2B LEx compatibility.
|
| 348 |
-
- **Object Conversion**: Maps and converts CloudCraze object definitions to B2B LEx.
|
| 349 |
-
- **Model Selection**: Choose from Together AI models or Anthropic's Claude models.
|
| 350 |
-
|
| 351 |
-
Always review AI-generated code before production use.
|
| 352 |
-
"""
|
| 353 |
-
)
|
| 354 |
-
|
| 355 |
-
app.launch()
|
| 356 |
-
|
| 357 |
-
if __name__ == "__main__":
|
| 358 |
-
main()
|
|
|
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
import requests
|
| 5 |
+
import json
|
| 6 |
+
import plotly.express as px
|
| 7 |
|
| 8 |
# Model options for dropdown with both Together AI and Anthropic models
|
| 9 |
together_models = [
|
|
|
|
| 20 |
|
| 21 |
all_models = together_models + anthropic_models
|
| 22 |
|
| 23 |
+
VALIDATION_SCHEMA = {
|
| 24 |
+
"quality_rating": "int (1–10)",
|
| 25 |
+
"accuracy": "float (0.0–1.0)",
|
| 26 |
+
"completeness": "float (0.0–1.0)",
|
| 27 |
+
"best_practices_alignment": "float (0.0–1.0)",
|
| 28 |
+
"explanations": {
|
| 29 |
+
"quality_rating": "string",
|
| 30 |
+
"accuracy": "string",
|
| 31 |
+
"completeness": "string",
|
| 32 |
+
"best_practices_alignment": "string"
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
def get_api_key(provider):
|
| 37 |
if provider == "together":
|
| 38 |
api_key = os.getenv("TOGETHER_API_KEY")
|
|
|
|
| 47 |
else:
|
| 48 |
raise ValueError(f"Unknown provider: {provider}")
|
| 49 |
|
|
|
|
| 50 |
def get_provider(model):
|
| 51 |
if model in together_models:
|
| 52 |
return "together"
|
|
|
|
| 55 |
else:
|
| 56 |
raise ValueError(f"Unknown model: {model}")
|
| 57 |
|
|
|
|
| 58 |
def call_together_api(model, prompt, temperature=0.7, max_tokens=1500):
|
| 59 |
api_key = get_api_key("together")
|
| 60 |
system_message = (
|
| 61 |
"You are a Salesforce development expert specializing in B2B Commerce migrations,"
|
| 62 |
" CloudCraze to B2B Lightning Experience conversions, and Apex code optimization."
|
| 63 |
)
|
|
|
|
| 64 |
try:
|
| 65 |
headers = {
|
| 66 |
"Authorization": f"Bearer {api_key}",
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
return f"Error calling Together AI API: {e}"
|
| 91 |
|
|
|
|
| 92 |
def call_anthropic_api(model, prompt, temperature=0.7, max_tokens=1500):
|
| 93 |
api_key = get_api_key("anthropic")
|
| 94 |
system_message = (
|
| 95 |
"You are a Salesforce development expert specializing in B2B Commerce migrations,"
|
| 96 |
" CloudCraze to B2B Lightning Experience conversions, and Apex code optimization."
|
| 97 |
)
|
|
|
|
| 98 |
try:
|
| 99 |
headers = {
|
| 100 |
"x-api-key": api_key,
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
return f"Error calling Anthropic API: {e}"
|
| 125 |
|
|
|
|
| 126 |
def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
|
| 127 |
provider = get_provider(model)
|
| 128 |
if provider == "together":
|
|
|
|
| 132 |
else:
|
| 133 |
return f"Error: Unknown provider for model {model}"
|
| 134 |
|
|
|
|
| 135 |
def correct_apex_trigger(model, trigger_code):
|
| 136 |
if not trigger_code.strip():
|
| 137 |
return "Please provide Apex Trigger code to correct."
|
|
|
|
| 140 |
Identify any issues, optimize it according to best practices, and provide the corrected code.
|
| 141 |
|
| 142 |
```apex
|
| 143 |
+
{trigger_code}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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