Skip to content

Commit

Permalink
fix(main): fix choosing from material db in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamhexi committed Oct 4, 2024
1 parent 3e4237f commit c197e84
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
24 changes: 15 additions & 9 deletions knowledge_verificator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,37 @@
material_titles = [
material.title for material in material_db.materials
]
material = choose_from_menu(material_titles, 'materials')
material = choose_from_menu(
material_db.materials,
plural_name='materials',
attribute_to_show='title',
)

if material is None:
continue

paragraph = str(
PARAGRAPH = str(
choose_from_menu(material.paragraphs, 'paragraphs')
)
if paragraph is None:

if PARAGRAPH is None:
continue

console.print('Learn this paragraph: ')
console.print(paragraph)
console.print(PARAGRAPH)
console.print()
input('Press ENTER when ready.')

case '2':
console.print('Enter a paragraph you would like to learn: ')
paragraph = input().strip()
PARAGRAPH = input().strip()

case _:
console.print('Unrecognised option, try again!')

logger.debug('Loaded the following paragraph:\n %s', paragraph)
logger.debug('Loaded the following paragraph:\n %s', PARAGRAPH)

chosen_answer = chooser.choose_answer(paragraph=paragraph)
chosen_answer = chooser.choose_answer(paragraph=PARAGRAPH)
if not chosen_answer:
raise ValueError(
'The supplied paragaph is either too short or too general. '
Expand All @@ -83,7 +89,7 @@
)

question_with_context = qg_module.generate(
answer=chosen_answer, context=paragraph
answer=chosen_answer, context=PARAGRAPH
)
question = question_with_context['question']
logger.debug(
Expand All @@ -97,7 +103,7 @@

nli_module = NaturalLanguageInference()
relation = nli_module.infer_relation(
premise=paragraph, hypothesis=user_answer
premise=PARAGRAPH, hypothesis=user_answer
)

match relation:
Expand Down
16 changes: 12 additions & 4 deletions knowledge_verificator/utils/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@


def choose_from_menu(
menu_elements: list[str], plural_name: str, max_line_width: int = 40
menu_elements: list[Any],
plural_name: str,
attribute_to_show: str = '',
max_line_width: int = 40,
) -> Any | None:
"""
Prompt a user to choose an element from a list via terminal.
Expand All @@ -15,15 +18,20 @@ def choose_from_menu(
menu_elements (list[str]): List of elements to choose from.
Elements should be convertible to `str` (implement `__str__` method).
plural_name (str): Plural name of the elements. For example: options, paragraphs or names.
attribute_to_show (str): Attribute, which should be shown. If empty, print an entire object.
max_line_width (int): Maximum line width in number of columns. By default: 40.
Returns:
any | None: Element of a list or None if a user provided incorrect
value via a terminal.
any | None: Element of a list or None if a user provided incorrect value via a terminal.
"""
console.print(f'Available {plural_name}:')
for i, element in enumerate(menu_elements):
console.print(f'[{i+1}] {clip_text(element, max_line_width)}')
option_name = ''
if attribute_to_show:
option_name = getattr(element, attribute_to_show)
else:
option_name = element
console.print(f'[{i+1}] {clip_text(option_name, max_line_width)}')
material_choice = input('Your choice: ')
console.print()

Expand Down
2 changes: 2 additions & 0 deletions tests/test_qg.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ def test_basic_question_generation(
}

assert output == expected

# https://huggingface.co/sentence-transformers/all-distilroberta-v1

0 comments on commit c197e84

Please sign in to comment.