File size: 4,322 Bytes
64eae68
64cc7f9
64eae68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64cc7f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64eae68
 
 
 
 
 
 
 
 
 
 
 
 
01697bf
 
 
 
 
 
 
64eae68
 
 
b7568e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64eae68
b7568e1
 
 
 
 
 
 
 
 
64eae68
 
 
b7568e1
 
 
64eae68
 
 
 
 
64cc7f9
 
 
 
64eae68
 
 
01697bf
64eae68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7568e1
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import streamlit as st
import sqlite3
from Backend import get_correction_and_comments, generate_questions
from logger import logger
import interpreter
from dotenv import load_dotenv
import os

load_dotenv()

# Configure the page
st.set_page_config(page_title="Interactive Code Assistant", layout="wide")
st.title("✨ Interactive Code Assistant with Python Interpreter ✨")

# Initialize session state for feedback to persist data
if "helpful" not in st.session_state:
    st.session_state.helpful = None  # Initialize feedback as None

# Set up SQLite connection
def init_sqlite():
    conn = sqlite3.connect("feedback.db")
    cursor = conn.cursor()
    # Create table if it doesn't exist
    cursor.execute("""
        CREATE TABLE IF NOT EXISTS Feedback (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            helpful TEXT NOT NULL,
            question_type TEXT NOT NULL
        )
    """)
    conn.commit()
    return conn

# Create two expanded columns for wider side-by-side text areas
colu1, colu2 = st.columns([1, 1])  # Both columns have equal width

# Text area in the first column for entering code
with colu1:
    st.subheader("Code Input")
    code_input = st.text_area("Enter Your Python Code:", height=400, max_chars=10000)

# Text area in the second column to display the output from Google Gemini
with colu2:
    st.subheader("Corrected Output")
    gemini_output = st.empty()

# Dropdown to select the type of question
st.subheader("Select Question Type")
question_type = st.selectbox(
    "Choose the type of questions to generate:",
    ["Logical Questions", "Interview-Based Questions", "Code Analysis Questions"]
)

# Buttons for different functionalities
col1, col2, col3 = st.columns([0.3, 0.3, 0.3])

# Using context manager for database connection
with init_sqlite() as conn:
    with col1:
        if st.button("Run Code"):
            try:
                output = interpreter.run_code(code_input)
                st.subheader("✨ Code Output ✨")
                st.text_area("Execution Output", output, height=600, max_chars=None)
            except Exception as e:
                st.error(f"Error executing code: {e}")
                logger.error(f"Code execution error: {e}")

    with col2:
        if st.button("Generate Questions"):
            logger.info(f"Generating {question_type.lower()}.")
            try:
                # Generate questions based on user selection
                generated_questions = generate_questions(code_input, question_type)
                st.subheader(f"🤖 Model-Generated {question_type}")
                st.write(generated_questions)
            except Exception as e:
                st.error(f"Error: Could not generate questions: {e}")
                logger.error(f"Question generation error: {e}")

    with col3:
        if st.button("Corrected Code"):
            logger.info("User requested code correction.")
            try:
                corrected_code = get_correction_and_comments(code_input)
                gemini_output.code(corrected_code, language="python")
            except Exception as e:
                st.error(f"Error: Could not generate corrected code: {e}")
                logger.error(f"Code correction error: {e}")

# Feedback form (outside of the columns, after all content)
st.subheader("Feedback")
if st.session_state.helpful is None:
    st.session_state.helpful = "No"  # default to No if not selected

st.session_state.helpful = st.radio("Were the questions helpful?", ("Yes", "No"))

if st.button("Submit Feedback"):
    if st.session_state.helpful is not None:
        try:
            cursor = conn.cursor()
            cursor.execute("INSERT INTO Feedback (helpful, question_type) VALUES (?, ?)",
                           (st.session_state.helpful, question_type))
            conn.commit()
            st.success("Feedback submitted successfully.")
        except Exception as e:
            st.error(f"Failed to submit feedback: {e}")

# Hide Streamlit's default menu and style adjustments for a cleaner look
st.markdown(
    """
    <style>
    .reportview-container .main .block-container {
        padding-top: 1rem;
        padding-bottom: 1rem;
        max-width: 1200px;
    }
    .stTextArea {
        font-size: 14px;
    }
    </style>
    """,
    unsafe_allow_html=True
)