| import streamlit as st |
| from utility import chatbot_response, run_code_with_openai, get_new_example, get_quize, generate_content, run_code_locally |
|
|
| st.set_page_config(page_title="AI Tutor", page_icon="favicon.ico", |
| layout="wide", initial_sidebar_state="auto", menu_items=None) |
|
|
| |
| course_topics = { |
| "Introduction to Python": { |
| "description": """ |
| Python is a versatile programming language known for its simplicity and readability. |
| It is widely used in various fields, including web development, data analysis, artificial intelligence, |
| scientific computing, and more. In this section, we'll cover the basic syntax of Python, |
| including how to write simple programs using variables and data types. |
| """, |
| "example": """ |
| **Example: Basic Python Program** |
| |
| ```python |
| # A simple Python program to add two numbers |
| num1 = 5 |
| num2 = 3 |
| |
| # Adding two numbers |
| sum = num1 + num2 |
| |
| # Display the sum |
| print("The sum of", num1, "and", num2, "is", sum) |
| ``` |
| |
| **Output:** |
| ``` |
| The sum of 5 and 3 is 8 |
| ``` |
| """ |
| } |
| } |
|
|
| st.sidebar.title("Python Course") |
|
|
| pages = ["Home","Course Content", "Practice Coding"] |
| selected_page = st.sidebar.selectbox("Select a Page", pages) |
|
|
| selected_topic = "Introduction to Python" |
|
|
| if selected_page == "Home": |
| st.title("Introduction to Python Course") |
| st.subheader("Welcome to the Python Basics Course!") |
| |
| |
| st.write("In this course, we’ll cover the fundamentals of Python programming. Here’s an outline of the topics you will learn:") |
| |
| |
| topics = [ |
| "Introduction to Python", |
| "Data Types and Type Conversion", |
| "Operators", |
| "Functions", |
| "Data Structures", |
| "Modules and Packages", |
| "Exception Handling", |
| "OOP Basics" |
| ] |
| |
| for topic in topics: |
| st.markdown(f"- **{topic}**") |
| |
| st.write("\nThis course is designed for beginners to give you a solid foundation in Python programming.") |
|
|
| if selected_page == "Course Content": |
| if 'question_index' not in st.session_state: |
| st.session_state.question_index = 0 |
|
|
| st.title(selected_topic) |
|
|
| st.subheader("Description") |
| st.write(course_topics[selected_topic]["description"]) |
|
|
| st.subheader("Example") |
| example_text = course_topics[selected_topic]["example"] |
| st.markdown(example_text) |
|
|
| |
| if "generated_content" not in st.session_state: |
| st.session_state.generated_content = "" |
|
|
| |
| if st.button("Next Topic"): |
| st.session_state.generated_content = generate_content(selected_topic) |
|
|
| |
| st.markdown(st.session_state.generated_content) |
|
|
| if "question_index" not in st.session_state: |
| st.session_state.question_index = 0 |
|
|
| if "answers" not in st.session_state: |
| st.session_state.answers = [] |
|
|
| |
| if st.button("Completed"): |
| questions_data = get_quize(selected_topic) |
|
|
| |
| if "questions" in questions_data: |
| questions = questions_data["questions"] |
|
|
| |
| q_index = st.session_state.question_index |
| if q_index < len(questions): |
| current_question = questions[q_index] |
|
|
| try: |
| |
| question_text = current_question["question"] |
| options = current_question["options"] |
|
|
| |
| st.write(f"Question {q_index + 1}: {question_text}") |
| selected_option = st.radio("Options", options, key=f"q{q_index}") |
|
|
| |
| if st.button("Next"): |
| |
| st.session_state.answers.append(selected_option) |
|
|
| |
| if st.session_state.question_index < len(questions) - 1: |
| st.session_state.question_index += 1 |
| else: |
| st.write("Quiz completed!") |
| st.write("Your answers:", st.session_state.answers) |
|
|
| except (KeyError, TypeError) as e: |
| st.error("Error loading question. Please check question format.") |
| else: |
| st.write("Quiz completed!") |
| st.write("Your answers:", st.session_state.answers) |
| else: |
| st.error("Invalid question data format. Please ensure 'questions' key exists in response.") |
| elif selected_page == "Practice Coding": |
| with st.container(): |
| |
| with st.expander("Practice Coding", expanded=False): |
| |
| user_code = st.text_area("Enter your Python code here:", height=200) |
|
|
| |
| output = None |
| if st.button("Run Code"): |
| if user_code: |
| output = run_code_locally(user_code) |
| st.subheader("Output") |
| st.markdown(output) |
| else: |
| st.write("Please enter some code to run.") |
|
|
| |
| st.subheader("Chatbot Response") |
| user_query = st.chat_input("Enter your question here:") |
|
|
| if user_query: |
| |
| response = chatbot_response(user_query, topic="Python Practice", current_code=user_code, last_output=output) |
|
|
| st.markdown(response) |
|
|