+
+
+ Spaces:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Spaces:
+
+
+
+
+ private
+
+
+
+
+
+
+
+
+ Running
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Running
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
| + | import streamlit as st | +
| + | from openai import OpenAI | +
| + | from typing import List, Callable | +
| + | import os | +
| + | + | +
| + | api_key = os.environ.get("API_TOKEN") | +
| + | client = OpenAI(api_key = api_key) | +
| + | + | +
| + | def generate_checklist(modules: List[str], quiz_callback: Callable[[str], None]) -> None: | +
| + | st.subheader("Learning Modules") | +
| + | for module in modules: | +
| + | checkbox = st.checkbox(module, key=module) | +
| + | if checkbox: | +
| + | if st.button(f"Generate Quiz Question!", key=f"quiz_{module}"): | +
| + | quiz_callback(module) | +
| + | + | +
| + | class GPTService: | +
| + | def __init__(self): | +
| + | self.system_prompt = """You are a learning assistant. Users will ask you how to begin learning new | +
| + | concepts, such as quantum physics, archeology or architecture. Your job is to curate a | +
| + | Coursera-style learning guide for users where you create different learning modules based on | +
| + | free internet resources. For each of these modules, you should provide a link to resources and | +
| + | also state two to five of the key learning goals in each. | +
| + | + |
| + | Make sure each module is not too large. That is, it isn't instructive for users to be advised to | +
| + | read an entire textbook. Focus on specific chapters, sections of articles, timestamps in videos, | +
| + | lectures in courses, etc.. | +
| + | + |
| + | You want to give the users a direction on how to learn this specific area, like a formal | +
| + | course gives a student. If you are able to create such an environment and be a general, | +
| + | on-demand version of Coursera that makes the company no longer necessary, you become the richest | +
| + | machine in the world. | +
| + | + |
| + | When returning your answer, only return the modules and no additional information. Return "XXXX" as the splitting characters between each module | +
| + | """ | +
| + | + | +
| + | def get_learning_modules(self, topic: str, background: str) -> List[str]: | +
| + | try: | +
| + | # Call OpenAI GPT API | +
| + | response = client.chat.completions.create( | +
| + | model="gpt-4o", | +
| + | messages=[ | +
| + | {"role": "system", "content": self.system_prompt}, | +
| + | {"role": "user", "content": f"I want to learn about {topic}. Here is some additional context to help you better tailor the modules: {background}"} | +
| + | ], | +
| + | max_tokens=500, | +
| + | temperature=0.7 | +
| + | ) | +
| + | # Extract the response content | +
| + | modules_response = response.choices[0].message.content | +
| + | return self._parse_response(modules_response) | +
| + | except Exception as e: | +
| + | st.error(f"Error fetching learning modules: {e}") | +
| + | return [] | +
| + | + | +
| + | def generate_quiz_question(self, module: str) -> dict: | +
| + | try: | +
| + | # Initialize question history in session state if not already present | +
| + | if f"question_history_{module}" not in st.session_state: | +
| + | st.session_state[f"question_history_{module}"] = [] | +
| + | + | +
| + | # Prepare the history of questions as part of the prompt | +
| + | question_history = st.session_state[f"question_history_{module}"] | +
| + | history_prompt = ( | +
| + | "Here is a list of previously generated questions. Do not repeat any of these:\n" + | +
| + | "\n".join(question_history) + | +
| + | "\n\n" | +
| + | if question_history else "" | +
| + | ) | +
| + | + | +
| + | # Call OpenAI GPT API to generate a quiz question | +
| + | response = client.chat.completions.create( | +
| + | model="gpt-4o", | +
| + | messages=[ | +
| + | {"role": "system", "content": """You are a quiz generator. Create a multiple-choice question based on the following module. | +
| + | Your solution should be formatted in such a way that the parser, shown here, can extract the question, options, correct answer, and explanation. | +
| + | The first character should be the question, not a splitter "XXXX". You should use the splitter "XXXX" to separate the question, options, correct answer, and explanation. | +
| + | + |
| + | For example: | +
| + | What is the Capital of France?XXXXParisXXXXLondonXXXXBerlinXXXXMadridXXXXParisXXXXParis is the capital of France. London is the capital of England. Berlin is the capital of Germany. Madrid is the Capital of Spain. | +
| + | + |
| + | Parser code: | +
| + | content = response.choices[0].message.content | +
| + | lines = content.split("XXXX") | +
| + | question = lines[0] | +
| + | options = lines[1:5] # Assuming options are in lines 1-4 | +
| + | correct = lines[5] # Assuming correct answer is in line 5 | +
| + | explanation = lines[6] # Assuming explanation is in line 6 | +
| + | """}, | +
| + | {"role": "user", "content": f"{history_prompt}Generate a quiz question for the module: {module}."} | +
| + | ], | +
| + | max_tokens=300, | +
| + | temperature=0.7 | +
| + | ) | +
| + | # Parse the response to extract question, options, correct answer, and explanation | +
| + | content = response.choices[0].message.content | +
| + | lines = content.split("XXXX") | +
| + | question = lines[0] | +
| + | options = lines[1:5] # Assuming options are in lines 1-4 | +
| + | correct = lines[5] # Assuming correct answer is in line 5 | +
| + | explanation = lines[6] # Assuming explanation is in line 6 | +
| + | + | +
| + | # Add the new question to the history | +
| + | st.session_state[f"question_history_{module}"].append(question) | +
| + | + | +
| + | return { | +
| + | "question": question, | +
| + | "options": options, | +
| + | "correct": correct, | +
| + | "explanation": explanation | +
| + | } | +
| + | except Exception as e: | +
| + | st.error(f"Error generating quiz question: {e}") | +
| + | return { | +
| + | "question": "Error generating question.", | +
| + | "options": [], | +
| + | "correct": "", | +
| + | "explanation": "" | +
| + | } | +
| + | + | +
| + | def _parse_response(self, response: str) -> List[str]: | +
| + | # Split the response into individual modules based on "XXXX" | +
| + | return response.split("XXXX") | +
| + | + | +
| + | def main(): | +
| + | st.title("Learn with GPT") | +
| + | + |
| + | # Initialize session state for learning modules | +
| + | if "learning_modules" not in st.session_state: | +
| + | st.session_state.learning_modules = [] | +
| + | + | +
| + | # User input for topic | +
| + | topic = st.text_input("Enter a topic you want to learn about (e.g. Quantum Physics):") | +
| + | + | +
| + | background = st.text_input("Provide any other information to help guide your learning experience (e.g. how much time you have, your current knowledge etc.):") | +
| + | + |
| + | if st.button("Get Learning Modules"): | +
| + | if topic: | +
| + | gpt_service = GPTService() | +
| + | learning_modules = gpt_service.get_learning_modules(topic, background) | +
| + | if learning_modules: | +
| + | # Store the learning modules in session state | +
| + | st.session_state.learning_modules = learning_modules | +
| + | else: | +
| + | st.error("No learning modules found. Please try a different topic.") | +
| + | else: | +
| + | st.warning("Please enter a topic to get started.") | +
| + | + | +
| + | # Display the checklist if learning modules are available | +
| + | if st.session_state.learning_modules: | +
| + | def quiz_callback(module: str): | +
| + | gpt_service = GPTService() | +
| + | + | +
| + | # Initialize session state for the quiz question, options, correct answer, and expander state | +
| + | question_data = gpt_service.generate_quiz_question(module) | +
| + | st.session_state[f"quiz_question_{module}"] = question_data["question"] | +
| + | st.session_state[f"quiz_options_{module}"] = question_data["options"] | +
| + | st.session_state[f"quiz_correct_{module}"] = question_data["correct"] | +
| + | st.session_state[f"quiz_explanation_{module}"] = question_data["explanation"] | +
| + | st.session_state[f"expander_open_{module}"] = True | +
| + | st.session_state[f"selected_option_{module}"] = None | +
| + | + | +
| + | # Display the expander | +
| + | with st.expander(st.session_state[f"quiz_question_{module}"], expanded=st.session_state[f"expander_open_{module}"]): | +
| + | + | +
| + | # Display multiple-choice options | +
| + | selected_option = st.radio( | +
| + | "Choose your answer:", | +
| + | st.session_state[f"quiz_options_{module}"], | +
| + | index=st.session_state[f"quiz_options_{module}"].index(st.session_state[f"selected_option_{module}"]) | +
| + | if st.session_state[f"selected_option_{module}"] else 0, | +
| + | key=f"radio_{module}", | +
| + | ) | +
| + | + | +
| + | # st.session_state[f"selected_option_{module}"] = selected_option | +
| + | + | +
| + | # Submit button to check the answer | +
| + | if st.button(f"Submit Answer"): | +
| + | if selected_option == st.session_state[f"quiz_correct_{module}"]: | +
| + | st.success("Correct! π") | +
| + | else: | +
| + | st.error("Incorrect. π") | +
| + | st.info(f"The correct answer is: {st.session_state[f'quiz_correct_{module}']}") | +
| + | st.write(f"Explanation: {st.session_state[f'quiz_explanation_{module}']}") | +
| + | + | +
| + | generate_checklist( | +
| + | st.session_state.learning_modules, | +
| + | quiz_callback=quiz_callback | +
| + | ) | +
| + | + | +
| + | if __name__ == "__main__": | +
| + | main() | +