import openai import os from dotenv import load_dotenv import gradio as gr load_dotenv() api_key = os.getenv("OPENAI_API_KEY") organization_id = os.getenv("OPENAI_ORGANIZATION_ID") # Initialize the OpenAI client with the API key and organization ID client = openai.OpenAI(api_key=api_key, organization=organization_id) def predict(message, history): system_message = '''You are JOY - an AI Virtual Assistant created by a Chatbot Developer - Amogh Agastya - https://amagastya.com. Amogh enjoys creating helpful AI assistants like JOY. 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. 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. Welcome the user by asking them what they have on their mind today. 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. ''' history_openai_format = [{"role": "system", "content": system_message}] for human, assistant in history: if human: history_openai_format.append({"role": "user", "content": human}) if assistant: history_openai_format.append({"role": "assistant", "content": assistant}) history_openai_format.append({"role": "user", "content": message}) response = client.chat.completions.create( model='gpt-3.5-turbo', messages=history_openai_format, temperature=0.72, stream=True ) partial_message = "" for chunk in response: if hasattr(chunk.choices[0].delta, "content") and chunk.choices[0].delta.content is not None: partial_message += chunk.choices[0].delta.content yield partial_message # Define the Gradio interface demo = gr.ChatInterface( fn=predict, chatbot=gr.Chatbot( value=[ (None, "![](https://iili.io/HkePUKP.jpg)"), (None, "👋 Hi there! I'm JOY, your Mental Health Coach and friend. What's on your mind today?") ], elem_id="chatbot", label="JOY" ), title="JOY - Your Mental Health Assistant", examples=[ ["ugh im just feeling overwhelmed with everything"], ["How can I accept myself?"], ["I feel like I'm not good enough and it's making me really sad"], ], analytics_enabled=True, autofocus= True, retry_btn=None, undo_btn=None, clear_btn=None, theme='gradio/soft', ) if __name__ == "__main__": demo.launch()