Spaces:
Build error
Build error
File size: 7,711 Bytes
868a05e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
import gradio as gr
from anthropic import Anthropic
from openai import OpenAI
import openai
import json
import uuid
import os
# List of available Claude models
claude_models = ["claude-3-opus-20240229", "claude-3-sonnet-20240229", "claude-3-haiku-20240307"]
# List of available OpenAI models
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"]
def generate_response(api_key, model, user_prompt):
if api_key.startswith("sk-ant-"):
# Set the Anthropic API key
client = Anthropic(api_key=api_key)
system_prompt_path = __file__.replace("app.py", "json.txt")
elif api_key.startswith("sk-"):
# Set the OpenAI API key
client = OpenAI(api_key=api_key)
system_prompt_path = __file__.replace("app.py", "json.txt")
else:
return "Invalid API key prefix. Please use 'sk-ant-' for Anthropic or 'sk-' for OpenAI.", "", None
# Read the system prompt from a text file
with open(system_prompt_path, "r") as file:
system_prompt = file.read()
if api_key.startswith("sk-ant-"):
# Generate a response using the selected Anthropic model
response = client.messages.create(
system=system_prompt,
messages=[{"role": "user", "content": user_prompt}],
model=model,
max_tokens=4096
)
response_text = response.content[0].text
else:
# Generate a response using the selected OpenAI model
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
max_tokens=4096
)
response_text = response.choices[0].message.content
json_string, json_json = extract_json(response_text)
json_file = json_string if json_string else None
create_unique_id = str(uuid.uuid4())
json_folder = __file__.replace("app.py", f"outputs/")
if not os.path.exists(json_folder):
os.makedirs(json_folder)
path = None
if json_string:
with open(f"{json_folder}{json_json['name']}_{create_unique_id}.json", "w") as file:
file.write(json_file)
path = f"{json_folder}{json_json['name']}_{create_unique_id}.json"
else:
json_string = "No JSON data was found, or the JSON data was incomplete."
return response_text, json_string or "", path
def extract_json(generated_output):
try:
generated_output = generated_output.replace("```json", "").replace("```", "").strip()
# Find the JSON string in the generated output
json_start = generated_output.find("{")
json_end = generated_output.rfind("}") + 1
json_string = generated_output[json_start:json_end]
print(json_string)
# Parse the JSON string
json_data = json.loads(json_string)
json_data['name'] = json_data['char_name']
json_data['personality'] = json_data['char_persona']
json_data['scenario'] = json_data['world_scenario']
json_data['first_mes'] = json_data['char_greeting']
# Check if all the required keys are present
required_keys = ["char_name", "char_persona", "world_scenario", "char_greeting", "example_dialogue", "description"]
if all(key in json_data for key in required_keys):
return json.dumps(json_data), json_data
else:
return None, None
except Exception as e:
print(e)
return None, None
def generate_second_response(api_key, model, generated_output):
if api_key.startswith("sk-ant-"):
# Set the Anthropic API key
client = Anthropic(api_key=api_key)
system_prompt_path = __file__.replace("app.py", "diffusion.txt")
elif api_key.startswith("sk-"):
# Set the OpenAI API key
client = OpenAI(api_key=api_key)
system_prompt_path = __file__.replace("app.py", "diffusion.txt")
else:
return "Invalid API key prefix. Please use 'sk-ant-' for Anthropic or 'sk-' for OpenAI."
# Read the system prompt from a text file
with open(system_prompt_path, "r") as file:
system_prompt = file.read()
if api_key.startswith("sk-ant-"):
# Generate a second response using the selected Anthropic model and the previously generated output
response = client.messages.create(
system=system_prompt,
messages=[{"role": "user", "content": generated_output}],
model=model,
max_tokens=4096
)
response_text = response.content[0].text
else:
# Generate a response using the selected OpenAI model
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": generated_output}
],
max_tokens=4096
)
response_text = response.choices[0].message.content
return response_text
# Set up the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# SillyTavern Character Generator")
#Text explaining that you can use the API key from the Anthropic API or the OpenAI API
gr.Markdown("You can use the API key from the Anthropic API or the OpenAI API. The API key should start with 'sk-ant-' for Anthropic or 'sk-' for OpenAI.")
with gr.Row():
with gr.Column():
api_key = gr.Textbox(label="API Key", type="password", placeholder="sk-ant-api03-... or sk-...")
model_dropdown = gr.Dropdown(choices=[], label="Select a model")
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).")
generate_button = gr.Button("Generate JSON")
with gr.Column():
generated_output = gr.Textbox(label="Generated Output")
json_output = gr.Textbox(label="JSON Output")
json_download = gr.File(label="Download JSON")
with gr.Row():
with gr.Column():
generate_button_2 = gr.Button("Generate SDXL Prompt")
with gr.Column():
generated_output_2 = gr.Textbox(label="Generated SDXL Prompt")
def update_models(api_key):
if api_key.startswith("sk-ant-"):
return gr.Dropdown(choices=claude_models)
elif api_key.startswith("sk-"):
return gr.Dropdown(choices=openai_models)
else:
return gr.Dropdown(choices=[])
api_key.change(update_models, inputs=api_key, outputs=model_dropdown)
generate_button.click(generate_response, inputs=[api_key, model_dropdown, user_prompt], outputs=[generated_output, json_output, json_download])
generate_button_2.click(generate_second_response, inputs=[api_key, model_dropdown, generated_output], outputs=generated_output_2)
demo.launch() |