Owen Wang commited on
Commit
70a1a09
·
1 Parent(s): 12b9fc6

add users interests

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -5,7 +5,6 @@ import openai
5
  import pinecone
6
  import json
7
  import re
8
- import os
9
 
10
  PINECONE_API_KEY = st.secrets["PINECONE_API_KEY"]
11
  # Set OpenAI API key from Streamlit Secrets
@@ -19,6 +18,7 @@ class Metadata(TypedDict):
19
  title: str
20
  description: str
21
  slides: str
 
22
 
23
  # Initialize Pinecone and OpenAI
24
  pinecone.init(api_key=PINECONE_API_KEY, environment="asia-southeast1-gcp")
@@ -46,13 +46,16 @@ def get_embeddings(texts: List[str]) -> List[List[float]]:
46
  # Return the embeddings as a list of lists of floats
47
  return [result["embedding"] for result in data]
48
 
 
 
 
 
 
49
  # Pinecone fetch function
50
  def fetch_lesson(query: str):
51
  vector = get_embeddings([query])[0]
52
 
53
- index_name = "prequelworkshops"
54
- index = pinecone.Index(index_name)
55
- return index.query(
56
  vector=vector,
57
  # filter={
58
  # "genre": {"$eq": "documentary"},
@@ -99,32 +102,38 @@ def extract_arrays(s: str) -> Optional[List[str]]:
99
  else:
100
  return None
101
 
102
- def generate_curriculum(user_input) -> Optional[List[str]]:
103
- prompt = f"You are a world class high school teacher. Create a curriculum for a course based on the student's interest below. Output the curriculum as a javascript array of strings, where each string is a description of the lesson. The output should just be the array and nothing else. Student's interest: {user_input}"
104
  response = query_openai(prompt)
105
  return extract_arrays(response)
106
 
107
- def get_metadata(lesson) -> Metadata:
108
- return lesson.matches[0].metadata
 
 
109
 
110
- def format_metadata(metadata) -> List[str]:
111
- return f"Title: {metadata['title']}\n\nDescription: {metadata['description']}"
 
 
112
 
113
  # Streamlit UI
114
  st.set_page_config(layout="centered")
115
  st.title("Personalized Learning Curriculum Generator")
116
- user_input = st.text_area("Enter what you want to learn:", height=200)
 
117
  submit_button = st.button("Generate curriculum")
118
  status = st.empty()
119
 
120
  if submit_button:
121
  status.text("Generating curriculum...")
122
- curriculum = generate_curriculum(user_input)
123
 
124
  if curriculum is not None:
125
- status.text("Fetching relevant courses...")
126
  lessons = [fetch_lesson(lesson) for lesson in curriculum]
127
- lesson_text = "\n\n".join([format_metadata(get_metadata(lesson)) for lesson in lessons])
 
128
  status.empty()
129
  st.markdown(f"**Generated Curriculum:**\n\n{lesson_text}")
130
  else:
 
5
  import pinecone
6
  import json
7
  import re
 
8
 
9
  PINECONE_API_KEY = st.secrets["PINECONE_API_KEY"]
10
  # Set OpenAI API key from Streamlit Secrets
 
18
  title: str
19
  description: str
20
  slides: str
21
+ outcome: str
22
 
23
  # Initialize Pinecone and OpenAI
24
  pinecone.init(api_key=PINECONE_API_KEY, environment="asia-southeast1-gcp")
 
46
  # Return the embeddings as a list of lists of floats
47
  return [result["embedding"] for result in data]
48
 
49
+ @st.cache_resource
50
+ def load_pinecone_index():
51
+ index_name = "prequelworkshops"
52
+ return pinecone.Index(index_name)
53
+
54
  # Pinecone fetch function
55
  def fetch_lesson(query: str):
56
  vector = get_embeddings([query])[0]
57
 
58
+ return load_pinecone_index().query(
 
 
59
  vector=vector,
60
  # filter={
61
  # "genre": {"$eq": "documentary"},
 
102
  else:
103
  return None
104
 
105
+ def generate_curriculum(skills: str) -> Optional[List[str]]:
106
+ prompt = f"You are a world-class middle and high school educator who develops project-based entrepreneurship curriculum catered to student interests. Create a curriculum of up to 5 lessons for a course based on the student's target skills to learn. Output the curriculum as a javascript array of strings, where each string is a description of the lesson. The output should just be the array and nothing else. Student's target skills: {skills}"
107
  response = query_openai(prompt)
108
  return extract_arrays(response)
109
 
110
+ def generate_application(metadata: Metadata, interests: str) -> str:
111
+ prompt = f"You are a world-class middle and high school educator who develops project-based entrepreneurship curriculum catered to student interests. You've created a lesson called \"{metadata['title']}\". The description of the lesson is \"{metadata['description']}\". The expected learning outcome is \"{metadata['outcome']}\". Explain how this lesson can be applied to the student's interests in one sentence. Student's interests: {interests}"
112
+ response = query_openai(prompt)
113
+ return response
114
 
115
+ def format_lesson(lesson, interests: str) -> List[str]:
116
+ metadata = lesson.matches[0].metadata
117
+ application = generate_application(metadata, interests)
118
+ return f"Title: [{metadata['title']}]({metadata['slides']})\n\nDescription: {metadata['description']}\n\n{application}"
119
 
120
  # Streamlit UI
121
  st.set_page_config(layout="centered")
122
  st.title("Personalized Learning Curriculum Generator")
123
+ skills = st.text_area("What skills would you like to learn", height=200)
124
+ interests = st.text_area("What are you interested in? We'll try our best to build a curriculum that could apply to your interests", height=200)
125
  submit_button = st.button("Generate curriculum")
126
  status = st.empty()
127
 
128
  if submit_button:
129
  status.text("Generating curriculum...")
130
+ curriculum = generate_curriculum(skills)
131
 
132
  if curriculum is not None:
133
+ status.text("Fetching relevant lessons...")
134
  lessons = [fetch_lesson(lesson) for lesson in curriculum]
135
+ status.text("Characterizing relevance to your interests...")
136
+ lesson_text = "\n\n".join([format_lesson(lesson, interests) for lesson in lessons])
137
  status.empty()
138
  st.markdown(f"**Generated Curriculum:**\n\n{lesson_text}")
139
  else: