Update app.py
Browse files
app.py
CHANGED
@@ -62,9 +62,9 @@ def getChooseClass(msg):
|
|
62 |
else:
|
63 |
return ""
|
64 |
|
65 |
-
def chatbot(
|
66 |
#messages = messages or [{"role": "system", "content": initMsg}]
|
67 |
-
localvar = localvar or {"messages": [{"role": "system", "content": "I choose as "+ race + " "+ cls +" in D&D world.\n" + initMsg}], "race": race, "class": cls}
|
68 |
messages = localvar['messages']
|
69 |
#if len(messages) == 1:
|
70 |
|
@@ -77,13 +77,18 @@ def chatbot(input, race, cls, localvar):
|
|
77 |
#logging.info("put memory return:"+chat.choices[0].message.content)
|
78 |
|
79 |
if input:
|
80 |
-
messages
|
|
|
|
|
|
|
81 |
chat = openai.ChatCompletion.create(
|
82 |
model="gpt-3.5-turbo", messages=messages[-2:],
|
83 |
max_tokens=2048,n=1,temperature=0.5,
|
84 |
)
|
85 |
reply = chat.choices[0].message.content
|
86 |
logging.info("AI Reply:"+reply)
|
|
|
|
|
87 |
messages.append({"role": "assistant", "content": reply})
|
88 |
localvar['messages'] = messages
|
89 |
return reply, printMessages(messages), localvar
|
@@ -96,7 +101,7 @@ def printMessages(messages):
|
|
96 |
|
97 |
app = gr.Interface(fn=chatbot, inputs=[gr.Dropdown(["Orc", "Human", "Elf", "Dwarf", "Halfling", "Goliath", "Dragonborn"], label="Race", info="please choose your race:"),
|
98 |
gr.Dropdown(["Warrior", "Mage", "Priest", "Assassin", "Thief", "Paladin", "Archer", "Guardian"], label="Class", info="please choose the name of the class you wish to play as:"),
|
99 |
-
gr.Textbox(lines=7, label="You ask and answer questions below"), "state"],
|
100 |
-
outputs=[gr.Textbox(label="DND Game Reply", placeholder=
|
101 |
description="DND Game",theme="compact")
|
102 |
app.launch(share=False)
|
|
|
62 |
else:
|
63 |
return ""
|
64 |
|
65 |
+
def chatbot(race, cls, input, localvar):
|
66 |
#messages = messages or [{"role": "system", "content": initMsg}]
|
67 |
+
localvar = localvar or {"messages": [{"role": "system", "content": "I choose as "+ race + " "+ cls +" in D&D world.\n" + initMsg}], "race": race, "class": cls, "status":""}
|
68 |
messages = localvar['messages']
|
69 |
#if len(messages) == 1:
|
70 |
|
|
|
77 |
#logging.info("put memory return:"+chat.choices[0].message.content)
|
78 |
|
79 |
if input:
|
80 |
+
if len(messages) == 1:
|
81 |
+
messages.append({"role": "user", "content": input})
|
82 |
+
else:
|
83 |
+
messages.append({"role": "user", "content": defaultMsg + localvar['status'] + ". \n" + input})
|
84 |
chat = openai.ChatCompletion.create(
|
85 |
model="gpt-3.5-turbo", messages=messages[-2:],
|
86 |
max_tokens=2048,n=1,temperature=0.5,
|
87 |
)
|
88 |
reply = chat.choices[0].message.content
|
89 |
logging.info("AI Reply:"+reply)
|
90 |
+
if len(messages) == 1:
|
91 |
+
localvar['status'] = reply
|
92 |
messages.append({"role": "assistant", "content": reply})
|
93 |
localvar['messages'] = messages
|
94 |
return reply, printMessages(messages), localvar
|
|
|
101 |
|
102 |
app = gr.Interface(fn=chatbot, inputs=[gr.Dropdown(["Orc", "Human", "Elf", "Dwarf", "Halfling", "Goliath", "Dragonborn"], label="Race", info="please choose your race:"),
|
103 |
gr.Dropdown(["Warrior", "Mage", "Priest", "Assassin", "Thief", "Paladin", "Archer", "Guardian"], label="Class", info="please choose the name of the class you wish to play as:"),
|
104 |
+
gr.Textbox(lines=7, label="You ask and answer questions below", placeholder="Type word "start game" to play"), "state"],
|
105 |
+
outputs=[gr.Textbox(label="DND Game Reply", placeholder="Waiting for you start..."), gr.Textbox(label="History"), "state"], title="DND Game",#, gr.Textbox(label="History"),
|
106 |
description="DND Game",theme="compact")
|
107 |
app.launch(share=False)
|