|
import streamlit as st |
|
import json |
|
import google.generativeai as genai |
|
|
|
|
|
genai.configure(api_key="AIzaSyCA4__JMC_ZIQ9xQegIj5LOMLhSSrn3pMw") |
|
|
|
|
|
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"}, |
|
] |
|
|
|
|
|
model = genai.GenerativeModel( |
|
model_name="gemini-1.0-pro", generation_config=generation_config, safety_settings=safety_settings |
|
) |
|
|
|
|
|
with open("dsa_questions.json", "r") as file: |
|
questions = json.load(file) |
|
|
|
def next_question(): |
|
|
|
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(): |
|
|
|
st.title("Coding Screen") |
|
|
|
|
|
current_question_index = st.session_state.get("current_question_index", 0) |
|
progress = (current_question_index + 1) / len(questions) |
|
st.progress(progress) |
|
|
|
|
|
question = questions[current_question_index] |
|
st.markdown(f"### Question {current_question_index + 1}: {question['title']}") |
|
st.markdown(f"**Description:** {question['description']}") |
|
|
|
|
|
user_answer = st.text_area("Enter your answer here", height=150) |
|
|
|
|
|
if st.button("Submit"): |
|
|
|
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) |
|
|
|
|
|
|
|
if st.button("Next"): |
|
next_question() |
|
|
|
|
|
st.caption(f"Question {current_question_index + 1} of {len(questions)}") |
|
|
|
|