Spaces:
Sleeping
Sleeping
adding survey logic
Browse files- chatbot.py +52 -3
chatbot.py
CHANGED
|
@@ -5,7 +5,7 @@ import psycopg2
|
|
| 5 |
from urllib.parse import urlparse
|
| 6 |
|
| 7 |
# Initialize OpenAI client
|
| 8 |
-
|
| 9 |
|
| 10 |
# Database URL
|
| 11 |
DATABASE_URL = os.getenv('DATABASE_URL')
|
|
@@ -50,6 +50,12 @@ def create_table():
|
|
| 50 |
transcript TEXT NOT NULL,
|
| 51 |
timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
|
| 52 |
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
'''
|
| 54 |
cur.execute(create_table_query)
|
| 55 |
conn.commit()
|
|
@@ -103,9 +109,42 @@ def authenticate(username, password):
|
|
| 103 |
return user_id[0]
|
| 104 |
return None
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
# Initialize the table
|
| 107 |
create_table()
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
# Initial system messages for the chatbot
|
| 110 |
initial_messages = [
|
| 111 |
{"role": "system", "content": "You are an attachment and close relationship research surveyor"},
|
|
@@ -121,11 +160,11 @@ def chatbot(input, state):
|
|
| 121 |
|
| 122 |
if input:
|
| 123 |
messages.append({"role": "user", "content": input})
|
| 124 |
-
response =
|
| 125 |
model="gpt-3.5-turbo",
|
| 126 |
messages=messages
|
| 127 |
)
|
| 128 |
-
reply = response.choices[0].message
|
| 129 |
messages.append({"role": "assistant", "content": reply})
|
| 130 |
|
| 131 |
conversation = ""
|
|
@@ -147,6 +186,9 @@ with gr.Blocks() as demo:
|
|
| 147 |
chat_output = gr.Textbox(label="Conversation", visible=False)
|
| 148 |
state = gr.State([None, initial_messages.copy()])
|
| 149 |
|
|
|
|
|
|
|
|
|
|
| 150 |
def login(username, password):
|
| 151 |
user_id = authenticate(username, password)
|
| 152 |
if user_id:
|
|
@@ -161,8 +203,15 @@ with gr.Blocks() as demo:
|
|
| 161 |
else:
|
| 162 |
return gr.update(visible=False), gr.update(visible=False), [None, initial_messages.copy()], "Registration failed, try a different username."
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
login_button.click(login, inputs=[username, password], outputs=[chat_input, chat_output, state, auth_message])
|
| 165 |
register_button.click(register, inputs=[username, password], outputs=[chat_input, chat_output, state, auth_message])
|
|
|
|
| 166 |
|
| 167 |
chat_interface = gr.Interface(
|
| 168 |
fn=chatbot,
|
|
|
|
| 5 |
from urllib.parse import urlparse
|
| 6 |
|
| 7 |
# Initialize OpenAI client
|
| 8 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
| 10 |
# Database URL
|
| 11 |
DATABASE_URL = os.getenv('DATABASE_URL')
|
|
|
|
| 50 |
transcript TEXT NOT NULL,
|
| 51 |
timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
|
| 52 |
);
|
| 53 |
+
CREATE TABLE IF NOT EXISTS survey_responses (
|
| 54 |
+
id SERIAL PRIMARY KEY,
|
| 55 |
+
user_id INTEGER REFERENCES users(id),
|
| 56 |
+
responses JSONB,
|
| 57 |
+
timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
|
| 58 |
+
);
|
| 59 |
'''
|
| 60 |
cur.execute(create_table_query)
|
| 61 |
conn.commit()
|
|
|
|
| 109 |
return user_id[0]
|
| 110 |
return None
|
| 111 |
|
| 112 |
+
# Store survey responses
|
| 113 |
+
def store_survey_responses(user_id, responses):
|
| 114 |
+
conn = connect_db()
|
| 115 |
+
if conn:
|
| 116 |
+
cur = conn.cursor()
|
| 117 |
+
insert_query = '''
|
| 118 |
+
INSERT INTO survey_responses (user_id, responses)
|
| 119 |
+
VALUES (%s, %s);
|
| 120 |
+
'''
|
| 121 |
+
cur.execute(insert_query, (user_id, responses))
|
| 122 |
+
conn.commit()
|
| 123 |
+
cur.close()
|
| 124 |
+
conn.close()
|
| 125 |
+
|
| 126 |
# Initialize the table
|
| 127 |
create_table()
|
| 128 |
|
| 129 |
+
# Survey questions with different input types
|
| 130 |
+
survey_questions = [
|
| 131 |
+
{"question": "On a scale of 1 to 5, how are you feeling today?", "type": "scale", "options": ["1", "2", "3", "4", "5"]},
|
| 132 |
+
{"question": "How often do you feel stressed?", "type": "multiple_choice", "options": ["Never", "Rarely", "Sometimes", "Often", "Always"]},
|
| 133 |
+
{"question": "Do you have trouble sleeping? (True/False)", "type": "true_false"}
|
| 134 |
+
]
|
| 135 |
+
|
| 136 |
+
# Function to create survey inputs dynamically
|
| 137 |
+
def create_survey_inputs():
|
| 138 |
+
inputs = []
|
| 139 |
+
for q in survey_questions:
|
| 140 |
+
if q["type"] == "scale":
|
| 141 |
+
inputs.append(gr.Radio(label=q["question"], choices=q["options"]))
|
| 142 |
+
elif q["type"] == "multiple_choice":
|
| 143 |
+
inputs.append(gr.Radio(label=q["question"], choices=q["options"]))
|
| 144 |
+
elif q["type"] == "true_false":
|
| 145 |
+
inputs.append(gr.Radio(label=q["question"], choices=["True", "False"]))
|
| 146 |
+
return inputs
|
| 147 |
+
|
| 148 |
# Initial system messages for the chatbot
|
| 149 |
initial_messages = [
|
| 150 |
{"role": "system", "content": "You are an attachment and close relationship research surveyor"},
|
|
|
|
| 160 |
|
| 161 |
if input:
|
| 162 |
messages.append({"role": "user", "content": input})
|
| 163 |
+
response = openai.ChatCompletion.create(
|
| 164 |
model="gpt-3.5-turbo",
|
| 165 |
messages=messages
|
| 166 |
)
|
| 167 |
+
reply = response.choices[0].message['content']
|
| 168 |
messages.append({"role": "assistant", "content": reply})
|
| 169 |
|
| 170 |
conversation = ""
|
|
|
|
| 186 |
chat_output = gr.Textbox(label="Conversation", visible=False)
|
| 187 |
state = gr.State([None, initial_messages.copy()])
|
| 188 |
|
| 189 |
+
survey_inputs = create_survey_inputs()
|
| 190 |
+
submit_survey = gr.Button("Submit Survey")
|
| 191 |
+
|
| 192 |
def login(username, password):
|
| 193 |
user_id = authenticate(username, password)
|
| 194 |
if user_id:
|
|
|
|
| 203 |
else:
|
| 204 |
return gr.update(visible=False), gr.update(visible=False), [None, initial_messages.copy()], "Registration failed, try a different username."
|
| 205 |
|
| 206 |
+
def submit_survey_fn(responses, state):
|
| 207 |
+
user_id = state[0]
|
| 208 |
+
survey_responses = {survey_questions[i]["question"]: responses[i] for i in range(len(responses))}
|
| 209 |
+
store_survey_responses(user_id, survey_responses)
|
| 210 |
+
return gr.update(visible=True), gr.update(visible=True), state
|
| 211 |
+
|
| 212 |
login_button.click(login, inputs=[username, password], outputs=[chat_input, chat_output, state, auth_message])
|
| 213 |
register_button.click(register, inputs=[username, password], outputs=[chat_input, chat_output, state, auth_message])
|
| 214 |
+
submit_survey.click(submit_survey_fn, inputs=survey_inputs + [state], outputs=[chat_input, chat_output, state])
|
| 215 |
|
| 216 |
chat_interface = gr.Interface(
|
| 217 |
fn=chatbot,
|