Spaces:
Runtime error
Runtime error
test
Browse files- .env +2 -0
- __pycache__/app.cpython-313.pyc +0 -0
- __pycache__/db.cpython-313.pyc +0 -0
- __pycache__/main.cpython-313.pyc +0 -0
- app.py +45 -0
- db.py +11 -0
- dockerfile +20 -0
- requirements.txt +5 -0
.env
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MONGO_URI=mongodb+srv://pujanneupaneop0907:Pujan@1234@cluster0.5kuhtss.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
|
| 2 |
+
|
__pycache__/app.cpython-313.pyc
ADDED
|
Binary file (2.47 kB). View file
|
|
|
__pycache__/db.cpython-313.pyc
ADDED
|
Binary file (604 Bytes). View file
|
|
|
__pycache__/main.cpython-313.pyc
ADDED
|
Binary file (2.27 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from db import db
|
| 4 |
+
from bson import ObjectId
|
| 5 |
+
from bson.errors import InvalidId
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class Item(BaseModel):
|
| 11 |
+
name: str
|
| 12 |
+
description: str
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@app.post("/items/")
|
| 16 |
+
async def create_item(item: Item):
|
| 17 |
+
result = await db["items"].insert_one(item.dict())
|
| 18 |
+
return {"id": str(result.inserted_id), "message": "Item created"}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@app.get("/items/")
|
| 22 |
+
async def read_items():
|
| 23 |
+
items = []
|
| 24 |
+
cursor = db["items"].find()
|
| 25 |
+
async for document in cursor:
|
| 26 |
+
document["id"] = str(document["_id"])
|
| 27 |
+
del document["_id"]
|
| 28 |
+
items.append(document)
|
| 29 |
+
return items
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@app.get("/items/{item_id}")
|
| 33 |
+
async def read_item(item_id: str):
|
| 34 |
+
try:
|
| 35 |
+
oid = ObjectId(item_id)
|
| 36 |
+
except InvalidId:
|
| 37 |
+
raise HTTPException(status_code=400, detail="Invalid item ID format")
|
| 38 |
+
|
| 39 |
+
item = await db["items"].find_one({"_id": oid})
|
| 40 |
+
if not item:
|
| 41 |
+
raise HTTPException(status_code=404, detail="Item not found")
|
| 42 |
+
|
| 43 |
+
item["id"] = str(item["_id"])
|
| 44 |
+
del item["_id"]
|
| 45 |
+
return item
|
db.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from motor.motor_asyncio import AsyncIOMotorClient
|
| 2 |
+
from urllib.parse import quote_plus
|
| 3 |
+
|
| 4 |
+
username = "pujanneupaneop0907"
|
| 5 |
+
password = "Pujan@1234" # your actual password
|
| 6 |
+
password_encoded = quote_plus(password)
|
| 7 |
+
|
| 8 |
+
MONGO_URI = f"mongodb+srv://{username}:{password_encoded}@cluster0.5kuhtss.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
|
| 9 |
+
|
| 10 |
+
client = AsyncIOMotorClient(MONGO_URI)
|
| 11 |
+
db = client["your_database_name"] # Replace with your DB name
|
dockerfile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use official Python image
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy your app files
|
| 8 |
+
COPY ./app.py ./app.py
|
| 9 |
+
COPY ./db.py ./db.py
|
| 10 |
+
COPY ./requirements.txt ./requirements.txt
|
| 11 |
+
|
| 12 |
+
# Install dependencies
|
| 13 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 14 |
+
|
| 15 |
+
# Expose port (Hugging Face Spaces uses 7860 or 8000)
|
| 16 |
+
EXPOSE 8000
|
| 17 |
+
|
| 18 |
+
# Command to run the app with uvicorn
|
| 19 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
| 20 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
motor
|
| 4 |
+
pymongo
|
| 5 |
+
|