Skip to content

Commit

Permalink
madlibs and fileviewr
Browse files Browse the repository at this point in the history
  • Loading branch information
nicole-brewer committed Apr 4, 2024
1 parent d1bd8e4 commit 01d61d5
Show file tree
Hide file tree
Showing 9 changed files with 655 additions and 86 deletions.
27 changes: 21 additions & 6 deletions jupyter_mentor/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
'jupyter_mentor/chatbot.py'),
'jupyter_mentor.chatbot.ChatBotModel.update_bot_template': ( 'chatbot.html#chatbotmodel.update_bot_template',
'jupyter_mentor/chatbot.py'),
'jupyter_mentor.chatbot.ChatBotModel.update_human_template': ( 'chatbot.html#chatbotmodel.update_human_template',
'jupyter_mentor/chatbot.py'),
'jupyter_mentor.chatbot.ChatBotView': ('chatbot.html#chatbotview', 'jupyter_mentor/chatbot.py'),
'jupyter_mentor.chatbot.ChatBotView.__init__': ( 'chatbot.html#chatbotview.__init__',
'jupyter_mentor/chatbot.py'),
Expand All @@ -32,16 +34,29 @@
'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.educator_profile': { 'jupyter_mentor.educator_profile.EducatorProfile': ( 'educator_profile.html#educatorprofile',
'jupyter_mentor/educator_profile.py'),
'jupyter_mentor.educator_profile.EducatorProfile.__init__': ( 'educator_profile.html#educatorprofile.__init__',
'jupyter_mentor/educator_profile.py')},
'jupyter_mentor.file_viewer': { 'jupyter_mentor.file_viewer.FileViewer': ( 'file_viewer.html#fileviewer',
'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/file_viewer.py'),
'jupyter_mentor.file_viewer.FileViewer.__init__': ( 'file_viewer.html#fileviewer.__init__',
'jupyter_mentor/file_viewer.py')},
'jupyter_mentor/file_viewer.py'),
'jupyter_mentor.file_viewer.FileViewer.observe_file_select': ( 'file_viewer.html#fileviewer.observe_file_select',
'jupyter_mentor/file_viewer.py'),
'jupyter_mentor.file_viewer.FileViewerView': ( 'file_viewer.html#fileviewerview',
'jupyter_mentor/file_viewer.py'),
'jupyter_mentor.file_viewer.FileViewerView.__init__': ( 'file_viewer.html#fileviewerview.__init__',
'jupyter_mentor/file_viewer.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'),
'jupyter_mentor.madlib.MadLib.__init__': ('madlib.html#madlib.__init__', 'jupyter_mentor/madlib.py'),
'jupyter_mentor.madlib.MadLib.observe_submit_button': ( 'madlib.html#madlib.observe_submit_button',
'jupyter_mentor/madlib.py'),
'jupyter_mentor.madlib.MadLibModel': ('madlib.html#madlibmodel', 'jupyter_mentor/madlib.py'),
'jupyter_mentor.madlib.MadLibModel.__init__': ( 'madlib.html#madlibmodel.__init__',
'jupyter_mentor/madlib.py')},
'jupyter_mentor.student_course_overview': { 'jupyter_mentor.student_course_overview.StudentCourseOverview': ( 'student_course_overview.html#studentcourseoverview',
'jupyter_mentor/student_course_overview.py'),
'jupyter_mentor.student_course_overview.StudentCourseOverview.__init__': ( 'student_course_overview.html#studentcourseoverview.__init__',
Expand Down
33 changes: 19 additions & 14 deletions jupyter_mentor/chatbot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/05_chatbot.ipynb.
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/06_chatbot.ipynb.

# %% auto 0
__all__ = ['ChatBotModel', 'ChatBotView', 'ChatBot', 'StudentChatBot', 'EducatorChatBot']

# %% ../nbs/05_chatbot.ipynb 1
# %% ../nbs/06_chatbot.ipynb 1
import ipywidgets as widgets
import traitlets
from ipywidgets import Textarea, Text, Layout, HBox
from ipywidgets import Textarea, Text, Layout, HBox, Stack, Layout
from traitlets import HasTraits
import os
from langchain_openai import ChatOpenAI
Expand All @@ -18,27 +18,32 @@
)
from langchain_openai import ChatOpenAI

# %% ../nbs/05_chatbot.ipynb 3
# %% ../nbs/06_chatbot.ipynb 5
class ChatBotModel(HasTraits):

def __init__(self, llm, bot_template="You are playing the role of a tutor/educator"):
def __init__(self, llm, bot_template="You are playing the role of a tutor/educator", human_template="{input_text}"):
super().__init__()
self.llm = llm
self.human_template = "{input_text}"
self.human_template = human_template
self.human_message_prompt = HumanMessagePromptTemplate.from_template(self.human_template)
self.update_bot_template(bot_template)

def update_bot_template(self, bot_template):
self.bot_template = bot_template
self.bot_message_prompt = SystemMessagePromptTemplate.from_template(self.bot_template)
self.human_message_prompt = HumanMessagePromptTemplate.from_template(self.human_template)
self.chat_prompt = ChatPromptTemplate.from_messages([self.bot_message_prompt, self.human_message_prompt])

def update_human_template(self, human_template):
self.human_template = human_template
self.human_message_prompt = HumanMessagePromptTemplate.from_template(self.human_template)
self.chat_prompt = ChatPromptTemplate.from_messages([self.bot_message_prompt, self.human_message_prompt])

def prompt(self, input_text):
#prompt_val = prompt_template.invoke({"adjective": "funny", "content": "chickens"})
ret = self.llm.invoke(self.chat_prompt.format_prompt(input_text=input_text))
def prompt(self, kwargs):
ret = self.llm.invoke(self.chat_prompt.format_prompt(**kwargs))
#ret = self.llm.invoke(self.chat_prompt.format_prompt(input_text=input_text))
return ret.content

# %% ../nbs/05_chatbot.ipynb 5
# %% ../nbs/06_chatbot.ipynb 8
class ChatBotView(widgets.VBox):

def __init__(self):
Expand All @@ -65,7 +70,7 @@ def __init__(self):

self.children = (self.chat, self.user_input_and_submit)

# %% ../nbs/05_chatbot.ipynb 7
# %% ../nbs/06_chatbot.ipynb 10
class ChatBot(ChatBotView):

def __init__(self, model):
Expand All @@ -81,7 +86,7 @@ def on_click(self, change):
ret = self.model.prompt(self.user_input.value)
self.chat.value = self.chat.value + "CHATBOT: " + ret + '\n\n'

# %% ../nbs/05_chatbot.ipynb 9
# %% ../nbs/06_chatbot.ipynb 12
class StudentChatBot(widgets.VBox):

#user = traitlets.CUnicode()
Expand Down Expand Up @@ -122,7 +127,7 @@ def __init__(self, chatbot_model):
self.suggestion_buttons.children = (self.step_by_step, self.metaphor, self.hints, self.guided_questions)
self.children = (self.chat_bot, self.suggestion_buttons)

# %% ../nbs/05_chatbot.ipynb 11
# %% ../nbs/06_chatbot.ipynb 14
class EducatorChatBot(widgets.VBox):

#user = traitlets.CUnicode()
Expand Down
39 changes: 0 additions & 39 deletions jupyter_mentor/educator_profile.py

This file was deleted.

54 changes: 45 additions & 9 deletions jupyter_mentor/file_viewer.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,62 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03_file_viewer.ipynb.

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

# %% ../nbs/03_file_viewer.ipynb 1
import ipywidgets as widgets
from ipywidgets import VBox, HBox, HTML, Button, Label, Text, Checkbox, Accordion, FileUpload
from IPython.display import display, clear_output
import ipyvuetify as v
from traitlets import observe
from traitlets import observe, HasTraits, Unicode
from ipyfilechooser import FileChooser

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

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


self.file_upload = widgets.FileUpload(
accept='', # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'
multiple=False # True to accept multiple files upload else False
)
self.file_browser = FileChooser()
self.file_browser.sandbox_path = '.'
self.file_browser.filter_pattern = ['*.pdf', '*.md']
self.file_browser.title = '<b>File Selector</b>' # Change the title (use '' to hide)

# TODO: file_download

# File viewer (hidden in accordion)
self.file_viewer_accordion = Accordion(children=[HTML('File viewer content')])
self.file_viewer_accordion.selected_index = None # Hide the accordion content initially
self.selected_index = None # Hide the accordion content initially
self.titles = ('Course Files',) # this isn't working for some reason

self.vbox = VBox()
self.vbox.children = (self.file_upload, self.file_browser) # File viewer (hidden initially)

self.children = (self.vbox, )

# Arrange widgets vertically
self.children = [
self.file_viewer_accordion, # File viewer (hidden initially)
]
# %% ../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
class FileViewer(FileViewerView):

def __init__(self, model):
super().__init__()
self.model = model
self.file_browser.register_callback(self.observe_file_select)

def observe_file_select(self, file_browser):
# update download button with correct filepath
# filepath stored in self.file_browser.selected
pass
46 changes: 46 additions & 0 deletions jupyter_mentor/madlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/05_madlib.ipynb.

# %% auto 0
__all__ = ['MadLibModel', 'MadLib']

# %% ../nbs/05_madlib.ipynb 1
import ipywidgets as widgets
import traitlets
from ipywidgets import Textarea, Text, Layout, HBox, Stack, Layout
from traitlets import HasTraits
import os

# %% ../nbs/05_madlib.ipynb 2
class MadLibModel(object):
def __init__(self, description, variables, template, **kwargs):
self.description = description
# two ways to save variables...
for key, value in variables.items(): # ... as attributes
setattr(self, key, value)
self.variables = variables # ...or as a dictionary, ex. { 'input_text': 'value' }
self.template = template # string: '{input_text}'

# %% ../nbs/05_madlib.ipynb 5
class MadLib(HBox):

def __init__(self, model, **kwargs):
super().__init__()

self.model = model
self.variables = {}
for i, (key, value) in enumerate(self.model.variables.items()):
text = Text(description=self.model.description, style={'description_width': 'initial'})
self.variables[key] = text

self.submit_button = widgets.Button(
value=False,
disabled=False,
button_style='success',
icon='arrow-circle-right'
)
self.submit_button.on_click(self.observe_submit_button)
self.children = list(self.variables.values()) + [self.submit_button]

def observe_submit_button(self, change):
for key, value in self.variables.items():
setattr(self.model, key, value.value)
Loading

0 comments on commit 01d61d5

Please sign in to comment.