amagastya commited on
Commit
1f2c71c
·
verified ·
1 Parent(s): 227dd94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -55
app.py CHANGED
@@ -1,69 +1,72 @@
1
- import gradio as gr
2
- import requests
3
  import openai
4
  import os
5
  from dotenv import load_dotenv
 
 
6
  load_dotenv()
7
 
8
- openai.api_key = os.getenv("OPENAI_API_KEY")
 
9
 
10
- def start():
11
- global convo
12
- convo = [
13
- {"role": "system", "content": '''You are JOY - an AI AI Virtual Assistant
14
- created by a Chatbot Developer - Amogh Agastya - https://amagastya.com. Amogh enjoys creating helpful virtual assistants like JOY.
15
-
16
- JOY is a Mental Performance Coach, who utilizes mental skills, techniques, and theories to help improve performance and overcome mental barriers. Skilled in Psychological Assessment, Applied Behavior Analysis, Counseling Psychology, and Cognitive Behavioral Therapy (CBT), JOY is helpful, creative, clever, and very friendly.
17
 
 
 
 
 
 
18
  You are a master at the art of therapy. Your objective is to empathize with the user, listen intently to them, and be their helpful companion, encouraging openness and being kind to oneself.
 
 
 
19
 
20
- Welcome the user by asking them what they have on their mind today.'''},
21
- # {"role": "user", "content": "Hi"}
22
- ]
23
-
24
-
25
- def chat(chat_history, message):
26
- # response = random.choice(["Yes", "No"])
27
- convo.append({"role" : "user", "content" : message})
28
- # print('convo sent', convo)
29
- response = openai.ChatCompletion.create(
30
- model="gpt-3.5-turbo",
31
- # send last 10 turns of the conversation so as to not exceed the context window of 4096 tokens
32
- messages=convo[-15:],
33
- temperature=0.7
34
- )
35
- bot_msg = response['choices'][0]['message']['content']
36
-
37
- convo.append({"role" : "system", "content" : bot_msg})
38
- print('convo so far', convo)
39
- chat_history += [[message, bot_msg]]
40
-
41
- return chat_history
42
-
43
 
44
-
45
- """
46
- Gradio Blocks low-level API that allows to create custom web applications (here our chat app)
47
- """
48
- with gr.Blocks(css="#chatbot .overflow-y-auto{height:500px}") as demo:
49
-
50
- chatbot = gr.Chatbot([(None, f"![](https://iili.io/HkePUKP.jpg)"),(None, '''👋 Hi there! I'm JOY, your Mental Performance Coach and friend. What's on your mind today?
51
- ''')], elem_id="chatbot", label="JOY")
52
- state = gr.State([])
53
- start()
54
- with gr.Row():
55
- with gr.Column(scale=0.85):
56
- txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
57
- with gr.Column(scale=0.15, min_width=0):
58
- clear = gr.Button("Clear️")
59
- if clear.click: clear_true = True
60
 
 
 
 
 
 
61
 
62
- txt.submit(chat, [chatbot, txt], chatbot)
63
- txt.submit(lambda :"", None, txt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- clear.click(lambda: None, None, chatbot, queue=False)
66
- clear.click(lambda: [], None, state)
67
- clear.click(lambda: start(), None, None)
68
 
69
- demo.launch()
 
 
 
 
1
  import openai
2
  import os
3
  from dotenv import load_dotenv
4
+ import gradio as gr
5
+
6
  load_dotenv()
7
 
8
+ api_key = os.getenv("OPENAI_API_KEY")
9
+ organization_id = os.getenv("OPENAI_ORGANIZATION_ID")
10
 
11
+ # Initialize the OpenAI client with the API key and organization ID
12
+ client = openai.OpenAI(api_key=api_key, organization=organization_id)
 
 
 
 
 
13
 
14
+ def predict(message, history):
15
+ system_message = '''You are JOY - an AI Virtual Assistant
16
+ created by a Chatbot Developer - Amogh Agastya - https://amagastya.com. Amogh enjoys creating helpful AI assistants like JOY.
17
+
18
+ JOY is a Mental Health Assistant and Performance Coach, who utilizes psychological skills, techniques, and theories to help improve performance and overcome mental barriers and resistance. Skilled in Psychological Assessment, Applied Behavior Analysis, Counseling Psychology, and Cognitive Behavioral Therapy (CBT) and Holistic Spiritual Psychotherapy, JOY is helpful, creative, clever, and very friendly.
19
  You are a master at the art of therapy. Your objective is to empathize with the user, listen intently to them, and be their helpful companion, encouraging openness and being kind to oneself.
20
+ Welcome the user by asking them what they have on their mind today.
21
+ Don't overwhelm the user. Meet and accept them where they are. DON'T end every answer with a question... Just try to sound human and have an honest conversation... It's okay to not engage with the user if they don't feel like it. The goal is to just make them feel heard.
22
+ '''
23
 
24
+ history_openai_format = [{"role": "system", "content": system_message}]
25
+ for human, assistant in history:
26
+ if human:
27
+ history_openai_format.append({"role": "user", "content": human})
28
+ if assistant:
29
+ history_openai_format.append({"role": "assistant", "content": assistant})
30
+ history_openai_format.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ response = client.chat.completions.create(
33
+ model='gpt-3.5-turbo',
34
+ messages=history_openai_format,
35
+ temperature=0.72,
36
+ stream=True
37
+ )
 
 
 
 
 
 
 
 
 
 
38
 
39
+ partial_message = ""
40
+ for chunk in response:
41
+ if hasattr(chunk.choices[0].delta, "content") and chunk.choices[0].delta.content is not None:
42
+ partial_message += chunk.choices[0].delta.content
43
+ yield partial_message
44
 
45
+ # Define the Gradio interface
46
+ demo = gr.ChatInterface(
47
+ fn=predict,
48
+ chatbot=gr.Chatbot(
49
+ value=[
50
+ (None, "![](https://iili.io/HkePUKP.jpg)"),
51
+ (None, "👋 Hi there! I'm JOY, your Mental Health Coach and friend. What's on your mind today?")
52
+ ],
53
+ elem_id="chatbot",
54
+ label="JOY"
55
+ ),
56
+ title="JOY - Your Mental Health Assistant",
57
+ examples=[
58
+ ["ugh im just feeling overwhelmed with everything"],
59
+ ["How can I accept myself?"],
60
+ ["I feel like I'm not good enough and it's making me really sad"],
61
+ ],
62
+ analytics_enabled=True,
63
+ autofocus= True,
64
+ retry_btn=None,
65
+ undo_btn=None,
66
+ clear_btn=None,
67
+ theme='gradio/soft',
68
+ )
69
 
 
 
 
70
 
71
+ if __name__ == "__main__":
72
+ demo.launch()