AI-Tutor / app.py
vthamaraikannan1@gmail.com
changes in utility file
127e883
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 structure
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!")
# Introduction and Overview
st.write("In this course, we’ll cover the fundamentals of Python programming. Here’s an outline of the topics you will learn:")
# Topic List
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)
# Store generated content in session state to prevent data loss on reload
if "generated_content" not in st.session_state:
st.session_state.generated_content = ""
# Generate new content on "Next Topic" button click and store it
if st.button("Next Topic"):
st.session_state.generated_content = generate_content(selected_topic)
# Display stored content
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 = [] # Initialize empty list to store answers
# Fetch questions when "Completed" button is pressed
if st.button("Completed"):
questions_data = get_quize(selected_topic) # Ensure `get_quize` returns a dictionary with a "questions" key
# Validate that `questions_data` contains the expected structure
if "questions" in questions_data:
questions = questions_data["questions"]
# Ensure question_index does not exceed the list length
q_index = st.session_state.question_index
if q_index < len(questions):
current_question = questions[q_index]
try:
# Extract question text and options
question_text = current_question["question"]
options = current_question["options"]
# Display question and options
st.write(f"Question {q_index + 1}: {question_text}")
selected_option = st.radio("Options", options, key=f"q{q_index}")
# Save the selected answer on "Next" button click
if st.button("Next"):
# Save the selected answer in session state
st.session_state.answers.append(selected_option)
# Increment the question index if there are more questions
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():
# Coding Practice Section with Expander (Drawer)
with st.expander("Practice Coding", expanded=False):
# Text area for user code input
user_code = st.text_area("Enter your Python code here:", height=200)
# Button to run the code locally
output = None
if st.button("Run Code"):
if user_code:
output = run_code_locally(user_code) # Run code locally
st.subheader("Output")
st.markdown(output)
else:
st.write("Please enter some code to run.")
# Chatbot Section with Expander (Drawer)
st.subheader("Chatbot Response")
user_query = st.chat_input("Enter your question here:")
if user_query:
# Pass current code and output as context to the chatbot function
response = chatbot_response(user_query, topic="Python Practice", current_code=user_code, last_output=output)
st.markdown(response)