File size: 2,539 Bytes
90c735c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import spacy
import openai
import gradio as gr


# Loading the pre-trained English language model in spaCy
try:
    nlp = spacy.load('en_core_web_sm')
except OSError:
    spacy.cli.download('en_core_web_sm')
    nlp = spacy.load('en_core_web_sm')

# Function to obtain word embeddings
def get_word_embeddings(sentence):
    tokens = nlp(sentence)
    embeddings = [token.vector for token in tokens]
    return embeddings

# Function to get the chatbot response
def get_response(message):
    openai.api_key = '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'
    
    # Obtain word embeddings for the user message
    embeddings = get_word_embeddings(message)
    
    # Convert the embeddings to a string representation
    embeddings_str = [str(embedding) for embedding in embeddings]
    
    response = openai.ChatCompletion.create(
        model='gpt-3.5-turbo-16k',
        messages=[
            {"role":"system", "content":"You are a polite, helpful postpartum care assistant who answers anything related to POSTPARTUM CARE, for other questions politely say you cannot answer, if you have any concern related then I can help."},
            # {"role":"user","content": message},
            # {"role":"assistant","content":""},

            {"role": "user", "content": message + " "+ " ".join(embeddings_str) }
        ],
        max_tokens=100,
        temperature=0.9,
    )
    return response["choices"][0]["message"]["content"]

def chatbot_interface(input_text):
    response = get_response(input_text)
    return response

# code to create the Gradio app interface

iface = gr.Interface(
    fn=chatbot_interface,
    inputs="text",
    outputs="text",
    layout="vertical",
    title="Postpartum Care Chatbot",
    description="Ask any postpartum care-related questions!",
    theme='HaleyCH/HaleyCH_Theme',
    # inputs_layout="textarea",
    # outputs_layout="textarea",
    examples=[
        ["Hi, how can I take care of my newborn's skin?"],
        ["What is postpartum depression? What are the signs and symptoms?"],
        ["What activities are safe to do in the first few days? Which activities should I avoid?"],
        ['Are there certain foods or beverages I should avoid when breastfeeding?'],
        ["What should I do to prevent deep vein thrombosis?"],
        ['What should I do to help prevent post-delivery infections??']
    ],
)

# Start the Gradio app
if __name__ == "__main__":
    print("Welcome to the Postpartum Care Chatbot!")
    print("How can I assist you today??")
    iface.launch()