Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,230 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import re
|
| 3 |
-
from components.session_manager import initialize_session_state, clear_session, save_current_to_history, get_or_create_user_id
|
| 4 |
-
from components.ui_components import render_header, render_sidebar
|
| 5 |
-
from components.student_flow import render_student_flow
|
| 6 |
-
from components.tutor_flow import render_tutor_flow
|
| 7 |
-
from components.output_renderer import render_output_section
|
| 8 |
-
from components.feedback_handler import render_feedback_section
|
| 9 |
-
from components.export_handler import render_export_section
|
| 10 |
-
from components.history_page import render_history_page
|
| 11 |
-
|
| 12 |
-
import base64
|
| 13 |
-
|
| 14 |
-
# Find where the validation error is coming from
|
| 15 |
-
original_b64decode = base64.b64decode
|
| 16 |
-
|
| 17 |
-
def debug_b64decode(data, *args, **kwargs):
|
| 18 |
-
try:
|
| 19 |
-
return original_b64decode(data, *args, **kwargs)
|
| 20 |
-
except Exception as e:
|
| 21 |
-
print(f"π¨ BASE64 DECODE ERROR: {e}")
|
| 22 |
-
print(f"π¨ Data type: {type(data)}")
|
| 23 |
-
print(f"π¨ Data length: {len(data) if data else 0}")
|
| 24 |
-
if data and isinstance(data, str):
|
| 25 |
-
print(f"π¨ Data preview: {data[:100]}...")
|
| 26 |
-
import traceback
|
| 27 |
-
traceback.print_stack()
|
| 28 |
-
raise
|
| 29 |
-
|
| 30 |
-
base64.b64decode = debug_b64decode
|
| 31 |
-
|
| 32 |
-
# Streamlit App Configuration
|
| 33 |
-
st.set_page_config(page_title="TailorED", layout="wide")
|
| 34 |
-
|
| 35 |
-
def scroll_to_top():
|
| 36 |
-
"""Force scroll to top of page"""
|
| 37 |
-
st.components.v1.html("""
|
| 38 |
-
<script>
|
| 39 |
-
window.scrollTo(0, 0);
|
| 40 |
-
setTimeout(() => window.scrollTo(0, 0), 100);
|
| 41 |
-
setTimeout(() => window.scrollTo({top: 0, behavior: 'smooth'}), 200);
|
| 42 |
-
</script>
|
| 43 |
-
""", height=0)
|
| 44 |
-
|
| 45 |
-
def main():
|
| 46 |
-
|
| 47 |
-
try:
|
| 48 |
-
from db.connection import init_db
|
| 49 |
-
init_db()
|
| 50 |
-
except Exception as e:
|
| 51 |
-
st.error(f"β Database initialization failed: {e}")
|
| 52 |
-
st.stop()
|
| 53 |
-
|
| 54 |
-
# Initialize session state
|
| 55 |
-
initialize_session_state()
|
| 56 |
-
|
| 57 |
-
# Ensure user ID is stored in session
|
| 58 |
-
if "user_id" not in st.session_state:
|
| 59 |
-
st.session_state.user_id = get_or_create_user_id()
|
| 60 |
-
|
| 61 |
-
# Create a scroll anchor at the top
|
| 62 |
-
scroll_anchor = st.empty()
|
| 63 |
-
|
| 64 |
-
# Render header with navigation
|
| 65 |
-
render_header_with_nav()
|
| 66 |
-
|
| 67 |
-
# Render sidebar
|
| 68 |
-
render_sidebar()
|
| 69 |
-
|
| 70 |
-
# Handle model regeneration if needed
|
| 71 |
-
if st.session_state.get("regenerate_with_new_model"):
|
| 72 |
-
handle_regeneration()
|
| 73 |
-
|
| 74 |
-
# Main application logic based on current page
|
| 75 |
-
handle_page_navigation()
|
| 76 |
-
|
| 77 |
-
# Session management
|
| 78 |
-
handle_session_management()
|
| 79 |
-
|
| 80 |
-
# Force scroll to top after content generation
|
| 81 |
-
if st.session_state.get("generated_output") and not st.session_state.get("scrolled_to_top", False):
|
| 82 |
-
scroll_to_top()
|
| 83 |
-
st.session_state.scrolled_to_top = True
|
| 84 |
-
|
| 85 |
-
def render_header_with_nav():
|
| 86 |
-
st.title("π§ TailorED - AI-Powered Educational Content Generator")
|
| 87 |
-
|
| 88 |
-
col1, col2, col3, col4 = st.columns([2, 1, 1, 1])
|
| 89 |
-
|
| 90 |
-
with col1:
|
| 91 |
-
st.caption("Create, manage, and access your educational content")
|
| 92 |
-
|
| 93 |
-
with col2:
|
| 94 |
-
if st.button("π New Content", use_container_width=True, key="new_content_btn"):
|
| 95 |
-
st.session_state.current_page = "generator"
|
| 96 |
-
clear_session()
|
| 97 |
-
st.rerun()
|
| 98 |
-
|
| 99 |
-
with col3:
|
| 100 |
-
if st.button("π History", use_container_width=True, key="history_btn"):
|
| 101 |
-
st.session_state.current_page = "history"
|
| 102 |
-
# RELOAD HISTORY WHEN NAVIGATING TO HISTORY PAGE
|
| 103 |
-
from components.session_manager import load_user_history_from_db
|
| 104 |
-
load_user_history_from_db()
|
| 105 |
-
st.rerun()
|
| 106 |
-
|
| 107 |
-
with col4:
|
| 108 |
-
if st.button("π¬ Research", use_container_width=True, key="research_btn"):
|
| 109 |
-
st.session_state.current_page = "research"
|
| 110 |
-
st.rerun()
|
| 111 |
-
|
| 112 |
-
def handle_regeneration():
|
| 113 |
-
"""Handle model regeneration when user switches models"""
|
| 114 |
-
if st.session_state.get("regenerate_with_new_model"):
|
| 115 |
-
# Clear the flag first to prevent loops
|
| 116 |
-
st.session_state.regenerate_with_new_model = False
|
| 117 |
-
|
| 118 |
-
# Show regeneration in progress
|
| 119 |
-
regeneration_status = st.empty()
|
| 120 |
-
regeneration_status.info("π Regenerating content with new model...")
|
| 121 |
-
|
| 122 |
-
# Get the preserved context
|
| 123 |
-
user_type = st.session_state.user_type
|
| 124 |
-
student_level = st.session_state.student_level
|
| 125 |
-
|
| 126 |
-
# Trigger regeneration based on user type
|
| 127 |
-
if user_type == "student":
|
| 128 |
-
from components.student_flow import generate_student_content
|
| 129 |
-
content_text = st.session_state.get("original_content_text", "")
|
| 130 |
-
if content_text:
|
| 131 |
-
generate_student_content(content_text, student_level, "", "regenerated_content.pdf")
|
| 132 |
-
else:
|
| 133 |
-
from components.tutor_flow import generate_tutor_content
|
| 134 |
-
topic = st.session_state.get("original_topic", "")
|
| 135 |
-
objectives = st.session_state.get("original_objectives", "")
|
| 136 |
-
content_type = st.session_state.get("tutor_content_type", "Comprehensive Explanation")
|
| 137 |
-
if topic and objectives:
|
| 138 |
-
generate_tutor_content(topic, objectives, student_level, content_type, "")
|
| 139 |
-
|
| 140 |
-
regeneration_status.empty()
|
| 141 |
-
|
| 142 |
-
def handle_page_navigation():
|
| 143 |
-
current_page = st.session_state.get("current_page", "generator")
|
| 144 |
-
|
| 145 |
-
if current_page == "history":
|
| 146 |
-
# ENSURE HISTORY IS LOADED BEFORE RENDERING
|
| 147 |
-
from components.session_manager import load_user_history_from_db
|
| 148 |
-
load_user_history_from_db()
|
| 149 |
-
render_history_page()
|
| 150 |
-
elif current_page == "research":
|
| 151 |
-
try:
|
| 152 |
-
from components.research_dashboard import render_research_dashboard
|
| 153 |
-
render_research_dashboard()
|
| 154 |
-
except ImportError as e:
|
| 155 |
-
st.error("π¬ Research Dashboard - Import Error")
|
| 156 |
-
st.code(f"Error: {str(e)}")
|
| 157 |
-
st.info("""
|
| 158 |
-
**To fix this:**
|
| 159 |
-
1. Make sure `components/research_dashboard.py` exists
|
| 160 |
-
2. Check the file has no syntax errors
|
| 161 |
-
3. Restart the Streamlit app
|
| 162 |
-
""")
|
| 163 |
-
except Exception as e:
|
| 164 |
-
st.error("π¬ Research Dashboard - Runtime Error")
|
| 165 |
-
st.code(f"Error: {str(e)}")
|
| 166 |
-
st.info("The research dashboard encountered an error while running.")
|
| 167 |
-
else:
|
| 168 |
-
handle_generator_flow()
|
| 169 |
-
|
| 170 |
-
def handle_generator_flow():
|
| 171 |
-
# DEBUG: Check what's in session state
|
| 172 |
-
print(f"π DEBUG handle_generator_flow:")
|
| 173 |
-
print(f" - generated_output: {bool(st.session_state.get('generated_output'))}")
|
| 174 |
-
print(f" - regenerated: {st.session_state.get('regenerated', False)}")
|
| 175 |
-
print(f" - feedback_given: {st.session_state.get('feedback_given', False)}")
|
| 176 |
-
print(f" - pending_regeneration: {st.session_state.get('pending_regeneration', False)}")
|
| 177 |
-
|
| 178 |
-
# Handle pending regeneration FIRST in the generator flow
|
| 179 |
-
if st.session_state.get('pending_regeneration'):
|
| 180 |
-
print("π DEBUG: Handling pending regeneration in generator flow")
|
| 181 |
-
from components.feedback_handler import handle_pending_regeneration
|
| 182 |
-
handle_pending_regeneration()
|
| 183 |
-
|
| 184 |
-
# Check if we have content to display - REGARDLESS of regeneration status
|
| 185 |
-
if st.session_state.get("generated_output"):
|
| 186 |
-
print("β
DEBUG: Rendering content sections")
|
| 187 |
-
render_output_section()
|
| 188 |
-
render_export_section()
|
| 189 |
-
render_feedback_section()
|
| 190 |
-
return
|
| 191 |
-
|
| 192 |
-
# If no content, check if we have a user type selected
|
| 193 |
-
if not st.session_state.user_type:
|
| 194 |
-
render_user_selection()
|
| 195 |
-
return
|
| 196 |
-
|
| 197 |
-
# If user type is selected but no content, render the appropriate flow
|
| 198 |
-
if st.session_state.user_type == "student":
|
| 199 |
-
render_student_flow()
|
| 200 |
-
else:
|
| 201 |
-
render_tutor_flow()
|
| 202 |
-
|
| 203 |
-
def render_user_selection():
|
| 204 |
-
st.header("π― Welcome to TailorED!")
|
| 205 |
-
st.subheader("Are you a Student or Tutor?")
|
| 206 |
-
|
| 207 |
-
col1, col2 = st.columns(2)
|
| 208 |
-
|
| 209 |
-
with col1:
|
| 210 |
-
if st.button("π I'm a Student", use_container_width=True, key="student_btn"):
|
| 211 |
-
st.session_state.user_type = "student"
|
| 212 |
-
st.session_state.scrolled_to_top = False
|
| 213 |
-
st.rerun()
|
| 214 |
-
|
| 215 |
-
with col2:
|
| 216 |
-
if st.button("π¨βπ« I'm a Tutor", use_container_width=True, key="tutor_btn"):
|
| 217 |
-
st.session_state.user_type = "tutor"
|
| 218 |
-
st.session_state.scrolled_to_top = False
|
| 219 |
-
st.rerun()
|
| 220 |
-
|
| 221 |
-
def handle_session_management():
|
| 222 |
-
# Only show start over if we have content
|
| 223 |
-
if (st.session_state.get("current_page") == "generator" and
|
| 224 |
-
st.session_state.get("generated_output") and
|
| 225 |
-
st.button("π Start Over", key="start_over_btn")):
|
| 226 |
-
clear_session()
|
| 227 |
-
st.rerun()
|
| 228 |
-
|
| 229 |
-
if __name__ == "__main__":
|
| 230 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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