Scakmak commited on
Commit
8630213
1 Parent(s): 96f3c0d

session problem is solved

Browse files
Files changed (1) hide show
  1. app.py +25 -19
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import openai
3
 
 
4
  import os
5
  from dotenv import load_dotenv
6
 
@@ -8,17 +9,18 @@ load_dotenv()
8
 
9
  openai.api_key = os.getenv("openai_api_key")
10
 
11
- history = []
12
  character_name = "AI"
13
- chat_history = []
14
 
15
- def openai_chat(prompt):
16
- print("buda prompt", prompt, " ", character_name)
17
- history.append({"role": "user", "content": prompt})
 
18
 
19
  response = openai.ChatCompletion.create(
20
  model="gpt-3.5-turbo",
21
- messages=history,
22
  max_tokens=150,
23
  temperature=0.6,
24
  top_p=1,
@@ -29,32 +31,36 @@ def openai_chat(prompt):
29
  return response.choices[0].message["content"].strip()
30
 
31
 
32
- # def character_selection(prompt_input):
33
- # history.append(prompt_input)
34
 
35
- def set_character_name(prompt_input):
36
  print(prompt_input)
37
  character_name = prompt_input
38
- history.append({"role": "system", "content": f"You are {character_name}. You will talk and think like {character_name} from now on."})
39
-
40
- return {msg: msg.update(visible=True), chatbot: chatbot.update(visible=True), char_selection : char_selection.update(visible=False), title: title.update( value = f"Chat with {character_name.upper()}",visible=True)}
41
 
42
- def respond(message):
43
- bot_message = openai_chat(message)
44
- history.append({"role": "assistant", "content": bot_message})
45
- chat_history.append((message, bot_message))
46
- return {msg: msg.update(value="", visible=True), chatbot: chatbot.update(value= chat_history,visible=True)}
 
 
 
47
 
48
 
49
  with gr.Blocks() as demo:
 
 
50
  char_selection = gr.Textbox(lines=1 , label="Enter the character you want to talk to:")
51
  title = gr.Markdown( visible=False)
52
  chatbot = gr.Chatbot(visible=False)
53
  msg = gr.Textbox(visible=False)
54
 
55
- char_selection.submit(set_character_name, char_selection, [chatbot, msg, char_selection, title])
56
 
57
- msg.submit(respond, msg, [chatbot, msg])
58
 
59
 
60
 
 
1
  import gradio as gr
2
  import openai
3
 
4
+
5
  import os
6
  from dotenv import load_dotenv
7
 
 
9
 
10
  openai.api_key = os.getenv("openai_api_key")
11
 
12
+ # history = []
13
  character_name = "AI"
14
+ # chat_history = []
15
 
16
+ def openai_chat(prompt, ch_history):
17
+
18
+ ch_history.append({"role": "user", "content": prompt})
19
+ messagesFiltered = [ch_history[i] for i in range(len(ch_history)) if ((i % 3 !=0) | (i ==0)) ]
20
 
21
  response = openai.ChatCompletion.create(
22
  model="gpt-3.5-turbo",
23
+ messages=messagesFiltered,
24
  max_tokens=150,
25
  temperature=0.6,
26
  top_p=1,
 
31
  return response.choices[0].message["content"].strip()
32
 
33
 
34
+
 
35
 
36
+ def set_character_name(prompt_input, ch_history):
37
  print(prompt_input)
38
  character_name = prompt_input
39
+ #history.append({"role": "system", "content": f"You are {character_name}. You will talk and think like {character_name} from now on."})
40
+ ch_history.append({"role": "system", "content": f"You are {character_name}. You will talk and think like {character_name} from now on."})
41
+ return {msg: msg.update(visible=True), chatbot: chatbot.update(visible=True), char_selection : char_selection.update(visible=False), title: title.update( value = f"Chat with {character_name.upper()}",visible=True), state: ch_history}
42
 
43
+ def respond(message, ch_history):
44
+ bot_message = openai_chat(message, ch_history)
45
+ #history.append({"role": "assistant", "content": bot_message})
46
+ ch_history.append({"role": "assistant", "content": bot_message})
47
+ ch_history.append((message, bot_message))
48
+ chats = [ch_history[i] for i in range(len(ch_history)) if ((i % 3 == 0) & (i !=0)) ]
49
+
50
+ return {msg: msg.update(value="", visible=True), chatbot: chatbot.update(value= chats,visible=True), state: ch_history}
51
 
52
 
53
  with gr.Blocks() as demo:
54
+ state = gr.State([])
55
+
56
  char_selection = gr.Textbox(lines=1 , label="Enter the character you want to talk to:")
57
  title = gr.Markdown( visible=False)
58
  chatbot = gr.Chatbot(visible=False)
59
  msg = gr.Textbox(visible=False)
60
 
61
+ char_selection.submit(set_character_name, [char_selection, state], [chatbot, msg, char_selection, title,state])
62
 
63
+ msg.submit(respond, [msg, state], [chatbot, msg,state])
64
 
65
 
66