Spaces:
Build error
Build error
AppleBotzz
commited on
Update app.py
Browse files
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 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
55 |
else:
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
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 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
143 |
else:
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
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 |
|