Spaces:
Sleeping
Sleeping
first commit
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Set up OpenAI API key
|
6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
+
|
8 |
+
st.title("Coding Interview Bot")
|
9 |
+
|
10 |
+
# Initialize chat history
|
11 |
+
if "messages" not in st.session_state:
|
12 |
+
st.session_state.messages = []
|
13 |
+
|
14 |
+
# Display chat messages from history on app rerun
|
15 |
+
for message in st.session_state.messages:
|
16 |
+
with st.chat_message(message["role"]):
|
17 |
+
st.markdown(message["content"])
|
18 |
+
|
19 |
+
# Function to generate response using OpenAI API
|
20 |
+
def generate_response(prompt):
|
21 |
+
response = openai.ChatCompletion.create(
|
22 |
+
model="gpt-3.5-turbo",
|
23 |
+
messages=[
|
24 |
+
{"role": "system", "content": "You are a coding interview bot. Your task is to ask coding interview questions, evaluate responses, and provide feedback."},
|
25 |
+
{"role": "user", "content": prompt}
|
26 |
+
],
|
27 |
+
max_tokens=1000,
|
28 |
+
temperature=0.7,
|
29 |
+
)
|
30 |
+
return response.choices[0].message['content']
|
31 |
+
|
32 |
+
# React to user input
|
33 |
+
if prompt := st.chat_input("What's your coding question?"):
|
34 |
+
# Display user message in chat message container
|
35 |
+
st.chat_message("user").markdown(prompt)
|
36 |
+
# Add user message to chat history
|
37 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
38 |
+
|
39 |
+
# Generate response
|
40 |
+
response = generate_response(prompt)
|
41 |
+
|
42 |
+
# Display assistant response in chat message container
|
43 |
+
with st.chat_message("assistant"):
|
44 |
+
st.markdown(response)
|
45 |
+
# Add assistant response to chat history
|
46 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
47 |
+
|
48 |
+
st.sidebar.markdown("""
|
49 |
+
## About
|
50 |
+
This is a coding interview bot powered by OpenAI's GPT-3.5-turbo.
|
51 |
+
Ask coding questions and get responses to help you prepare for interviews!
|
52 |
+
""")
|