import gradio as gr import os # Keep os import in case it's used elsewhere, though not for Streamlit env vars now # Import your existing modules from modules.wiki_fetcher import fetch_topic_summary, fetch_wikibooks_links, fetch_wikidata_facts, fetch_wikiversity_resources from modules.ai_engine import mentor_chat_response, generate_flashcards # --- Gradio Theme and Global Styling --- # Gradio handles light/dark mode automatically with its themes. # We'll use a theme that generally looks good. You can experiment with others like # gr.themes.Default(), gr.themes.Monochrome(), gr.themes.Glass() theme = gr.themes.Soft() # Custom CSS for the flashcards to mimic the Streamlit look # Gradio's theming will handle the overall app background/text colors. custom_css = """ /* Basic styling for the banner */ .banner { text-align: center; padding: 20px; background-color: var(--background-fill-primary); /* Gradio's primary background color */ color: var(--text-color-body); /* Gradio's body text color */ border-radius: var(--radius-xl); margin-bottom: 20px; } .banner h1 { font-size: 2.5em; margin-bottom: 0.2em; color: var(--text-color-body); } .banner p { font-size: 1.1em; color: var(--text-color-subdued); } /* Styling for flashcards, adapting to Gradio's theme variables */ .flashcard { background-color: var(--background-fill-secondary); /* Gradio's secondary background color */ padding: 1em; margin-bottom: 1em; border-radius: var(--radius-xl); border: 1px solid var(--border-color-primary); box-shadow: var(--shadow-drop-lg); } .flashcard h4 { color: var(--text-color-body); margin-top: 0; margin-bottom: 0.5em; } .flashcard details { margin-top: 0.5em; } .flashcard summary { color: var(--text-color-subdued); cursor: pointer; font-weight: bold; } .flashcard p { color: var(--text-color-subdued); padding-top: 0.5em; margin-bottom: 0; } """ # --- Functions to handle logic for Gradio UI interactions --- def learn_topic_action(topic): """ Handles the logic for the 'Learn a Topic' tab. Fetches summary, links, facts, resources, and generates flashcards. Returns multiple outputs for Gradio components. """ summary_output = "" books_output = "" facts_output = "" resources_output = "" flashcard_status = "" flashcards_data = [] # Data to be stored in session state if not topic: # Return empty strings for outputs and a message for status return "", "", "", "", "Please enter a topic to learn.", [] # Fetch summary # Gradio handles loading indicators automatically when functions are running summary = fetch_topic_summary(topic) if summary: summary_output = f"### 📖 Topic Summary\n\n{summary}" else: summary_output = "Couldn't find that topic. Try again with a simpler keyword." # If summary fails, no other data can be fetched, so return early return summary_output, "", "", "", "", [] # Fetch Wikibooks links books = fetch_wikibooks_links(topic) if books: books_output = "### 🔗 Wikibooks Links\n\n" + "\n".join([f"- 📘 [{title}]({url})" for title, url in books]) else: books_output = "No related books found on Wikibooks." # Fetch Wikidata facts facts = fetch_wikidata_facts(topic) if facts: facts_output = "### 🔬 Wikidata Facts\n\n" + "\n".join([f"- � {fact}" for fact in facts]) else: facts_output = "No structured facts found on Wikidata." # Fetch Wikiversity resources resources = fetch_wikiversity_resources(topic) if resources: resources_output = "### 🎓 Wikiversity Resources\n\n" + "\n".join([f"- 🎓 [{title}]({link})" for title, link in resources]) else: resources_output = "No learning resources found on Wikiversity." # Generate flashcards flashcards = generate_flashcards(summary) if flashcards: flashcards_data = flashcards # Store for session state flashcard_status = "✅ Flashcards generated! Check the Flashcard Review tab." else: flashcard_status = "⚠️ No flashcards were generated. Try a different topic." return summary_output, books_output, facts_output, resources_output, flashcard_status, flashcards_data def mentor_chat_action(user_query, chat_history): """ Handles the AI Mentor chat functionality. Takes user query and current chat history, generates a response, and updates history. """ if not user_query: # If query is empty, return current history without modification return chat_history, chat_history reply = mentor_chat_response(user_query) chat_history.append([user_query, reply]) # Gradio Chatbot expects list of [user_msg, bot_msg] return chat_history, chat_history # Return updated history for display and state def flashcard_review_display(flashcards_data): """ Generates HTML for displaying flashcards based on the session state data. """ if not flashcards_data: return "
Flashcards will appear here after learning a topic.
" flashcard_html = "" for idx, fc in enumerate(flashcards_data): # Apply inline styles for the flashcard to match the Streamlit look # These styles are derived from the custom_css block flashcard_html += f"""{fc['answer']}