sharktide commited on
Commit
665a288
·
verified ·
1 Parent(s): f30b258

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -18
app.py CHANGED
@@ -36,30 +36,59 @@ if not os.path.exists(DATASET_PATH):
36
  # Authentication for Hugging Face
37
  login(token=TOKEN)
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  @app.get("/status")
40
  def status():
41
  return {"status": "200"}
42
 
43
- @app.put("/add/recipe")
44
- async def add_recipe(filename: str, recipe: Recipe):
45
- # Define the file path based on the filename query parameter
46
- file_path = os.path.join(DATASET_PATH, f"{filename}.json")
47
 
48
- # Check if the file already exists
49
- if os.path.exists(file_path):
50
- raise HTTPException(status_code=400, detail="File already exists")
51
 
52
- # Prepare the data to be written in JSON format
53
- recipe_data = recipe.dict() # Convert Recipe model to dictionary
54
 
55
- # Write the data to the new file
56
- try:
57
- with open(file_path, "w") as f:
58
- json.dump(recipe_data, f, indent=4)
59
 
60
- dataset = Dataset.from_json(file_path) # Load the new file
61
- dataset.push_to_hub("sharktide/recipes") # Push the dataset to the Hugging Face Hub
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- return {"message": f"Recipe '{filename}' added successfully."}
64
- except Exception as e:
65
- raise HTTPException(status_code=500, detail=f"Error writing file: {str(e)}")
 
36
  # Authentication for Hugging Face
37
  login(token=TOKEN)
38
 
39
+ JSON_DATASET_DIR = Path("json_dataset")
40
+ JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
41
+
42
+ # Define path for the JSON file to save data
43
+ JSON_DATASET_PATH = JSON_DATASET_DIR / f"train-{uuid4()}.json"
44
+
45
+ scheduler = CommitScheduler(
46
+ repo_id="sharktide/recipes",
47
+ repo_type="dataset",
48
+ folder_path=JSON_DATASET_DIR,
49
+ path_in_repo="data",
50
+ )
51
+
52
  @app.get("/status")
53
  def status():
54
  return {"status": "200"}
55
 
56
+ # @app.put("/add/recipe")
57
+ # async def add_recipe(filename: str, recipe: Recipe):
58
+ # # Define the file path based on the filename query parameter
59
+ # file_path = os.path.join(DATASET_PATH, f"{filename}.json")
60
 
61
+ # # Check if the file already exists
62
+ # if os.path.exists(file_path):
63
+ # raise HTTPException(status_code=400, detail="File already exists")
64
 
65
+ # # Prepare the data to be written in JSON format
66
+ # recipe_data = recipe.dict() # Convert Recipe model to dictionary
67
 
68
+ # # Write the data to the new file
69
+ # try:
70
+ # with open(file_path, "w") as f:
71
+ # json.dump(recipe_data, f, indent=4)
72
 
73
+ # dataset = Dataset.from_json(file_path) # Load the new file
74
+ # dataset.push_to_hub("sharktide/recipes") # Push the dataset to the Hugging Face Hub
75
+
76
+ # return {"message": f"Recipe '{filename}' added successfully."}
77
+ # except Exception as e:
78
+ # raise HTTPException(status_code=500, detail=f"Error writing file: {str(e)}")
79
+
80
+ @app.put("/add/beta")
81
+ def save_json(filename: str, recipe: Recipe) -> None:
82
+ # Save the data to the JSON file
83
+ with JSON_DATASET_PATH.open("a") as f:
84
+ # Writing the recipe details (name, ingredients, instructions) to the JSON file
85
+ json.dump({
86
+ "name": recipe.name,
87
+ "ingredients": recipe.ingredients,
88
+ "instructions": recipe.instructions
89
+ }, f)
90
+ f.write("\n")
91
+
92
 
93
+
94
+ # Commit and push the changes to the dataset