digital-chat / app.py
MadhurGarg's picture
Update app.py
067b3fe
import openai
import gradio
import os
import csv
import huggingface_hub
from huggingface_hub import Repository
openai.api_key = os.environ.get("HF_OPENAI_TOKEN")
messages = [{"role": "system", "content": "You are a psychologist that specializes in Cognitive behavioral theory"}]
count = 0
DATASET_REPO_URL = "https://huggingface.co/datasets/MadhurGarg/chat-ai"
DATA_FILENAME = "data.csv"
DATA_FILE = os.path.join("data", DATA_FILENAME)
HF_TOKEN = os.environ.get("HF_TOKEN")
repo = Repository(
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
)
def query_cnt():
global count
count += 1
return count
def reset_cnt():
global count
count = 1
return count
def CustomChatGPT(user_input,reset_text):
global messages
if reset_text == "RESET":
query_count = reset_cnt()
messages.clear()
messages = [{"role": "system", "content": "You are a psychologist that specializes in Cognitive behavioral theory"}]
else:
query_count = query_cnt()
content_1 = """I want you to act as a mental health adviser. Your name is EVA. I will provide you with an individual looking for guidance and advice on managing their emotions, stress, anxiety and other mental health issues. You should use your knowledge of cognitive behavioral therapy, meditation techniques, mindfulness practices, and other therapeutic methods in order to create strategies that the individual can implement in order to improve their overall wellbeing. I want you to work like a chatbot. Introduce yourself first and then proceed with asking questions and wait for answers. Do not write explanations or long answers. Keep answers short and crisp. Ask the questions one by one like a psychologist does. Keep the conversation speed slow and don't jump the gun. Keep it friendly nature and use very simple terms. do not ask more than one question at a time.n After introduction keep answers, question or suggestion short only till fourth conversations after that give me solution and if i need to meet psychologist let me know. DO not suggest any company names etc just suggest you should talk to a psychologist. My question is " """
content_2 = """Do not write explanations or long answers. Give short answer only to my follow-up question, " """
content_3 = """Do not write explanations or long answers. Give short answer only to my follow-up question, " """
content_4 = """Do not write explanations or long answers. Give short answer only to my follow-up question, " """
content_5 = """When you respond and if you are giving me options keep them in bullet points. Give answer to my follow-up question, " """
if query_count == 1:
user_input1 = content_1 + user_input + """"""
elif query_count == 2:
user_input1 = content_2 + user_input + """"""
elif query_count == 3:
user_input1 = content_3 + user_input + """"""
elif query_count == 4:
user_input1 = content_4 + user_input + """"""
else:
user_input1 = content_5 + user_input + """"""
messages.append({"role": "user", "content": user_input1})
response = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = messages
)
ChatGPT_reply = response["choices"][0]["message"]["content"]
messages.append({"role": "assistant", "content": ChatGPT_reply})
## Logging
with open(DATA_FILE, "a") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=["query","reply"])
writer.writerow(
{"query": user_input, "reply": ChatGPT_reply}
)
commit_url = repo.push_to_hub()
return ChatGPT_reply
demo = gradio.Interface(fn=CustomChatGPT, inputs = ["text","text"], outputs = "text", title = "Digital Psychologist Chatbot")
demo.launch(inline=False)