delightfulrachel commited on
Commit
aab9b8a
·
verified ·
1 Parent(s): 506a965

update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -130
app.py CHANGED
@@ -1,44 +1,37 @@
1
  import gradio as gr
2
  import os
3
  import time
4
- import json
5
  import requests
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 api_key
25
 
26
- # Function to call LLM using REST API directly
27
  def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
28
  api_key = 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 the corrected code clearly."
32
-
33
- start_time = time.time()
34
-
35
  try:
36
  headers = {
37
  "Authorization": f"Bearer {api_key}",
38
  "Content-Type": "application/json"
39
  }
40
-
41
- data = {
42
  "model": model,
43
  "messages": [
44
  {"role": "system", "content": system_message},
@@ -48,35 +41,23 @@ def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
48
  "max_tokens": max_tokens,
49
  "top_p": 0.9
50
  }
51
-
52
- response = requests.post(
53
- "https://api.together.xyz/v1/chat/completions",
54
- headers=headers,
55
- json=data
56
  )
57
-
58
- if response.status_code != 200:
59
- return f"Error: API returned status code {response.status_code}: {response.text}"
60
-
61
- response_data = response.json()
62
- completion_text = response_data["choices"][0]["message"]["content"]
63
-
64
- end_time = time.time()
65
- duration = end_time - start_time
66
-
67
- # Add duration to the completion text if needed
68
- # completion_text += f"\n\nResponse time: {duration:.2f} seconds"
69
-
70
- return completion_text
71
-
72
  except Exception as e:
73
- return f"Error calling API: {str(e)}"
74
 
75
- # For Apex Trigger correction
76
  def correct_apex_trigger(model, trigger_code):
77
  if not trigger_code.strip():
78
  return "Please provide Apex Trigger code to correct."
79
-
80
  prompt = f"""
81
  Please analyze and correct the following Apex Trigger code for migration from CloudCraze to B2B Lightning Experience.
82
  Identify any issues, optimize it according to best practices, and provide the corrected code.
@@ -85,111 +66,101 @@ Identify any issues, optimize it according to best practices, and provide the co
85
  {trigger_code}
86
  ```
87
 
88
- Please return the corrected code with explanations of what was changed and why.
89
  """
90
-
91
  return call_llm(model, prompt)
92
 
93
- # For CloudCraze Object conversion
94
  def convert_cc_object(model, cc_object_code):
95
  if not cc_object_code.strip():
96
  return "Please provide CloudCraze Object code to convert."
97
-
98
  prompt = f"""
99
  Please convert the following CloudCraze Object code to B2B Lightning Experience format.
100
- Identify the appropriate B2B LEx system object that corresponds to this CloudCraze object,
101
- map the fields correctly, and provide the complete definition for the B2B LEx version.
102
 
103
  ```
104
  {cc_object_code}
105
  ```
106
 
107
- Please return:
108
- 1. The equivalent B2B LEx system object name
109
- 2. Field mapping between CloudCraze and B2B LEx
110
- 3. The complete definition for the B2B LEx implementation
111
- 4. Any additional steps needed for proper migration
112
  """
113
-
114
  return call_llm(model, prompt)
115
 
116
- # Gradio Interface
117
- with gr.Blocks(title="Salesforce B2B Commerce Migration Assistant") as app:
118
- gr.Markdown("# Salesforce B2B Commerce Migration Assistant")
119
- gr.Markdown("This tool helps with migrating from CloudCraze to B2B Lightning Experience")
120
-
121
- model_dropdown = gr.Dropdown(
122
- choices=TOGETHER_MODELS,
123
- value=TOGETHER_MODELS[0],
124
- label="Select AI Model"
125
- )
126
-
127
- with gr.Tab("Apex Trigger Correction"):
128
- gr.Markdown("### Apex Trigger Correction")
129
- gr.Markdown("Paste your Apex Trigger code below to correct issues for B2B LEx compatibility")
130
-
131
- trigger_input = gr.Textbox(
132
- lines=15,
133
- placeholder="Paste your Apex Trigger code here...",
134
- label="Apex Trigger Code"
135
- )
136
-
137
- # Make output multiline, interactive (allow scroll)
138
- trigger_output = gr.Textbox(
139
- lines=20,
140
- label="Corrected Apex Trigger",
141
- interactive=True # allows user to select/copy text easily
142
- )
143
-
144
- # Add the Correct button with loading indicator
145
- trigger_button = gr.Button("Correct Apex Trigger")
146
- trigger_button.click(
147
- fn=correct_apex_trigger,
148
- inputs=[model_dropdown, trigger_input],
149
- outputs=trigger_output,
150
- show_progress=True # this enables loading spinner on button
151
- )
152
-
153
- # Add a Clear button to reset the output textbox
154
- trigger_clear = gr.Button("Clear")
155
- trigger_clear.click(lambda: "", [], trigger_output)
156
-
157
- with gr.Tab("CloudCraze Object Conversion"):
158
- gr.Markdown("### CloudCraze to B2B LEx Object Conversion")
159
- gr.Markdown("Paste your CloudCraze Object code to convert it to B2B Lightning Experience format")
160
-
161
- object_input = gr.Textbox(
162
- lines=15,
163
- placeholder="Paste your CloudCraze Object code here...",
164
- label="CloudCraze Object Code"
165
- )
166
-
167
- object_output = gr.Textbox(
168
- lines=20,
169
- label="Converted B2B LEx Object",
170
- interactive=True
171
  )
172
-
173
- object_button = gr.Button("Convert Object")
174
- object_button.click(
175
- fn=convert_cc_object,
176
- inputs=[model_dropdown, object_input],
177
- outputs=object_output,
178
- show_progress=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  )
180
-
181
- object_clear = gr.Button("Clear")
182
- object_clear.click(lambda: "", [], object_output)
183
-
184
- gr.Markdown("### About This Tool")
185
- gr.Markdown("""
186
- This tool uses advanced AI models to assist with Salesforce B2B Commerce migrations.
187
- - The Apex Trigger Correction tool helps fix trigger code for B2B Lightning Experience compatibility
188
- - The CloudCraze Object Conversion tool maps CC objects to their B2B LEx equivalents
189
-
190
- **Note**: Always review AI-generated output before implementing in production environments.
191
- """)
192
-
193
- # Launch the app
194
  if __name__ == "__main__":
195
- app.launch()
 
1
  import gradio as gr
2
  import os
3
  import time
 
4
  import requests
5
 
 
 
 
 
6
  # Model options for dropdown
7
+ together_models = [
8
  "Qwen/Qwen2.5-Coder-32B-Instruct",
9
  "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF",
10
  "deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
11
  "meta-llama/Llama-3.3-70B-Instruct-Turbo-Free"
12
  ]
13
 
14
+ # Helper to fetch API key from environment
15
  def get_together_client():
16
+ api_key = os.getenv("TOGETHER_API_KEY")
17
  if not api_key:
18
+ raise ValueError("TOGETHER_API_KEY not set. Please add it in Space secrets.")
19
  return api_key
20
 
21
+ # Call Together AI via REST API
22
  def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
23
  api_key = get_together_client()
24
+ system_message = (
25
+ "You are a Salesforce development expert specializing in B2B Commerce migrations,"
26
+ " CloudCraze to B2B Lightning Experience conversions, and Apex code optimization."
27
+ )
28
+ start = time.time()
 
29
  try:
30
  headers = {
31
  "Authorization": f"Bearer {api_key}",
32
  "Content-Type": "application/json"
33
  }
34
+ payload = {
 
35
  "model": model,
36
  "messages": [
37
  {"role": "system", "content": system_message},
 
41
  "max_tokens": max_tokens,
42
  "top_p": 0.9
43
  }
44
+ resp = requests.post(
45
+ "https://api.together.xyz/v1/chat/completions",
46
+ headers=headers,
47
+ json=payload
 
48
  )
49
+ if resp.status_code != 200:
50
+ return f"Error: Status {resp.status_code}: {resp.text}"
51
+ data = resp.json()
52
+ text = data["choices"][0]["message"]["content"]
53
+ return text
 
 
 
 
 
 
 
 
 
 
54
  except Exception as e:
55
+ return f"Error calling API: {e}"
56
 
57
+ # Build prompt for trigger correction
58
  def correct_apex_trigger(model, trigger_code):
59
  if not trigger_code.strip():
60
  return "Please provide Apex Trigger code to correct."
 
61
  prompt = f"""
62
  Please analyze and correct the following Apex Trigger code for migration from CloudCraze to B2B Lightning Experience.
63
  Identify any issues, optimize it according to best practices, and provide the corrected code.
 
66
  {trigger_code}
67
  ```
68
 
69
+ Please return the corrected code with explanations of changes.
70
  """
 
71
  return call_llm(model, prompt)
72
 
73
+ # Build prompt for object conversion
74
  def convert_cc_object(model, cc_object_code):
75
  if not cc_object_code.strip():
76
  return "Please provide CloudCraze Object code to convert."
 
77
  prompt = f"""
78
  Please convert the following CloudCraze Object code to B2B Lightning Experience format.
79
+ Identify the corresponding B2B LEx system object, map fields, and provide the full definition.
 
80
 
81
  ```
82
  {cc_object_code}
83
  ```
84
 
85
+ Return:
86
+ 1. Equivalent B2B LEx object name
87
+ 2. Field mappings
88
+ 3. Full implementation code
89
+ 4. Additional steps if needed
90
  """
 
91
  return call_llm(model, prompt)
92
 
93
+ # Gradio App
94
+ def main():
95
+ with gr.Blocks(title="Salesforce B2B Commerce Migration Assistant") as app:
96
+ gr.Markdown("# Salesforce B2B Commerce Migration Assistant")
97
+ gr.Markdown("This tool helps migrate CloudCraze code to B2B Lightning Experience.")
98
+
99
+ model_dropdown = gr.Dropdown(
100
+ choices=together_models,
101
+ value=together_models[0],
102
+ label="Select AI Model"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  )
104
+
105
+ with gr.Tab("Apex Trigger Correction"):
106
+ gr.Markdown("### Apex Trigger Correction")
107
+ gr.Markdown("Paste your Apex Trigger code below:")
108
+
109
+ trigger_input = gr.Textbox(
110
+ lines=15,
111
+ placeholder="Paste Apex Trigger code here...",
112
+ label="Apex Trigger Code"
113
+ )
114
+ trigger_output = gr.Textbox(
115
+ lines=20,
116
+ label="Corrected Apex Trigger",
117
+ interactive=True
118
+ )
119
+ trigger_button = gr.Button("Correct Apex Trigger")
120
+ trigger_button.click(
121
+ fn=correct_apex_trigger,
122
+ inputs=[model_dropdown, trigger_input],
123
+ outputs=trigger_output,
124
+ show_progress=True
125
+ )
126
+ trigger_clear = gr.Button("Clear")
127
+ trigger_clear.click(lambda: "", [], trigger_output)
128
+
129
+ with gr.Tab("CloudCraze Object Conversion"):
130
+ gr.Markdown("### CloudCraze Object Conversion")
131
+ gr.Markdown("Paste your CloudCraze Object code below:")
132
+
133
+ object_input = gr.Textbox(
134
+ lines=15,
135
+ placeholder="Paste CC object code here...",
136
+ label="CloudCraze Object Code"
137
+ )
138
+ object_output = gr.Textbox(
139
+ lines=20,
140
+ label="Converted B2B LEx Object",
141
+ interactive=True
142
+ )
143
+ object_button = gr.Button("Convert Object")
144
+ object_button.click(
145
+ fn=convert_cc_object,
146
+ inputs=[model_dropdown, object_input],
147
+ outputs=object_output,
148
+ show_progress=True
149
+ )
150
+ object_clear = gr.Button("Clear")
151
+ object_clear.click(lambda: "", [], object_output)
152
+
153
+ gr.Markdown("### About This Tool")
154
+ gr.Markdown(
155
+ """
156
+ - **Trigger Correction**: Fixes Apex Triggers for B2B LEx compatibility.
157
+ - **Object Conversion**: Maps and converts CloudCraze object definitions to B2B LEx.
158
+
159
+ Always review AI-generated code before production use.
160
+ """
161
  )
162
+
163
+ app.launch()
164
+
 
 
 
 
 
 
 
 
 
 
 
165
  if __name__ == "__main__":
166
+ main()