| import streamlit as st |
| from Backend import get_correction_and_comments, generate_questions |
| from logger import logger |
| from pymongo import MongoClient, errors |
| import interpreter |
| from dotenv import load_dotenv |
| import os |
|
|
| load_dotenv() |
|
|
| |
| st.set_page_config(page_title="Interactive Code Assistant", layout="wide") |
| st.title("✨ Interactive Code Assistant with Python Interpreter ✨") |
|
|
| |
| if "helpful" not in st.session_state: |
| st.session_state.helpful = None |
|
|
| |
| try: |
| client = MongoClient(os.getenv("MONGO_URI"), serverSelectionTimeoutMS=5000) |
| client.server_info() |
| db = client["Capstone"] |
| feedback_collection = db["Feedback"] |
| except errors.ServerSelectionTimeoutError: |
| st.error("Error: Could not connect to MongoDB. Please ensure MongoDB is running.") |
|
|
| |
| colu1, colu2 = st.columns([1, 1]) |
|
|
| |
| with colu1: |
| st.subheader("Code Input") |
| code_input = st.text_area("Enter Your Python Code:", height=400, max_chars=10000) |
|
|
| |
| with colu2: |
| st.subheader("Corrected Output") |
| gemini_output = st.empty() |
|
|
| |
| st.subheader("Select Question Type") |
| question_type = st.selectbox( |
| "Choose the type of questions to generate:", |
| ["Logical Questions", "Interview-Based Questions", "Code Analysis Questions"] |
| ) |
|
|
| |
| col1, col2, col3 = st.columns([0.3, 0.3, 0.3]) |
| 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: {e}") |
| logger.error(f"Code execution error: {e}") |
|
|
| with col2: |
| if st.button("Generate Questions"): |
| logger.info(f"Generating {question_type.lower()}.") |
| try: |
| |
| 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.") |
| corrected_code = get_correction_and_comments(code_input) |
| gemini_output.code(corrected_code, language="python") |
|
|
| |
| st.subheader("Feedback") |
| 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: |
| feedback_collection.insert_one({"helpful": st.session_state.helpful, "question_type": question_type}) |
| st.success("Feedback submitted successfully.") |
| except Exception as e: |
| st.error(f"Failed to submit feedback: {e}") |
|
|
| |
| 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 |
| ) |
|
|