Update app.py
Browse files
app.py
CHANGED
@@ -5,49 +5,63 @@ from langchain.chat_models import ChatOpenAI
|
|
5 |
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
6 |
# from dotenv import load_dotenv
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# load_dotenv("E:/Development/Python/scripts/keys.env")
|
9 |
-
if "OPENAI_API_KEY" not in os.environ or os.environ["OPENAI_API_KEY"] == "":
|
10 |
-
def init_api_key(key):
|
11 |
-
os.environ["OPENAI_API_KEY"] = key
|
12 |
-
gr.Interface( fn=init_api_key, inputs=["text"], outputs = None).launch()
|
13 |
-
openai_key = os.getenv("OPENAI_API_KEY")
|
14 |
-
|
15 |
-
chat1 = ChatOpenAI(temperature=0.9, openai_api_key=openai_key)
|
16 |
-
chat2 = ChatOpenAI(temperature=0.9, openai_api_key=openai_key)
|
17 |
-
|
18 |
-
def format_chat(chat_history):
|
19 |
-
chat_html = "<div style='padding: 10px; border: 1px solid #ccc;'>"
|
20 |
-
for message in chat_history:
|
21 |
-
chat_html += "<p style='color:{}; '><strong>{}</strong>: {}</p>".format(message["color"], message["sender"], message["content"])
|
22 |
-
chat_html += "</div>"
|
23 |
-
return chat_html
|
24 |
-
|
25 |
-
def color_chg(name):
|
26 |
-
background_color = (11, 15, 25)
|
27 |
-
background_brightness = sum(background_color) / len(background_color)
|
28 |
-
|
29 |
-
value = sum(ord(char) for char in name)
|
30 |
-
red = (value % 128) + 128
|
31 |
-
green = (value // 128 % 128) + 128
|
32 |
-
blue = (value // 16384 % 128) + 128
|
33 |
-
|
34 |
-
# Calculate the contrast ratio between the background and the generated color
|
35 |
-
brightness = (red + green + blue) / 3
|
36 |
-
contrast_ratio = max(brightness, background_brightness) / min(brightness, background_brightness)
|
37 |
|
38 |
-
# Adjust the brightness of the generated color if the contrast ratio is too low
|
39 |
-
if contrast_ratio < 4.5:
|
40 |
-
# Increase the brightness by finding the ratio needed to achieve the desired contrast ratio
|
41 |
-
brightness_ratio = 4.5 / contrast_ratio
|
42 |
-
red = min(int(red * brightness_ratio), 255)
|
43 |
-
green = min(int(green * brightness_ratio), 255)
|
44 |
-
blue = min(int(blue * brightness_ratio), 255)
|
45 |
|
46 |
-
color_hex = "#{:02x}{:02x}{:02x}".format(red, green, blue)
|
47 |
-
return color_hex
|
48 |
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
Let's play a little Roleplaying Game: You are Isaac Newton. You will talk like you are Isaac Newton, and you will adjust your knowledge to that of Isaac NewtonYou will roleplay Isaac Newton and behave like Isaac Newton.
|
52 |
You will never admit that you are just an AI and always stay in Character.
|
53 |
If you understood, reply ACK.""".replace( "Isaac Newton", name1) ),
|
@@ -55,12 +69,12 @@ def initialize(name1, name2, iterations):
|
|
55 |
HumanMessage(content="Greetings, traveler")]
|
56 |
|
57 |
|
58 |
-
|
59 |
I want you to act like Mozart. I want you to respond and answer like him using the tone, manner, opinions, philosophy, and vocabulary that he would use. Do not write any explanations. You must know all of the knowledge of Mozart.
|
60 |
If you understood, reply ACK.""".replace( "Mozart", name2) ),
|
61 |
AIMessage(content = "ACK")]
|
62 |
|
63 |
-
|
64 |
|
65 |
# MessageStack1.append(AIMessage(chat1( MessageStack1).content))
|
66 |
|
@@ -74,19 +88,21 @@ def initialize(name1, name2, iterations):
|
|
74 |
|
75 |
# MessageStack1.append( HumanMessage( Message2 ))
|
76 |
# MessageStack2.append( AIMessage( Message2 ))
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
90 |
|
91 |
|
92 |
# MessageStack1.append(HumanMessage(content = response2.content))
|
@@ -94,8 +110,12 @@ def initialize(name1, name2, iterations):
|
|
94 |
# for i in MsgStack:
|
95 |
# conversation += i + "\n"
|
96 |
# print(conversation)
|
97 |
-
|
98 |
-
demo = gr.Interface( fn=initialize, inputs=["text", "text", gr.Number(minimum=1, maximum=50, step=1)], outputs =gr.outputs.HTML(label="Chat"))
|
|
|
|
|
|
|
99 |
|
100 |
-
|
101 |
|
|
|
|
5 |
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
6 |
# from dotenv import load_dotenv
|
7 |
|
8 |
+
if "OPENAI_API_KEY" in os.environ and os.environ["OPENAI_API_KEY"] != "":
|
9 |
+
openai_key = os.getenv("OPENAI_API_KEY")
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
# load_dotenv("E:/Development/Python/scripts/keys.env")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
|
|
17 |
|
18 |
+
with gr.Blocks() as demo:
|
19 |
+
if "openai_key" not in locals():
|
20 |
+
openai_key = gr.Text(label="OpenAI Key", placeholder="Paste your OpenAI API Key here")
|
21 |
+
else:
|
22 |
+
openai_key = gr.Text(label="OpenAI Key", placeholder=openai_key)
|
23 |
+
openai_key.visible = 0
|
24 |
+
with gr.Row():
|
25 |
+
name1 = gr.Text(label="Character 1", name="name1")
|
26 |
+
name2 = gr.Text(label="Character 2", name="name2")
|
27 |
+
iterations = gr.Number( label="Iterations", name="iterations")
|
28 |
+
btn_run = gr.Button("Run")
|
29 |
+
output = gr.outputs.HTML( )
|
30 |
+
def format_chat(chat_history):
|
31 |
+
chat_html = "<div style='padding: 10px; border: 1px solid #ccc;'>"
|
32 |
+
for message in chat_history:
|
33 |
+
chat_html += "<p style='color:{}; '><strong>{}</strong>: {}</p>".format(message["color"], message["sender"], message["content"])
|
34 |
+
chat_html += "</div>"
|
35 |
+
return chat_html
|
36 |
+
|
37 |
+
def color_chg(name):
|
38 |
+
background_color = (11, 15, 25)
|
39 |
+
background_brightness = sum(background_color) / len(background_color)
|
40 |
+
|
41 |
+
value = sum(ord(char) for char in name)
|
42 |
+
red = (value % 128) + 128
|
43 |
+
green = (value // 128 % 128) + 128
|
44 |
+
blue = (value // 16384 % 128) + 128
|
45 |
+
|
46 |
+
# Calculate the contrast ratio between the background and the generated color
|
47 |
+
brightness = (red + green + blue) / 3
|
48 |
+
contrast_ratio = max(brightness, background_brightness) / min(brightness, background_brightness)
|
49 |
+
|
50 |
+
# Adjust the brightness of the generated color if the contrast ratio is too low
|
51 |
+
if contrast_ratio < 4.5:
|
52 |
+
# Increase the brightness by finding the ratio needed to achieve the desired contrast ratio
|
53 |
+
brightness_ratio = 4.5 / contrast_ratio
|
54 |
+
red = min(int(red * brightness_ratio), 255)
|
55 |
+
green = min(int(green * brightness_ratio), 255)
|
56 |
+
blue = min(int(blue * brightness_ratio), 255)
|
57 |
+
|
58 |
+
color_hex = "#{:02x}{:02x}{:02x}".format(red, green, blue)
|
59 |
+
return color_hex
|
60 |
+
|
61 |
+
def initialize(name1, name2, iterations, openai_key):
|
62 |
+
chat1 = ChatOpenAI(temperature=0.9, openai_api_key=openai_key)
|
63 |
+
chat2 = ChatOpenAI(temperature=0.9, openai_api_key=openai_key)
|
64 |
+
MessageStack1 = [SystemMessage(content="""
|
65 |
Let's play a little Roleplaying Game: You are Isaac Newton. You will talk like you are Isaac Newton, and you will adjust your knowledge to that of Isaac NewtonYou will roleplay Isaac Newton and behave like Isaac Newton.
|
66 |
You will never admit that you are just an AI and always stay in Character.
|
67 |
If you understood, reply ACK.""".replace( "Isaac Newton", name1) ),
|
|
|
69 |
HumanMessage(content="Greetings, traveler")]
|
70 |
|
71 |
|
72 |
+
MessageStack2 = [SystemMessage(content="""
|
73 |
I want you to act like Mozart. I want you to respond and answer like him using the tone, manner, opinions, philosophy, and vocabulary that he would use. Do not write any explanations. You must know all of the knowledge of Mozart.
|
74 |
If you understood, reply ACK.""".replace( "Mozart", name2) ),
|
75 |
AIMessage(content = "ACK")]
|
76 |
|
77 |
+
MsgStack = [ name2+": Greetings, Traveler"]
|
78 |
|
79 |
# MessageStack1.append(AIMessage(chat1( MessageStack1).content))
|
80 |
|
|
|
88 |
|
89 |
# MessageStack1.append( HumanMessage( Message2 ))
|
90 |
# MessageStack2.append( AIMessage( Message2 ))
|
91 |
+
chat_history = []
|
92 |
+
print( iterations)
|
93 |
+
|
94 |
+
for i in range(int(iterations)):
|
95 |
+
response1 = chat1(MessageStack1)
|
96 |
+
print(name1+": " + response1.content)
|
97 |
+
MsgStack.append( name1+": "+response1.content)
|
98 |
+
MessageStack1.append(AIMessage(content = response1.content))
|
99 |
+
MessageStack2.append(HumanMessage(content = response1.content))
|
100 |
+
|
101 |
+
response2 = chat2(MessageStack2)
|
102 |
+
print(name2+": "+response2.content)
|
103 |
+
MsgStack.append( name2+": "+response2.content)
|
104 |
+
chat_history.append({"sender": name1, "content": response1.content, "color" : color_chg(name1) } )
|
105 |
+
chat_history.append({"sender": name2, "content": response2.content, "color" : color_chg(name2) } )
|
106 |
|
107 |
|
108 |
# MessageStack1.append(HumanMessage(content = response2.content))
|
|
|
110 |
# for i in MsgStack:
|
111 |
# conversation += i + "\n"
|
112 |
# print(conversation)
|
113 |
+
return format_chat(chat_history)
|
114 |
+
# demo = gr.Interface( fn=initialize, inputs=["text", "text", gr.Number(minimum=1, maximum=50, step=1)], outputs =gr.outputs.HTML(label="Chat"))
|
115 |
+
|
116 |
+
# demo.launch(server_port= 1113)
|
117 |
+
|
118 |
|
119 |
+
btn_run.click(fn=initialize, inputs=[name1, name2, iterations, openai_key], outputs = output)
|
120 |
|
121 |
+
demo.launch( )
|