from fastapi import FastAPI, UploadFile, File, Form
from pydantic import BaseModel
from typing import Optional
import pandas as pd
import io
app = FastAPI()
class UserRequest(BaseModel):
name: str
inputfile: Optional[UploadFile] = None
@app.post("/upload/")
async def upload_file(name: str = Form(...), inputfile: UploadFile = File(...)):
# Read the uploaded file
contents = await inputfile.read()
# Convert the bytes data into a pandas DataFrame
df = pd.read_csv(io.BytesIO(contents))
# You can process the DataFrame here as needed
print(df.head())
# Create a UserRequest object
user_request = UserRequest(name=name, inputfile=inputfile)
return {"filename": inputfile.filename, "name": user_request.name}
# Run the FastAPI application using:
# uvicorn your_module_name:app --reload