AppleBotzz commited on
Commit
676bff0
1 Parent(s): 2f7a0c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -28
app.py CHANGED
@@ -6,29 +6,44 @@ import json
6
  import uuid
7
  import os
8
 
 
 
9
  # List of available Claude models
10
  claude_models = ["claude-3-opus-20240229", "claude-3-sonnet-20240229", "claude-3-haiku-20240307"]
11
 
12
  # List of available OpenAI models
13
  openai_models = ["gpt-4", "gpt-4-32k", "gpt-3.5-turbo", "gpt-4-0125-preview", "gpt-4-turbo-preview", "gpt-4-1106-preview", "gpt-4-0613"]
14
 
15
- def generate_response(api_key, model, user_prompt):
16
- if api_key.startswith("sk-ant-"):
17
- # Set the Anthropic API key
18
- client = Anthropic(api_key=api_key)
19
- system_prompt_path = __file__.replace("app.py", "json.txt")
20
- elif api_key.startswith("sk-"):
21
- # Set the OpenAI API key
22
- client = OpenAI(api_key=api_key)
23
- system_prompt_path = __file__.replace("app.py", "json.txt")
 
 
 
 
 
 
24
  else:
25
- return "Invalid API key prefix. Please use 'sk-ant-' for Anthropic or 'sk-' for OpenAI.", "", None
 
 
 
 
 
 
 
26
 
27
  # Read the system prompt from a text file
28
  with open(system_prompt_path, "r") as file:
29
  system_prompt = file.read()
30
 
31
- if api_key.startswith("sk-ant-"):
32
  # Generate a response using the selected Anthropic model
33
  response = client.messages.create(
34
  system=system_prompt,
@@ -90,23 +105,33 @@ def extract_json(generated_output):
90
  print(e)
91
  return None, None
92
 
93
- def generate_second_response(api_key, model, generated_output):
94
- if api_key.startswith("sk-ant-"):
95
- # Set the Anthropic API key
96
- client = Anthropic(api_key=api_key)
97
- system_prompt_path = __file__.replace("app.py", "diffusion.txt")
98
- elif api_key.startswith("sk-"):
99
- # Set the OpenAI API key
100
- client = OpenAI(api_key=api_key)
101
- system_prompt_path = __file__.replace("app.py", "diffusion.txt")
 
 
 
102
  else:
103
- return "Invalid API key prefix. Please use 'sk-ant-' for Anthropic or 'sk-' for OpenAI."
 
 
 
 
 
 
 
104
 
105
  # Read the system prompt from a text file
106
  with open(system_prompt_path, "r") as file:
107
  system_prompt = file.read()
108
 
109
- if api_key.startswith("sk-ant-"):
110
  # Generate a second response using the selected Anthropic model and the previously generated output
111
  response = client.messages.create(
112
  system=system_prompt,
@@ -138,6 +163,7 @@ with gr.Blocks() as demo:
138
 
139
  with gr.Row():
140
  with gr.Column():
 
141
  api_key = gr.Textbox(label="API Key", type="password", placeholder="sk-ant-api03-... or sk-...")
142
  model_dropdown = gr.Dropdown(choices=[], label="Select a model")
143
  user_prompt = gr.Textbox(label="User Prompt", value="Make me a card for a panther made of translucent pastel colored goo. Its color never changes once it exists but each 'copy' has a different color. The creature comes out of a small jar, seemingly defying physics with its size. It is the size of a real panther, and as strong as one too. By default its female but is able to change gender. It can even split into multiple copies of itself if needed with no change in its own size or mass. Its outside is normally lightly squishy but solid, but on command it can become viscous like non-newtonian fluids. Be descriptive when describing this character, and make sure to describe all of its features in char_persona just like you do in description. Make sure to describe commonly used features in detail (visual, smell, taste, touch, etc).")
@@ -157,15 +183,15 @@ with gr.Blocks() as demo:
157
 
158
  def update_models(api_key):
159
  if api_key.startswith("sk-ant-"):
160
- return gr.Dropdown(choices=claude_models)
161
  elif api_key.startswith("sk-"):
162
- return gr.Dropdown(choices=openai_models)
163
  else:
164
- return gr.Dropdown(choices=[])
165
 
166
- api_key.change(update_models, inputs=api_key, outputs=model_dropdown)
167
 
168
- generate_button.click(generate_response, inputs=[api_key, model_dropdown, user_prompt], outputs=[generated_output, json_output, json_download])
169
- generate_button_2.click(generate_second_response, inputs=[api_key, model_dropdown, generated_output], outputs=generated_output_2)
170
 
171
  demo.launch()
 
6
  import uuid
7
  import os
8
 
9
+ default_urls = ["https://api.anthropic.com", "https://api.openai.com/v1"]
10
+
11
  # List of available Claude models
12
  claude_models = ["claude-3-opus-20240229", "claude-3-sonnet-20240229", "claude-3-haiku-20240307"]
13
 
14
  # List of available OpenAI models
15
  openai_models = ["gpt-4", "gpt-4-32k", "gpt-3.5-turbo", "gpt-4-0125-preview", "gpt-4-turbo-preview", "gpt-4-1106-preview", "gpt-4-0613"]
16
 
17
+ both_models = claude_models + openai_models
18
+
19
+ def generate_response(endpoint, api_key, model, user_prompt):
20
+ print(endpoint)
21
+ if endpoint in default_urls:
22
+ #check api keys as normal
23
+ if api_key.startswith("sk-ant-"):
24
+ client = Anthropic(api_key=api_key, base_url=endpoint)
25
+ system_prompt_path = __file__.replace("app.py", "json.txt")
26
+ elif api_key.startswith("sk-"):
27
+ client = OpenAI(api_key=api_key, base_url=endpoint)
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
35
+ client = Anthropic(api_key=api_key, base_url=endpoint)
36
+ system_prompt_path = __file__.replace("app.py", "json.txt")
37
+ else:
38
+ # Set the OpenAI API key
39
+ client = OpenAI(api_key=api_key, base_url=endpoint)
40
+ system_prompt_path = __file__.replace("app.py", "json.txt")
41
 
42
  # Read the system prompt from a text file
43
  with open(system_prompt_path, "r") as file:
44
  system_prompt = file.read()
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,
 
105
  print(e)
106
  return None, None
107
 
108
+ def generate_second_response(endpoint, api_key, model, generated_output):
109
+ if endpoint in default_urls:
110
+ #check api keys as normal
111
+ if api_key.startswith("sk-ant-"):
112
+ client = Anthropic(api_key=api_key, base_url=endpoint)
113
+ system_prompt_path = __file__.replace("app.py", "diffusion.txt")
114
+ elif api_key.startswith("sk-"):
115
+ client = OpenAI(api_key=api_key, base_url=endpoint)
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
123
+ client = Anthropic(api_key=api_key, base_url=endpoint)
124
+ system_prompt_path = __file__.replace("app.py", "diffusion.txt")
125
+ else:
126
+ # Set the OpenAI API key
127
+ client = OpenAI(api_key=api_key, base_url=endpoint)
128
+ system_prompt_path = __file__.replace("app.py", "diffusion.txt")
129
 
130
  # Read the system prompt from a text file
131
  with open(system_prompt_path, "r") as file:
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,
 
163
 
164
  with gr.Row():
165
  with gr.Column():
166
+ endpoint = gr.Textbox(label="Endpoint", value="https://api.anthropic.com")
167
  api_key = gr.Textbox(label="API Key", type="password", placeholder="sk-ant-api03-... or sk-...")
168
  model_dropdown = gr.Dropdown(choices=[], label="Select a model")
169
  user_prompt = gr.Textbox(label="User Prompt", value="Make me a card for a panther made of translucent pastel colored goo. Its color never changes once it exists but each 'copy' has a different color. The creature comes out of a small jar, seemingly defying physics with its size. It is the size of a real panther, and as strong as one too. By default its female but is able to change gender. It can even split into multiple copies of itself if needed with no change in its own size or mass. Its outside is normally lightly squishy but solid, but on command it can become viscous like non-newtonian fluids. Be descriptive when describing this character, and make sure to describe all of its features in char_persona just like you do in description. Make sure to describe commonly used features in detail (visual, smell, taste, touch, etc).")
 
183
 
184
  def update_models(api_key):
185
  if api_key.startswith("sk-ant-"):
186
+ return gr.Dropdown(choices=claude_models), gr.Textbox(label="Endpoint", value="https://api.anthropic.com")
187
  elif api_key.startswith("sk-"):
188
+ return gr.Dropdown(choices=openai_models), gr.Textbox(label="Endpoint", value="https://api.openai.com/v1")
189
  else:
190
+ return gr.Dropdown(choices=both_models), gr.Textbox(label="Endpoint", value="https://api.anthropic.com")
191
 
192
+ api_key.change(update_models, inputs=api_key, outputs=[model_dropdown, endpoint])
193
 
194
+ generate_button.click(generate_response, inputs=[endpoint, api_key, model_dropdown, user_prompt], outputs=[generated_output, json_output, json_download])
195
+ generate_button_2.click(generate_second_response, inputs=[endpoint, api_key, model_dropdown, generated_output], outputs=generated_output_2)
196
 
197
  demo.launch()