Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import openai | |
| import os | |
| # Set up OpenAI API key | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| st.title("Coding Interview Bot") | |
| # Initialize chat history | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # Display chat messages from history on app rerun | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| # Function to generate response using OpenAI API | |
| def generate_response(prompt): | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| {"role": "system", "content": "You are a coding interview bot. Your task is to ask coding interview questions, evaluate responses, and provide feedback."}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| max_tokens=1000, | |
| temperature=0.7, | |
| ) | |
| return response.choices[0].message['content'] | |
| # React to user input | |
| if prompt := st.chat_input("What's your coding question?"): | |
| # Display user message in chat message container | |
| st.chat_message("user").markdown(prompt) | |
| # Add user message to chat history | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| # Generate response | |
| response = generate_response(prompt) | |
| # Display assistant response in chat message container | |
| with st.chat_message("assistant"): | |
| st.markdown(response) | |
| # Add assistant response to chat history | |
| st.session_state.messages.append({"role": "assistant", "content": response}) | |
| st.sidebar.markdown(""" | |
| ## About | |
| This is a coding interview bot powered by OpenAI's GPT-3.5-turbo. | |
| Ask coding questions and get responses to help you prepare for interviews! | |
| """) |