careerv3 / coder.py
ombhojane's picture
Upload 88 files
8d404bc verified
import streamlit as st
import json
import google.generativeai as genai
# Configure Gemini API
genai.configure(api_key="AIzaSyCA4__JMC_ZIQ9xQegIj5LOMLhSSrn3pMw")
# Set up the model generation config and safety settings
generation_config = {
"temperature": 0.9,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 2048,
}
safety_settings = [
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
]
# Initialize the Gemini model
model = genai.GenerativeModel(
model_name="gemini-1.0-pro", generation_config=generation_config, safety_settings=safety_settings
)
# Load the set of coding questions from dsa_questions.json
with open("dsa_questions.json", "r") as file:
questions = json.load(file)
def next_question():
# Increment the question index
current_question_index = st.session_state.get("current_question_index", 0)
next_question_index = (current_question_index + 1) % len(questions)
st.session_state["current_question_index"] = next_question_index
st.experimental_rerun()
def app():
# Set up the Streamlit app
st.title("Coding Screen")
# Progress bar
current_question_index = st.session_state.get("current_question_index", 0)
progress = (current_question_index + 1) / len(questions)
st.progress(progress)
# Display the current question using markdown for better formatting
question = questions[current_question_index]
st.markdown(f"### Question {current_question_index + 1}: {question['title']}")
st.markdown(f"**Description:** {question['description']}")
# Use columns to arrange text area and buttons
user_answer = st.text_area("Enter your answer here", height=150)
# Submit button
if st.button("Submit"):
# Evaluate user's answer
prompt_parts = [
f"Question: {question}",
f"User's answer:\n{user_answer}",
"Please provide the correct code for the given question or suggest a better version of the code if the user's answer is incorrect or suboptimal. Explain your approach and any improvements made. If the user's answer is correct, simply confirm that it is correct and provide any additional insights or optimizations if applicable.",
]
response = model.generate_content(prompt_parts)
analysis = response.text
st.success("Answer submitted!")
st.write(analysis)
# Next question button
if st.button("Next"):
next_question()
# Show current question number and total questions for better user orientation
st.caption(f"Question {current_question_index + 1} of {len(questions)}")