Download app.py from RKrish03/sampleApp: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/RKrish03/sampleApp/resolve/main/app.py
- Command line
-
hf download hf://spaces/RKrish03/sampleApp/app.py
-
curl -L -o app.py https://huggingface.co/spaces/RKrish03/sampleApp/resolve/main/app.py
1.53 kB
| from typing import List, Optional | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| import uvicorn | |
| app = FastAPI() | |
| # Customer Model | |
| class Customer(BaseModel): | |
| id: int | |
| name: str | |
| email: str | |
| phone: Optional[str] = None | |
| address: Optional[str] = None | |
| # In-memory storage | |
| customers_list: List[Customer] = [] | |
| # Welcome API | |
| def welcome(): | |
| return {"message": "Customer API is running"} | |
| # Create Customer | |
| def create_customer(customer: Customer): | |
| customers_list.append(customer) | |
| return customer | |
| # Read All Customers | |
| def get_customers(): | |
| return customers_list | |
| # Update Customer | |
| def update_customer(id: int, customer: Customer): | |
| for i, existing_customer in enumerate(customers_list): | |
| if existing_customer.id == id: | |
| customers_list[i] = customer | |
| return customer | |
| raise HTTPException(status_code=404, detail="Customer not found") | |
| # Delete Customer | |
| def delete_customer(id: int): | |
| for i, customer in enumerate(customers_list): | |
| if customer.id == id: | |
| customers_list.pop(i) | |
| return {"message": "Customer deleted successfully"} | |
| raise HTTPException(status_code=404, detail="Customer not found") | |
| if __name__ == "__main__": | |
| uvicorn.run( | |
| "app:app", | |
| host="0.0.0.0", | |
| port=7860 | |
| ) |