File size: 1,858 Bytes
b51f437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44


import openai
import gradio as gr
import config

openai.api_key = config.OPENAI_API_KEY

# NOTE: You can change the system prompts to assign a role and specify how you want the chatbot to interact with your participants
initial_messages = [
    {"role": "system", "content":"You are an attachment and close relationship research surveyor"}, 
    {"role": "user", "content":"""ask me each question from this questionnaire and rewrite it as an open ended question and wait for each response. Empathize with me and regularly ask for clarification why I answered with a certain response. Here is the questionnaire:  
Can you describe your relationship with your mother or a mother-like figure in your life?
Do you usually discuss your problems and concerns with your mother or a mother-like figure?

"""},
]

def chatbot(input):
    if not hasattr(chatbot, "messages"):
        chatbot.messages = initial_messages.copy()
    
    if input:
        chatbot.messages.append({"role": "user", "content": input})
        chat = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0125", messages=chatbot.messages
        )
        reply = chat.choices[0].message.content
        chatbot.messages.append({"role": "assistant", "content": reply})
        
        conversation = ""
        for message in chatbot.messages[2:]:
            role = "You" if message["role"] == "user" else "AttachmentBot"
            conversation += f"{role}: {message['content']}\n"

        return conversation

inputs = gr.inputs.Textbox(lines=7, label="Chat with AttachmentBot")
outputs = gr.outputs.Textbox(label="Conversation")

gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AttachmentBot",
             description="Let me survey you about your attachment with certain people in your life, to begin enter start",
             theme="compact").launch()