Scakmak commited on
Commit
b383690
·
1 Parent(s): d119986
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+
4
+ import os
5
+ from dotenv import load_dotenv
6
+
7
+ 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)
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,
25
+ frequency_penalty=0.5,
26
+ presence_penalty=0.0
27
+ )
28
+ print(response.choices[0].message["content"].strip())
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
+
61
+ demo.launch(share= True)