File size: 2,923 Bytes
2d63df8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)}")