Skip to content

Commit

Permalink
Separate file view from model
Browse files Browse the repository at this point in the history
  • Loading branch information
nicole-brewer committed Apr 4, 2024
1 parent 01d61d5 commit 9d920d8
Show file tree
Hide file tree
Showing 8 changed files with 513 additions and 86 deletions.
20 changes: 15 additions & 5 deletions jupyter_mentor/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
'jupyter_mentor/educator_course_overview.py'),
'jupyter_mentor.educator_course_overview.EducatorCourseOverview.__init__': ( 'educator_course_overview.html#educatorcourseoverview.__init__',
'jupyter_mentor/educator_course_overview.py')},
'jupyter_mentor.file_viewer': { 'jupyter_mentor.file_viewer.FileModel': ( 'file_viewer.html#filemodel',
'jupyter_mentor/file_viewer.py'),
'jupyter_mentor.file_viewer.FileModel.save_content_from_upload': ( 'file_viewer.html#filemodel.save_content_from_upload',
'jupyter_mentor/file_viewer.py'),
'jupyter_mentor.file_viewer.FileViewer': ( 'file_viewer.html#fileviewer',
'jupyter_mentor.educator_profile': { 'jupyter_mentor.educator_profile.EducatorProfileView': ( 'educator_profile.html#educatorprofileview',
'jupyter_mentor/educator_profile.py'),
'jupyter_mentor.educator_profile.EducatorProfileView.__init__': ( 'educator_profile.html#educatorprofileview.__init__',
'jupyter_mentor/educator_profile.py')},
'jupyter_mentor.file_viewer': { 'jupyter_mentor.file_viewer.FileViewer': ( 'file_viewer.html#fileviewer',
'jupyter_mentor/file_viewer.py'),
'jupyter_mentor.file_viewer.FileViewer.__init__': ( 'file_viewer.html#fileviewer.__init__',
'jupyter_mentor/file_viewer.py'),
Expand All @@ -48,6 +48,16 @@
'jupyter_mentor/file_viewer.py'),
'jupyter_mentor.file_viewer.FileViewerView.__init__': ( 'file_viewer.html#fileviewerview.__init__',
'jupyter_mentor/file_viewer.py')},
'jupyter_mentor.llm': { 'jupyter_mentor.llm.FileModel': ('llm.html#filemodel', 'jupyter_mentor/llm.py'),
'jupyter_mentor.llm.FileModel.__init__': ('llm.html#filemodel.__init__', 'jupyter_mentor/llm.py'),
'jupyter_mentor.llm.FileModel.load_markdown': ( 'llm.html#filemodel.load_markdown',
'jupyter_mentor/llm.py'),
'jupyter_mentor.llm.FileModel.load_pdf': ('llm.html#filemodel.load_pdf', 'jupyter_mentor/llm.py'),
'jupyter_mentor.llm.FileModel.load_text': ('llm.html#filemodel.load_text', 'jupyter_mentor/llm.py'),
'jupyter_mentor.llm.FileModel.save_content_from_upload': ( 'llm.html#filemodel.save_content_from_upload',
'jupyter_mentor/llm.py'),
'jupyter_mentor.llm.LLM': ('llm.html#llm', 'jupyter_mentor/llm.py'),
'jupyter_mentor.llm.LLM.__init__': ('llm.html#llm.__init__', 'jupyter_mentor/llm.py')},
'jupyter_mentor.login': { 'jupyter_mentor.login.Login': ('login.html#login', 'jupyter_mentor/login.py'),
'jupyter_mentor.login.Login.__init__': ('login.html#login.__init__', 'jupyter_mentor/login.py')},
'jupyter_mentor.madlib': { 'jupyter_mentor.madlib.MadLib': ('madlib.html#madlib', 'jupyter_mentor/madlib.py'),
Expand Down
36 changes: 36 additions & 0 deletions jupyter_mentor/educator_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/04_educator_profile.ipynb.

# %% auto 0
__all__ = ['EducatorProfileView']

# %% ../nbs/04_educator_profile.ipynb 1
from ipywidgets import VBox, HBox, HTML, Text, Button, Label

# %% ../nbs/04_educator_profile.ipynb 2
class EducatorProfileView(VBox):

def __init__(self):
super().__init__()

# Username input
self.username_label = Label('Name:')
self.username_input = Text(placeholder='Enter your name')

# Password input
self.key_label = Label('Educator:')
self.key_input = Text(placeholder='Enter Your Level', password=True)

# Next button
self.next_button = Button(description='Next')

# Arrange labels and inputs horizontally
self.username_box = HBox([self.username_label, self.username_input])
self.key_box = HBox([self.key_label, self.key_input])

# Arrange widgets vertically
self.children = [
HTML('<h2>User Profile</h2>'), # Heading
self.username_box, # Username label and input box
self.key_box, # Password label and input box
HBox([self.next_button], layout={'justify_content': 'flex-end'}), # Login button aligned to the right
]
20 changes: 8 additions & 12 deletions jupyter_mentor/file_viewer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03_file_viewer.ipynb.

# %% auto 0
__all__ = ['FileViewerView', 'FileModel', 'FileViewer']
__all__ = ['FileViewerView', 'FileViewer']

# %% ../nbs/03_file_viewer.ipynb 1
import ipywidgets as widgets
Expand All @@ -10,13 +10,19 @@
import ipyvuetify as v
from traitlets import observe, HasTraits, Unicode
from ipyfilechooser import FileChooser
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from .llm import LLM, FileModel

# %% ../nbs/03_file_viewer.ipynb 2
class FileViewerView(Accordion):

def __init__(self):
super().__init__()

# TODO: put file upload /viewer (with pandasdf or mercury, etc) in separate class (FileUploader)
# FileDownloader: ipyfilechooser and download button

self.file_upload = widgets.FileUpload(
accept='', # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'
Expand All @@ -38,17 +44,7 @@ def __init__(self):

self.children = (self.vbox, )

# %% ../nbs/03_file_viewer.ipynb 8
class FileModel(HasTraits):
# Define a Unicode string trait
select = Unicode()

def save_content_from_upload(values):
for value in values:
with open(value['name'], "wb") as fp:
fp.write(value['content'])

# %% ../nbs/03_file_viewer.ipynb 10
# %% ../nbs/03_file_viewer.ipynb 6
class FileViewer(FileViewerView):

def __init__(self, model):
Expand Down
64 changes: 64 additions & 0 deletions jupyter_mentor/llm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_llm.ipynb.

# %% auto 0
__all__ = ['LLM', 'FileModel']

# %% ../nbs/01_llm.ipynb 1
from traitlets import HasTraits, Unicode
from langchain_openai import ChatOpenAI
import os
from langchain.docstore.document import Document
from langchain_community.document_loaders import UnstructuredMarkdownLoader
from langchain_openai import OpenAIEmbeddings
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings

# %% ../nbs/01_llm.ipynb 2
class LLM(HasTraits):

def __init__(self, filepath='OPENAI_API_KEY'):
super().__init__()

with open(filepath, 'r') as file:
openai_api_key = file.read().strip()
os.environ['OPENAI_API_KEY'] = openai_api_key
self.llm = ChatOpenAI(model_name="gpt-3.5-turbo")

# %% ../nbs/01_llm.ipynb 4
class FileModel(LLM):
# Define a Unicode string trait
select = Unicode()

def __init__(self):
super().__init__()
#self.embeddings = OpenAIEmbeddings()
#doc = Document(page_content='Course')
#self.db = FAISS.from_documents(doc, self.embeddings)

def save_content_from_upload(values):
for value in values:
with open('course_files/' + value['name'], "wb") as fp:
fp.write(value['content'])

def load_text(self, text):
doc = Document(page_content=text)
db = FAISS.from_documents(doc, self.embeddings)
self.db.merge_from(db)

def load_pdf(self, filepath):
loader = PyPDFLoader(filepath)
pages = loader.load_and_split()
db = FAISS.from_documents(pages, self.embeddings)
self.db.merge_from(db)

def load_markdown(filepath):
loader = UnstructuredMarkdownLoader(filepath, mode="elements") #mode=elements breaks up the text into chunks
doc = loader.load()
db = FAISS.from_documents(doc, embeddings)
self.db.merge_from(db)

def save_content_from_upload(values):
for value in values:
with open(value['name'], "wb") as fp:
fp.write(value['content'])
46 changes: 39 additions & 7 deletions nbs/00_main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
"import ipywidgets as widgets\n",
"from ipywidgets import Tab, Output, Button\n",
"from jupyter_mentor.login import Login\n",
"from jupyter_mentor.educator_profile import EducatorProfile\n",
"from jupyter_mentor.educator_profile import EducatorProfileView\n",
"from jupyter_mentor.student_profile import StudentProfile\n",
"from jupyter_mentor.educator_course_overview import EducatorCourseOverview\n",
"from jupyter_mentor.student_course_overview import StudentCourseOverview\n",
"from jupyter_mentor.chatbot import StudentChatBot, EducatorChatBot, ChatBotModel\n",
"from jupyter_mentor.file_viewer import FileViewer"
"from jupyter_mentor.file_viewer import FileViewerView"
]
},
{
Expand Down Expand Up @@ -91,8 +91,8 @@
" \n",
" # initialize views\n",
" self.first = Login()\n",
" self.second = EducatorProfile()\n",
" self.file_viewer = FileViewer()\n",
" self.second = EducatorProfileView()\n",
" self.file_viewer = FileViewerView()\n",
" self.third = EducatorCourseOverview(self.file_viewer)\n",
" self.fourth = EducatorChatBot(self.educator_chatbot_model)\n",
" \n",
Expand Down Expand Up @@ -126,7 +126,23 @@
"execution_count": null,
"id": "9ca28623-ed1f-4f1c-b671-462ee582040f",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "25b9cc1a7b794da19f64ac0d5a0d2d57",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"EducatorMain(children=(Login(children=(HTML(value='<h2>Login</h2>'), HBox(children=(Label(value='Username:'), …"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"educator_main = EducatorMain(llm)\n",
"educator_main"
Expand All @@ -153,7 +169,7 @@
" # initialize views\n",
" self.first = Login()\n",
" self.second = StudentProfile()\n",
" self.file_viewer = FileViewer()\n",
" self.file_viewer = FileViewerView()\n",
" self.third = StudentCourseOverview(self.file_viewer)\n",
" self.fourth = StudentChatBot(self.student_chatbot_model)\n",
"\n",
Expand Down Expand Up @@ -187,7 +203,23 @@
"execution_count": null,
"id": "36cabf8d-427b-4d24-9c5d-8a6f8e4ab6b5",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ae14a13795ac4ffb9105d9c90b859bca",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"StudentMain(children=(Login(children=(HTML(value='<h2>Login</h2>'), HBox(children=(Label(value='Username:'), T…"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"student_main = StudentMain()\n",
"student_main"
Expand Down
Loading

0 comments on commit 9d920d8

Please sign in to comment.