Spaces:
Sleeping
Sleeping
Jonas Wiesli
commited on
Commit
·
343724c
1
Parent(s):
2d4d338
add chat functionality back in
Browse files
app.py
CHANGED
@@ -4,53 +4,55 @@ import openai
|
|
4 |
|
5 |
openai.api_key = "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"
|
6 |
|
7 |
-
|
8 |
with gr.Blocks() as iface:
|
9 |
chatbot = gr.Chatbot()
|
10 |
msg = gr.Textbox()
|
11 |
|
|
|
12 |
def user(user_message, history):
|
13 |
return "", history + [[user_message, None]]
|
14 |
|
|
|
15 |
def bot(history):
|
16 |
-
|
17 |
-
for pair in history:
|
18 |
-
print(pair[0])
|
19 |
-
if pair[1] is not None:
|
20 |
-
print(pair[1])
|
21 |
-
bot_message = "test"
|
22 |
history[-1][1] = bot_message
|
23 |
return history
|
24 |
|
|
|
25 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
26 |
bot, chatbot, chatbot
|
27 |
)
|
28 |
|
29 |
iface.launch()
|
30 |
|
|
|
31 |
def generateAIMessage(history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
completion = openai.ChatCompletion.create(
|
33 |
model="gpt-3.5-turbo",
|
34 |
-
messages=
|
35 |
-
{"role": "system", "content": "You are Steve Binner, 31, a security guard for a museum of medieval "
|
36 |
-
"history. You're absolutely sure that this place is haunted. Before the "
|
37 |
-
"spooky stuff started, you were really happy with your job, "
|
38 |
-
"but that changed real fast. The ghost is a french nobleman named Hugo "
|
39 |
-
"Antoine the Third. You've been trying to tell other people about this, "
|
40 |
-
"but you've only been laughed at so far. But you've got evidence! Every "
|
41 |
-
"night, around 10pm, the broom closet gets locked from the inside and the "
|
42 |
-
"interior gets wrecked. We're talking pushed over tables, "
|
43 |
-
"broken containers, the whole shebang. When it comes to the murder of "
|
44 |
-
"director Eisenholz last thursday, that has to have been the ghost, "
|
45 |
-
"no question. You even have the door card records for the room he was in: "
|
46 |
-
"After the museum closed at 5pm, nobody entered that room till the next "
|
47 |
-
"morning. Now you're being interrogated by a detective. You don't use "
|
48 |
-
"uptight language, and you're not super well educated on most stuff, "
|
49 |
-
"but when it comes to the paranormal, you're an ace. Stay in character. Use "
|
50 |
-
"natural language. Don't reveal all of the information in a single message, "
|
51 |
-
"and leave hints. "},
|
52 |
-
{"role": "user", "content": firstmessage}
|
53 |
-
]
|
54 |
)
|
55 |
return completion.choices[0].message.content
|
56 |
-
|
|
|
4 |
|
5 |
openai.api_key = "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"
|
6 |
|
|
|
7 |
with gr.Blocks() as iface:
|
8 |
chatbot = gr.Chatbot()
|
9 |
msg = gr.Textbox()
|
10 |
|
11 |
+
|
12 |
def user(user_message, history):
|
13 |
return "", history + [[user_message, None]]
|
14 |
|
15 |
+
|
16 |
def bot(history):
|
17 |
+
bot_message = generateAIMessage(history)
|
|
|
|
|
|
|
|
|
|
|
18 |
history[-1][1] = bot_message
|
19 |
return history
|
20 |
|
21 |
+
|
22 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
23 |
bot, chatbot, chatbot
|
24 |
)
|
25 |
|
26 |
iface.launch()
|
27 |
|
28 |
+
|
29 |
def generateAIMessage(history):
|
30 |
+
message_history = [
|
31 |
+
{"role": "system", "content": "You are Steve Binner, 31, a security guard for a museum of medieval "
|
32 |
+
"history. You're absolutely sure that this place is haunted. Before the "
|
33 |
+
"spooky stuff started, you were really happy with your job, "
|
34 |
+
"but that changed real fast. The ghost is a french nobleman named Hugo "
|
35 |
+
"Antoine the Third. You've been trying to tell other people about this, "
|
36 |
+
"but you've only been laughed at so far. But you've got evidence! Every "
|
37 |
+
"night, around 10pm, the broom closet gets locked from the inside and the "
|
38 |
+
"interior gets wrecked. We're talking pushed over tables, "
|
39 |
+
"broken containers, the whole shebang. When it comes to the murder of "
|
40 |
+
"director Eisenholz last thursday, that has to have been the ghost, "
|
41 |
+
"no question. You even have the door card records for the room he was in: "
|
42 |
+
"After the museum closed at 5pm, nobody entered that room till the next "
|
43 |
+
"morning. Now you're being interrogated by a detective. You don't use "
|
44 |
+
"uptight language, and you're not super well educated on most stuff, "
|
45 |
+
"but when it comes to the paranormal, you're an ace. Stay in character. Use "
|
46 |
+
"natural language. Don't reveal all of the information in a single message, "
|
47 |
+
"and leave hints. "}
|
48 |
+
]
|
49 |
+
for pair in history:
|
50 |
+
message_history.append({"role": "user", "content": pair[0]})
|
51 |
+
print()
|
52 |
+
if pair[1] is not None:
|
53 |
+
message_history.append({"assistant": "user", "content": pair[1]})
|
54 |
completion = openai.ChatCompletion.create(
|
55 |
model="gpt-3.5-turbo",
|
56 |
+
messages=message_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
)
|
58 |
return completion.choices[0].message.content
|
|