Spaces:
Sleeping
Sleeping
Create format/format_output.py
Browse files- format/format_output.py +42 -0
format/format_output.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def format_output(raw_response):
|
| 5 |
+
response_object = json.loads(raw_response)
|
| 6 |
+
|
| 7 |
+
title_block = __get_title_block(response_object["title"])
|
| 8 |
+
ingredients_block = __get_ingredients_block(response_object["ingredients"])
|
| 9 |
+
method_block = __get_method_block(response_object["method"])
|
| 10 |
+
|
| 11 |
+
return title_block + ingredients_block + method_block
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def __get_title_block(title):
|
| 15 |
+
return f"# <ins>{title.title()}</ins>\n\n"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def __get_ingredients_block(ingredients):
|
| 19 |
+
ingredients_block = "## Ingredients:\n"
|
| 20 |
+
|
| 21 |
+
for ingredient in ingredients:
|
| 22 |
+
ingredients_block += f"- {ingredient.capitalize()}\n"
|
| 23 |
+
|
| 24 |
+
return f"{ingredients_block}\n\n"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def __get_method_block(method):
|
| 28 |
+
method_block = "## Method:\n"
|
| 29 |
+
|
| 30 |
+
for step in method:
|
| 31 |
+
sentences = step.split(".")
|
| 32 |
+
|
| 33 |
+
formatted_sentences = []
|
| 34 |
+
|
| 35 |
+
for sentence in sentences:
|
| 36 |
+
formatted_sentences.append(sentence.strip().capitalize())
|
| 37 |
+
|
| 38 |
+
joined_sentences = ". ".join(formatted_sentences)
|
| 39 |
+
|
| 40 |
+
method_block += f"- {joined_sentences}\n"
|
| 41 |
+
|
| 42 |
+
return f"{method_block}\n\n"
|