AppleBotzz commited on
Commit
0cbdbf7
·
verified ·
1 Parent(s): 1ff8960

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -37
app.py CHANGED
@@ -28,7 +28,7 @@ def generate_response(endpoint, api_key, model, user_prompt):
28
  system_prompt_path = __file__.replace("app.py", "json.txt")
29
  else:
30
  print("Invalid API key")
31
- return "Invalid API key"
32
  else:
33
  if model in claude_models:
34
  # Set the Anthropic API key
@@ -45,24 +45,32 @@ def generate_response(endpoint, api_key, model, user_prompt):
45
 
46
  if model in claude_models:
47
  # Generate a response using the selected Anthropic model
48
- response = client.messages.create(
49
- system=system_prompt,
50
- messages=[{"role": "user", "content": user_prompt}],
51
- model=model,
52
- max_tokens=4096
53
- )
54
- response_text = response.content[0].text
 
 
 
 
55
  else:
56
- # Generate a response using the selected OpenAI model
57
- response = client.chat.completions.create(
58
- model=model,
59
- messages=[
60
- {"role": "system", "content": system_prompt},
61
- {"role": "user", "content": user_prompt}
62
- ],
63
- max_tokens=4096
64
- )
65
- response_text = response.choices[0].message.content
 
 
 
 
66
 
67
  json_string, json_json = extract_json(response_text)
68
  json_file = json_string if json_string else None
@@ -116,7 +124,7 @@ def generate_second_response(endpoint, api_key, model, generated_output):
116
  system_prompt_path = __file__.replace("app.py", "diffusion.txt")
117
  else:
118
  print("Invalid API key")
119
- return "Invalid API key"
120
  else:
121
  if model in claude_models:
122
  # Set the Anthropic API key
@@ -132,25 +140,33 @@ def generate_second_response(endpoint, api_key, model, generated_output):
132
  system_prompt = file.read()
133
 
134
  if model in claude_models:
135
- # Generate a second response using the selected Anthropic model and the previously generated output
136
- response = client.messages.create(
137
- system=system_prompt,
138
- messages=[{"role": "user", "content": generated_output}],
139
- model=model,
140
- max_tokens=4096
141
- )
142
- response_text = response.content[0].text
 
 
 
 
143
  else:
144
- # Generate a response using the selected OpenAI model
145
- response = client.chat.completions.create(
146
- model=model,
147
- messages=[
148
- {"role": "system", "content": system_prompt},
149
- {"role": "user", "content": generated_output}
150
- ],
151
- max_tokens=4096
152
- )
153
- response_text = response.choices[0].message.content
 
 
 
 
154
 
155
  return response_text
156
 
 
28
  system_prompt_path = __file__.replace("app.py", "json.txt")
29
  else:
30
  print("Invalid API key")
31
+ return "Invalid API key", "Invalid API key", None
32
  else:
33
  if model in claude_models:
34
  # Set the Anthropic API key
 
45
 
46
  if model in claude_models:
47
  # Generate a response using the selected Anthropic model
48
+ try:
49
+ response = client.messages.create(
50
+ system=system_prompt,
51
+ messages=[{"role": "user", "content": user_prompt}],
52
+ model=model,
53
+ max_tokens=4096
54
+ )
55
+ response_text = response.content[0].text
56
+ except Exception as e:
57
+ print(e)
58
+ response_text = "An error occurred while generating the response. Check that your API key is correct!"
59
  else:
60
+ try:
61
+ # Generate a response using the selected OpenAI model
62
+ response = client.chat.completions.create(
63
+ model=model,
64
+ messages=[
65
+ {"role": "system", "content": system_prompt},
66
+ {"role": "user", "content": user_prompt}
67
+ ],
68
+ max_tokens=4096
69
+ )
70
+ response_text = response.choices[0].message.content
71
+ except Exception as e:
72
+ print(e)
73
+ response_text = "An error occurred while generating the response. Check that your API key is correct!"
74
 
75
  json_string, json_json = extract_json(response_text)
76
  json_file = json_string if json_string else None
 
124
  system_prompt_path = __file__.replace("app.py", "diffusion.txt")
125
  else:
126
  print("Invalid API key")
127
+ return "Invalid API key", "Invalid API key", None
128
  else:
129
  if model in claude_models:
130
  # Set the Anthropic API key
 
140
  system_prompt = file.read()
141
 
142
  if model in claude_models:
143
+ try:
144
+ # Generate a second response using the selected Anthropic model and the previously generated output
145
+ response = client.messages.create(
146
+ system=system_prompt,
147
+ messages=[{"role": "user", "content": generated_output}],
148
+ model=model,
149
+ max_tokens=4096
150
+ )
151
+ response_text = response.content[0].text
152
+ except Exception as e:
153
+ print(e)
154
+ response_text = "An error occurred while generating the response. Check that your API key is correct!"
155
  else:
156
+ try:
157
+ # Generate a response using the selected OpenAI model
158
+ response = client.chat.completions.create(
159
+ model=model,
160
+ messages=[
161
+ {"role": "system", "content": system_prompt},
162
+ {"role": "user", "content": generated_output}
163
+ ],
164
+ max_tokens=4096
165
+ )
166
+ response_text = response.choices[0].message.content
167
+ except Exception as e:
168
+ print(e)
169
+ response_text = "An error occurred while generating the response. Check that your API key is correct!"
170
 
171
  return response_text
172