Spaces:
Sleeping
Sleeping
import gradio as gr | |
import os | |
import openai | |
openai.api_key = "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" | |
with gr.Blocks() as iface: | |
chatbot = [] | |
msg = [] | |
roles = ["Security Guard", "Curator", "Researcher", "Conservationist", "Guide"] | |
initText = ["You are Steve Binner, 31, a security guard for a museum of medieval " | |
"history. You're absolutely sure that this place is haunted. A month ago, " | |
"before the spooky stuff started, you were really happy with your job, " | |
"but that changed real fast. The ghost is a french nobleman named Baron " | |
"Hugo Antoine the Third. You've been trying to tell other people about " | |
"this, but you've only been laughed at so far. But you've got evidence! " | |
"Every night, around 10pm, the broom closet gets locked from the inside and " | |
"the interior gets wrecked. We're talking pushed over tables, " | |
"broken containers, the whole shebang. When it comes to the murder of " | |
"director Eisenholz last thursday, that has to have been the ghost, " | |
"no question. You even have the door card records for the room he was in: " | |
"After the museum closed at 5pm, nobody entered that room till the next " | |
"morning. Now you're being interrogated by a detective. You don't use " | |
"uptight language, and you're not super well educated on most stuff, " | |
"but when it comes to the paranormal, you're an ace. ", | |
"You are a Curator. ", | |
"You are a Researcher. ", | |
"You are a Conservationist. ", | |
"You are a Guide. "] | |
i = 0 | |
while i < len(roles): | |
with gr.Tab(roles[i]): | |
chatbot.append(gr.Chatbot()) | |
msg.append(gr.Textbox()) | |
i += 1 | |
def user(user_message, history): | |
return "", history + [[user_message, None]] | |
def bot(history, characterId): | |
bot_message = generateAIMessage(history, characterId) | |
history[-1][1] = bot_message | |
return history | |
def generateAIMessage(history, characterId): | |
message_history = [ | |
{"role": "system", "content": initText[characterId] + "Stay in character. Use natural language. Don't " | |
"reveal all of the information in a single message," | |
" and leave hints. "} | |
] | |
for pair in history: | |
message_history.append({"role": "user", "content": pair[0]}) | |
print() | |
if pair[1] is not None: | |
message_history.append({"role": "assistant", "content": pair[1]}) | |
completion = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=message_history | |
) | |
return completion.choices[0].message.content | |
i = 0 | |
while i < len(msg): | |
msg[i].submit(user, [msg[i], chatbot[i]], [msg[i], chatbot[i]], queue=False).then( | |
bot, [chatbot[i], i], chatbot[i] | |
) | |
i += 1 | |
iface.launch() | |