harrygens commited on
Commit
2dc806b
1 Parent(s): 4d2023a

Create app_bak.py

Browse files
Files changed (1) hide show
  1. app_bak.py +109 -0
app_bak.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import gradio as gr
3
+ import os
4
+ import logging
5
+ import json
6
+
7
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
8
+
9
+ openai.api_key = os.environ['key']
10
+ initMsg = os.environ['init']
11
+ roleAns = os.environ['roleAns']
12
+ classAsk = os.environ['classAsk']
13
+ classAns = os.environ['classAns']
14
+ defaultMsg = os.environ['defaultMsg']
15
+
16
+ mod = 3;
17
+
18
+ def localRace(race):
19
+ race = race
20
+ return race
21
+
22
+ def localClass(cls):
23
+ cls = cls
24
+ return cls
25
+
26
+ def trimMessages(messages):
27
+ # Assuming messages is an array of objects with "role" and "content" properties
28
+ content = {}
29
+ for message in messages:
30
+ if message["role"] == "assistant":
31
+ if "Current Status:" in message["content"]:
32
+ content = message["content"]
33
+ start_index = message["content"].index("Current Status:") + len("Current Status:")
34
+ end_index = message["content"].index("Wielding:")
35
+ new_content = message["content"][:start_index] + message["content"][end_index:]
36
+ message["content"] = new_content
37
+ if content != {}:
38
+ messages.append({"role": "assistant", "content": content})
39
+ return messages
40
+
41
+ def getChooseRole(msg):
42
+ start_index = msg.index("As a") + len("As a")
43
+ if start_index >= 0:
44
+ end_index = msg.index(",")
45
+ str = msg[start_index:] + msg[:end_index]
46
+ str = str.strip()
47
+ if str.lower() == "dm":
48
+ return ""
49
+ else:
50
+ return str
51
+ else:
52
+ return ""
53
+
54
+ def getChooseClass(msg):
55
+ full = getChooseRole(msg)
56
+ if full != "":
57
+ start_index = full.index(" ") + len(" ")
58
+ end_index = msg.index(",")
59
+ str = full[start_index:] + full[:end_index]
60
+ str = str.strip()
61
+ return str
62
+ else:
63
+ return ""
64
+
65
+ def chatbot(input, localvar):
66
+ #messages = messages or [{"role": "system", "content": initMsg}]
67
+ localvar = localvar or {"messages": [{"role": "system", "content": initMsg}], "race": "", "class": ""}
68
+ messages = localvar['messages']
69
+ #if len(messages) == 1:
70
+
71
+ #memory = trimMessages(messages)
72
+ #logging.info("put memory:"+printMessages(memory))
73
+ #chat = openai.ChatCompletion.create(
74
+ # model="gpt-3.5-turbo", messages=memory,
75
+ # max_tokens=1048,n=1,temperature=0.5,
76
+ #)
77
+ #logging.info("put memory return:"+chat.choices[0].message.content)
78
+
79
+ if input:
80
+ messages.append({"role": "user", "content": input})
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
+ ans = ""
88
+ if len(messages) == 2:
89
+ localvar['race'] = localRace(getChooseRole(reply))
90
+ ans = "I choose "+ localvar['race'] +" as my race." + classAsk
91
+ logging.info("ChooseRole:"+ans)
92
+ if len(messages) == 4:
93
+ localvar['class'] = getChooseClass(reply)
94
+ ans = "My Race is "+ localvar['race'] +",I choose "+ localvar['class'] +" as my class." + classAns
95
+ logging.info("ChooseClass:"+ans)
96
+ messages.append({"role": "assistant", "content": ans})
97
+ localvar['messages'] = messages
98
+ return reply, printMessages(messages), localvar
99
+
100
+ def printMessages(messages):
101
+ delimiter = '\n'
102
+ msg_string = delimiter.join([f"{obj['role']}:{obj['content']}" for obj in messages])
103
+ logging.info("messages:"+msg_string)
104
+ return msg_string
105
+
106
+ app = gr.Interface(fn=chatbot, inputs=[gr.Textbox(lines=7, label="You ask and answer questions below"), "state"],
107
+ outputs=[gr.Textbox(label="DND Game Reply", placeholder=roleAns), gr.Textbox(label="History"), "state"], title="DND Game",#, gr.Textbox(label="History"),
108
+ description="DND Game",theme="compact")
109
+ app.launch(share=False)