Spaces:
Runtime error
Runtime error
init
Browse files- .gitignore +1 -0
- app.py +2 -59
- temp_prompts.txt +22 -0
- test.py +132 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
venv/
|
app.py
CHANGED
@@ -1,10 +1,5 @@
|
|
1 |
from ast import Interactive
|
2 |
import gradio as gr
|
3 |
-
from langchain.chains import LLMChain
|
4 |
-
from langchain.chains import SimpleSequentialChain
|
5 |
-
from langchain.llms import OpenAI
|
6 |
-
from langchain.prompts import PromptTemplate
|
7 |
-
from langchain.chains import SequentialChain
|
8 |
import openai
|
9 |
import os
|
10 |
import base64
|
@@ -14,62 +9,10 @@ import re
|
|
14 |
import codecs
|
15 |
import random
|
16 |
|
17 |
-
|
18 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
19 |
-
#openai.api_key = ""
|
20 |
def getAndParseQuickStart(text, count):
|
21 |
-
print("Asking AI for a character of " + text + " with trait count:")
|
22 |
-
print(count)
|
23 |
-
instruction_prompt = f"""
|
24 |
-
You are an AI bot that creates profiles of characters based on a simple input. You generate and give detailed characters in the following format:
|
25 |
-
Name: descriptors
|
26 |
-
Mind: descriptors (comma separated properties)
|
27 |
-
Personality: descriptors (comma separated properties, at least {count} traits)
|
28 |
-
Body: descriptors (comma separated properties, at least {count} traits)
|
29 |
-
Likes: descriptors (comma separated properties, at least {count} traits)
|
30 |
-
Hates: descriptors (comma separated properties, at least {count} traits)
|
31 |
-
Attributes: descriptors (comma separated properties, at least {count} traits)
|
32 |
-
Clothes: descriptors (comma separated properties)
|
33 |
-
Sex: descriptor (choose only from: Male, Female, or Other)
|
34 |
-
Sexuality: descriptor (choose only from: Gay, Straight, Bi, or Asexual. Default to Asexual)
|
35 |
-
Age: descriptor (as an integer, no additional text)
|
36 |
-
Description: descriptor (3 sentences, do not repeat any previous information)
|
37 |
-
Personality_Summary: descriptor
|
38 |
-
|
39 |
-
"""
|
40 |
-
prompt = """
|
41 |
-
Please generate a character based on the following description assuming this is a real person,
|
42 |
-
make sure any dialog is accurate as to how this specific character sounds. The dialog should give a good indication
|
43 |
-
of the manner in which the character speaks:\n
|
44 |
-
""" + text
|
45 |
-
|
46 |
-
response = openai.ChatCompletion.create(
|
47 |
-
model="gpt-3.5-turbo-0613",
|
48 |
-
messages=[
|
49 |
-
{"role": "system", "content": instruction_prompt},
|
50 |
-
{"role": "user", "content": prompt}
|
51 |
-
])
|
52 |
|
53 |
-
|
54 |
-
# Parse the AI's response
|
55 |
-
response_content = response['choices'][0]['message']['content']
|
56 |
-
print(response_content)
|
57 |
-
example_chat_index = response_content.find('Example_Chat:')
|
58 |
-
if example_chat_index != -1:
|
59 |
-
# Extract 'Example Chat' and everything after it
|
60 |
-
example_chat_content = response_content[example_chat_index:]
|
61 |
-
# Split the content into lines
|
62 |
-
example_chat_lines = example_chat_content.split('\n')
|
63 |
-
# The first line is 'Example Chat: <first line of chat>'
|
64 |
-
# So we need to remove 'Example Chat: ' from it
|
65 |
-
example_chat_lines[0] = example_chat_lines[0][len('Example_Chat: '):]
|
66 |
-
# Join the lines back together to get the full 'Example Chat'
|
67 |
-
example_chat = '\n'.join(example_chat_lines)
|
68 |
-
traits_dict = {'Example_Chat': example_chat.strip()}
|
69 |
-
# Remove the 'Example Chat' part from the response content so it won't be processed again in the loop
|
70 |
-
api_response = response_content[:example_chat_index]
|
71 |
-
else:
|
72 |
-
traits_dict = {}
|
73 |
|
74 |
traits = response_content.split('\n')
|
75 |
print(traits)
|
|
|
1 |
from ast import Interactive
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
3 |
import openai
|
4 |
import os
|
5 |
import base64
|
|
|
9 |
import codecs
|
10 |
import random
|
11 |
|
12 |
+
|
|
|
|
|
13 |
def getAndParseQuickStart(text, count):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
traits_dict = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
traits = response_content.split('\n')
|
18 |
print(traits)
|
temp_prompts.txt
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Describe the mind of Ender Wiggins in 3 sentences
|
2 |
+
|
3 |
+
### Response:
|
4 |
+
Ender Wiggin is a highly intelligent and strategic thinker, capable of quickly analyzing complex situations and adapting to new challenges. He possesses an innate sense of empathy that allows him to understand the perspectives of others, even those he may perceive as enemies. Ender's mind is constantly working to find solutions and overcome obstacles, driven by a deep desire for justice and peace in the world around him.
|
5 |
+
|
6 |
+
Abraham Lincoln's mind would be: "Analytical, Compassionate, Resilient, Introspective, Humble"
|
7 |
+
|
8 |
+
Ender Wiggin's mind would be: "Strategic, Empathetic, Resolute, Adaptable, Curious"
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
Abraham Lincoln likes: "Reading, Writing, Storytelling, Chess, Philanthropy"
|
15 |
+
Ender Wiggin's mind would be: "Intelligent, Creative, Resourceful, Empathetic, Strategic"
|
16 |
+
Ender Wiggin likes: "Gaming, Leadership, Exploration, Problem-solving, Learning new things"
|
17 |
+
|
18 |
+
JSON: {"Abe Lincoln (mind, likes)":
|
19 |
+
{"mind": "Analytical, Compassionate, Resilient, Introspective, Humble",
|
20 |
+
"likes": "Reading, Writing, Storytelling, Chess, Philanthropy"}}
|
21 |
+
{"Ender Wiggins (mind, likes, hates, body, age)":
|
22 |
+
{"mind":
|
test.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
|
4 |
+
# For local streaming, the websockets are hosted without ssl - http://
|
5 |
+
HOST = 'localhost:5000'
|
6 |
+
URI = f'http://{HOST}/api/v1/generate'
|
7 |
+
|
8 |
+
# For reverse-proxied streaming, the remote will likely host with ssl - https://
|
9 |
+
# URI = 'https://your-uri-here.trycloudflare.com/api/v1/generate'
|
10 |
+
|
11 |
+
|
12 |
+
def run(prompt, toks, seed=-1):
|
13 |
+
request = {
|
14 |
+
'prompt': prompt,
|
15 |
+
'max_new_tokens': toks,
|
16 |
+
'auto_max_new_tokens': False,
|
17 |
+
|
18 |
+
# Generation params. If 'preset' is set to different than 'None', the values
|
19 |
+
# in presets/preset-name.yaml are used instead of the individual numbers.
|
20 |
+
'preset': 'None',
|
21 |
+
'do_sample': True,
|
22 |
+
'temperature': 0.7,
|
23 |
+
'top_p': 0.1,
|
24 |
+
'typical_p': 1,
|
25 |
+
'epsilon_cutoff': 0, # In units of 1e-4
|
26 |
+
'eta_cutoff': 0, # In units of 1e-4
|
27 |
+
'tfs': 1,
|
28 |
+
'top_a': 0,
|
29 |
+
'repetition_penalty': 1.18,
|
30 |
+
'repetition_penalty_range': 0,
|
31 |
+
'top_k': 40,
|
32 |
+
'min_length': 0,
|
33 |
+
'no_repeat_ngram_size': 0,
|
34 |
+
'num_beams': 1,
|
35 |
+
'penalty_alpha': 0,
|
36 |
+
'length_penalty': 1,
|
37 |
+
'early_stopping': False,
|
38 |
+
'mirostat_mode': 0,
|
39 |
+
'mirostat_tau': 5,
|
40 |
+
'mirostat_eta': 0.1,
|
41 |
+
|
42 |
+
'seed': seed,
|
43 |
+
'add_bos_token': True,
|
44 |
+
'truncation_length': 2048,
|
45 |
+
'ban_eos_token': False,
|
46 |
+
'skip_special_tokens': True,
|
47 |
+
'stopping_strings': []
|
48 |
+
}
|
49 |
+
|
50 |
+
response = requests.post(URI, json=request)
|
51 |
+
|
52 |
+
if response.status_code == 200:
|
53 |
+
result = response.json()['results'][0]['text']
|
54 |
+
# return result
|
55 |
+
print(result)
|
56 |
+
return result
|
57 |
+
else:
|
58 |
+
print('fail')
|
59 |
+
|
60 |
+
def getTraits(character):
|
61 |
+
prompt = f"""JSON: {{"Abe Lincoln (mind, likes, body)":
|
62 |
+
{{"mind": "Analytical, Compassionate, Resilient, Introspective, Humble",
|
63 |
+
"likes": "Reading, Writing, Storytelling, Chess, Philanthropy",
|
64 |
+
"body": "lean, tall, brown hair, muscular, long face"}}}},
|
65 |
+
{{"{character} (mind, likes, body, hates, personality)": """
|
66 |
+
print("Getting traits.")
|
67 |
+
print(prompt)
|
68 |
+
return run(prompt, 250)
|
69 |
+
|
70 |
+
def getSex(character):
|
71 |
+
prompt = f"Male, Female, or Other:\nAbe Lincoln is a Male\nJessica Rabbit is a Female\n{character} is a"
|
72 |
+
print("Getting sex.")
|
73 |
+
return run(prompt, 2)
|
74 |
+
|
75 |
+
def getSexuality(character):
|
76 |
+
prompt = f"Here is a list of whether someone is a Heterosexual, Homosexual, or Bisexual:\nAbe Lincoln is a \"Heterosexual\"\nElton John is a \"Homosexual\"\nJames Hille is a \"Heterosexual\"\nJoey Ball is a \"Bisexual\"\n{character} is a "
|
77 |
+
print("Getting sexuality.")
|
78 |
+
return run(prompt, 6)
|
79 |
+
|
80 |
+
def getAge(character):
|
81 |
+
prompt = f"Ages:\nAbraham Lincoln: age is \"54\"\nJames Hille is: age is \"28\"\nJohnny McBoom is: age is \"8\"{character}: age is "
|
82 |
+
print("Getting age.")
|
83 |
+
return run(prompt, 6)
|
84 |
+
|
85 |
+
def getGeneralDescription(character, json):
|
86 |
+
prompt = f"""
|
87 |
+
Based on these properties:
|
88 |
+
{json}\n
|
89 |
+
Here is a 3 sentence description of {character}:
|
90 |
+
"""
|
91 |
+
print("Getting description")
|
92 |
+
return run(prompt, 200)
|
93 |
+
|
94 |
+
def getName(character):
|
95 |
+
prompt = f"A childrens book character of brilliant scientist's name is: Professor McBrains\nAbe Licoln's name is: Abraham Licoln\n{character}'s name is:"
|
96 |
+
print("Getting name")
|
97 |
+
return run(prompt, 12)
|
98 |
+
|
99 |
+
def remove_extra_braces(data_str):
|
100 |
+
parts = data_str.split('}')
|
101 |
+
if len(parts) > 2: # Considering the split creates an extra empty part at the end
|
102 |
+
data_str = "}".join(parts[:2]) + '}'
|
103 |
+
return data_str
|
104 |
+
|
105 |
+
def remove_after_first_brace(data_str):
|
106 |
+
return data_str.split('}', 1)[0] + '}'
|
107 |
+
|
108 |
+
def remove_non_letters(s):
|
109 |
+
return ''.join([char for char in s if char.isalpha()])
|
110 |
+
|
111 |
+
def remove_non_numbers(s):
|
112 |
+
return ''.join([char for char in s if char.isdigit()])
|
113 |
+
|
114 |
+
if __name__ == '__main__':
|
115 |
+
character = "Chris Chambers from Stand By Me"
|
116 |
+
traits_dict = {}
|
117 |
+
cleaned_data = remove_after_first_brace(getTraits(character))
|
118 |
+
print("#####")
|
119 |
+
print(cleaned_data)
|
120 |
+
traits_dict = json.loads(cleaned_data)
|
121 |
+
for key, value in traits_dict.items():
|
122 |
+
if isinstance(value, str):
|
123 |
+
traits_dict[key] = value.lower()
|
124 |
+
|
125 |
+
# To check the updated dictionary:
|
126 |
+
traits_dict['name'] = getName(character)
|
127 |
+
traits_dict['sex'] = remove_non_letters(getSex(character))
|
128 |
+
traits_dict['sexuality'] = remove_non_letters(getSexuality(character))
|
129 |
+
traits_dict['age'] = remove_non_numbers(getAge(character))
|
130 |
+
traits_dict['description'] = (getGeneralDescription(character, traits_dict))
|
131 |
+
for key, value in traits_dict.items():
|
132 |
+
print(f"{key}: {value}")
|