File size: 1,878 Bytes
3ebdd53
 
a3ac8df
3ebdd53
 
 
 
 
 
a3ac8df
3ebdd53
a3ac8df
 
3ebdd53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3ac8df
3ebdd53
 
 
a3ac8df
3ebdd53
 
a3ac8df
3ebdd53
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import json
import os
from datasets import Dataset, DatasetDict, DatasetInfo
from huggingface_hub import login, HfApi
from typing import List
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware

# Initialize FastAPI
app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Allow all origins (or specify specific origins)
    allow_credentials=True,
    allow_methods=["*"],  # Allow all HTTP methods
    allow_headers=["*"],  # Allow all headers
)

TOKEN = os.getenv("token")


class Recipe(BaseModel):
    ingredients: List[str]
    instructions: str

DATASET_PATH = "/mnt/data/sharktide/recipes/data"

# Ensure the 'data' folder exists
if not os.path.exists(DATASET_PATH):
    os.makedirs(DATASET_PATH)

# Authentication for Hugging Face
login(token=TOKEN)

@app.put("/add/recipe")
async def add_recipe(filename: str, recipe: Recipe):
    # Define the file path based on the filename query parameter
    file_path = os.path.join(DATASET_PATH, f"{filename}.json")

    # Check if the file already exists
    if os.path.exists(file_path):
        raise HTTPException(status_code=400, detail="File already exists")

    # Prepare the data to be written in JSON format
    recipe_data = recipe.dict()  # Convert Recipe model to dictionary

    # Write the data to the new file
    try:
        with open(file_path, "w") as f:
            json.dump(recipe_data, f, indent=4)
        
        dataset = Dataset.from_json(file_path)  # Load the new file
        dataset.push_to_hub("sharktide/recipes")  # Push the dataset to the Hugging Face Hub
        
        return {"message": f"Recipe '{filename}' added successfully."}
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error writing file: {str(e)}")