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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -13
app.py CHANGED
@@ -3,7 +3,7 @@ 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",
@@ -11,16 +11,40 @@ together_models = [
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."
@@ -52,7 +76,53 @@ def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
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):
@@ -96,9 +166,10 @@ def main():
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
 
@@ -155,6 +226,7 @@ def main():
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
  """
@@ -163,4 +235,4 @@ Always review AI-generated code before production use.
163
  app.launch()
164
 
165
  if __name__ == "__main__":
166
- main()
 
3
  import time
4
  import requests
5
 
6
+ # Model options for dropdown with both Together AI and Anthropic models
7
  together_models = [
8
  "Qwen/Qwen2.5-Coder-32B-Instruct",
9
  "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF",
 
11
  "meta-llama/Llama-3.3-70B-Instruct-Turbo-Free"
12
  ]
13
 
14
+ anthropic_models = [
15
+ "claude-3-7-sonnet-20250219",
16
+ "claude-3-haiku-20240307"
17
+ ]
18
+
19
+ all_models = together_models + anthropic_models
20
+
21
+ # Helper to fetch API keys from environment
22
+ def get_api_key(provider):
23
+ if provider == "together":
24
+ api_key = os.getenv("TOGETHER_API_KEY")
25
+ if not api_key:
26
+ raise ValueError("TOGETHER_API_KEY not set. Please add it in Space secrets.")
27
+ return api_key
28
+ elif provider == "anthropic":
29
+ api_key = os.getenv("ANTHROPIC_API_KEY")
30
+ if not api_key:
31
+ raise ValueError("ANTHROPIC_API_KEY not set. Please add it in Space secrets.")
32
+ return api_key
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"
40
+ elif model in anthropic_models:
41
+ return "anthropic"
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."
 
76
  text = data["choices"][0]["message"]["content"]
77
  return text
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,
92
+ "anthropic-version": "2023-06-01",
93
+ "content-type": "application/json"
94
+ }
95
+ payload = {
96
+ "model": model,
97
+ "system": system_message,
98
+ "messages": [
99
+ {"role": "user", "content": prompt}
100
+ ],
101
+ "temperature": temperature,
102
+ "max_tokens": max_tokens
103
+ }
104
+ resp = requests.post(
105
+ "https://api.anthropic.com/v1/messages",
106
+ headers=headers,
107
+ json=payload
108
+ )
109
+ if resp.status_code != 200:
110
+ return f"Error: Status {resp.status_code}: {resp.text}"
111
+ data = resp.json()
112
+ text = data["content"][0]["text"]
113
+ return text
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":
121
+ return call_together_api(model, prompt, temperature, max_tokens)
122
+ elif provider == "anthropic":
123
+ return call_anthropic_api(model, prompt, temperature, max_tokens)
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):
 
166
  gr.Markdown("# Salesforce B2B Commerce Migration Assistant")
167
  gr.Markdown("This tool helps migrate CloudCraze code to B2B Lightning Experience.")
168
 
169
+ # Create a dropdown with model type labels
170
  model_dropdown = gr.Dropdown(
171
+ choices=all_models,
172
+ value=anthropic_models[0], # Default to Claude 3.7 Sonnet
173
  label="Select AI Model"
174
  )
175
 
 
226
  """
227
  - **Trigger Correction**: Fixes Apex Triggers for B2B LEx compatibility.
228
  - **Object Conversion**: Maps and converts CloudCraze object definitions to B2B LEx.
229
+ - **Model Selection**: Choose from Together AI models or Anthropic's Claude models.
230
 
231
  Always review AI-generated code before production use.
232
  """
 
235
  app.launch()
236
 
237
  if __name__ == "__main__":
238
+ main()