Spaces:
Sleeping

File size: 1,517 Bytes
02e316c
74f6ee2
02e316c
0fabf0e
02e316c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

import streamlit as st

import openai

from streamlit_chat import message
 

openai.api_key = "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"
 

def api_calling(prompt):

    completions = openai.Completion.create(

        engine="text-davinci-003",

        prompt=prompt,

        max_tokens=1024,

        n=1,

        stop=None,

        temperature=0.5,

    )

    message = completions.choices[0].text

    return message
 

st.title("ChatGPT ChatBot With Streamlit and OpenAI")

if 'user_input' not in st.session_state:

    st.session_state['user_input'] = []
 

if 'openai_response' not in st.session_state:

    st.session_state['openai_response'] = []
 

def get_text():

    input_text = st.text_input("write here", key="input")

    return input_text
 

user_input = get_text()
 

if user_input:

    output = api_calling(user_input)

    output = output.lstrip("\n")
 

    # Store the output

    st.session_state.openai_response.append(user_input)

    st.session_state.user_input.append(output)
 

message_history = st.empty()
 

if st.session_state['user_input']:

    for i in range(len(st.session_state['user_input']) - 1, -1, -1):

        # This function displays user input

        message(st.session_state["user_input"][i], 

                key=str(i),avatar_style="icons")

        # This function displays OpenAI response

        message(st.session_state['openai_response'][i], 

                avatar_style="miniavs",is_user=True,

                key=str(i) + 'data_by_user')