-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
108 lines (84 loc) · 3.88 KB
/
app.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import streamlit as st
import openai
import os
import json
openai_api_key =st.secrets['OPENAI_API_KEY']
def new_session():
st.session_state['text']=''
file = 'database.json'
with open (file,'w') as f:
f.seek(0)
f.truncate()
def clear_history():
file = 'database.json'
with open (file,'w') as f:
f.seek(0)
f.truncate()
if 'chat_history' not in st.session_state: # Creating a list -chat history in session state if not existed
st.session_state.chat_history=[]
if "messages" not in st.session_state: # Creating messages list in session state if not existed
st.session_state.messages = []
with st.sidebar:
st.header("Interview Chat bot")
st.button("New Session",on_click=new_session)
st.write("For Reference")
st.write('''-[Streamlit](https://streamlit.io/)
- [LangChain](https://python.langchain.com/)
- [OpenAI](https://platform.openai.com/docs/models)''')
def main():
name=st.text_input("Enter your name",key="text")
options=st.chat_input("Enter your response ")
# if options == '':
if options == None:
st.markdown("Please enter a Role to start the interview")
# st.write("Enter your role")
else:
with st.chat_message("assistant"):
st.write(f"Hi {name}")
chat_response=get_chat_response(options)
# st.markdown(chat_response)
for message in st.session_state["messages"]:
if message["role"] == "user": # Display a user message in the chat interface
st.write('👤:',message["content"])
elif message["role"] == "assistant":
# Display an assistant message in the chat interface
st.write('🤖:',message["content"])
def get_chat_response(user_message):
messages = load_messages()
messages.append({"role": "user", "content": user_message})
# Send to ChatGpt/OpenAi
gpt_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
parsed_gpt_response = gpt_response['choices'][0]['message']['content']
# Save messages
save_messages(user_message, parsed_gpt_response)
return parsed_gpt_response
def load_messages():
messages = []
options=''
name=''
file = 'database.json'
empty = os.stat(file).st_size == 0
if not empty:
with open(file) as db_file:
data = json.load(db_file)
for item in data:
messages.append(item)
else:
messages.append(
{"role": "system", "content": f"You are interviewing the user for a {options} position. Ask short techincal questions that are relevant to a same role.The user is {name}.Ask a single question.Ask the candidate the next question as an interviewer.After 3-4 questions,give the feedback of the interview andend the interview"}
)
return messages
def save_messages(user_message, gpt_response):
file = 'database.json'
messages = load_messages()
messages.append({"role": "user", "content": user_message})
st.session_state.messages.append({"role": "user", "content": user_message})
messages.append({"role": "assistant", "content": gpt_response})
st.session_state.messages.append({"role": "assistant", "content": gpt_response})
with open(file, 'w') as f:
json.dump(messages, f)
if __name__ == '__main__':
main()