Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,21 +58,73 @@ scheduler = CommitScheduler(
|
|
| 58 |
path_in_repo="data",
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
@app.get("/status")
|
| 62 |
def status():
|
| 63 |
return {"status": "200"}
|
| 64 |
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
@app.put("/add/recipe")
|
| 67 |
-
def
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
|
|
|
|
| 58 |
path_in_repo="data",
|
| 59 |
)
|
| 60 |
|
| 61 |
+
def check_huggingface_recipe_name(name: str) -> bool:
|
| 62 |
+
url = "https://datasets-server.huggingface.co/rows?dataset=sharktide%2Frecipes&config=default&split=train"
|
| 63 |
+
response = requests.get(url)
|
| 64 |
+
|
| 65 |
+
if response.status_code == 200:
|
| 66 |
+
data = response.json()
|
| 67 |
+
# Extract all the recipe names in the dataset
|
| 68 |
+
existing_names = [row['row']['name'] for row in data['rows']]
|
| 69 |
+
return name in existing_names
|
| 70 |
+
else:
|
| 71 |
+
raise HTTPException(status_code=500, detail="Error accessing store")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def check_local_cache_recipe_name(name: str) -> bool:
|
| 75 |
+
for file in os.listdir(JSON_DATASET_DIR):
|
| 76 |
+
file_path = JSON_DATASET_DIR / file
|
| 77 |
+
if file_path.suffix == ".json":
|
| 78 |
+
with open(file_path, "r") as f:
|
| 79 |
+
for line in f:
|
| 80 |
+
try:
|
| 81 |
+
recipe_data = json.loads(line)
|
| 82 |
+
if recipe_data['name'] == name:
|
| 83 |
+
return True
|
| 84 |
+
except json.JSONDecodeError:
|
| 85 |
+
continue
|
| 86 |
+
return False
|
| 87 |
+
|
| 88 |
+
|
| 89 |
@app.get("/status")
|
| 90 |
def status():
|
| 91 |
return {"status": "200"}
|
| 92 |
|
| 93 |
|
| 94 |
+
# @app.put("/add/recipe")
|
| 95 |
+
# def save_json(filename: str, recipe: Recipe):
|
| 96 |
+
# with JSON_DATASET_PATH.open("a") as f:
|
| 97 |
+
# json.dump({
|
| 98 |
+
# "name": recipe.name,
|
| 99 |
+
# "ingredients": recipe.ingredients,
|
| 100 |
+
# "instructions": recipe.instructions
|
| 101 |
+
# }, f)
|
| 102 |
+
# f.write("\n")
|
| 103 |
+
# return {"Status": "Added to cache"}
|
| 104 |
+
|
| 105 |
@app.put("/add/recipe")
|
| 106 |
+
def add_recipe(recipe: Recipe):
|
| 107 |
+
# First, check if the name exists in the Hugging Face dataset
|
| 108 |
+
if check_huggingface_recipe_name(recipe.name):
|
| 109 |
+
raise HTTPException(status_code=400, detail="Recipe name already exists in the store.")
|
| 110 |
+
|
| 111 |
+
# Second, check if the name exists in the local cache
|
| 112 |
+
if check_local_cache_recipe_name(recipe.name):
|
| 113 |
+
raise HTTPException(status_code=400, detail="Recipe name already exists in the local cache.")
|
| 114 |
+
|
| 115 |
+
# If both checks pass, save the recipe to the cache
|
| 116 |
+
try:
|
| 117 |
+
with JSON_DATASET_PATH.open("a") as f:
|
| 118 |
+
json.dump({
|
| 119 |
+
"name": recipe.name,
|
| 120 |
+
"ingredients": recipe.ingredients,
|
| 121 |
+
"instructions": recipe.instructions
|
| 122 |
+
}, f)
|
| 123 |
+
f.write("\n")
|
| 124 |
+
return {"Status": "Recipe added successfully."}
|
| 125 |
+
|
| 126 |
+
except Exception as e:
|
| 127 |
+
raise HTTPException(status_code=500, detail=f"Error saving recipe: {str(e)}")
|
| 128 |
|
| 129 |
|
| 130 |
|