Spaces:
Runtime error
Runtime error
First commit
Browse files- .gitignore +1 -0
- Dockerfile +17 -0
- main.py +36 -0
- requirements.txt +4 -0
- templates/gallery.html +36 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
venv/
|
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use official Python image
|
| 2 |
+
FROM python:3.10
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy files
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
# Install dependencies
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Expose port
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
# Run the app
|
| 17 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Request
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
from fastapi.templating import Jinja2Templates
|
| 5 |
+
import shutil
|
| 6 |
+
import os
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
UPLOAD_DIR = "static/uploads"
|
| 12 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 15 |
+
templates = Jinja2Templates(directory="templates")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@app.post("/upload")
|
| 19 |
+
async def upload_image(file: UploadFile = File(...)):
|
| 20 |
+
filename = datetime.now().strftime("%Y%m%d_%H%M%S_") + file.filename
|
| 21 |
+
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 22 |
+
with open(file_path, "wb") as buffer:
|
| 23 |
+
shutil.copyfileobj(file.file, buffer)
|
| 24 |
+
return {"message": "Image uploaded", "filename": filename}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@app.get("/gallery", response_class=HTMLResponse)
|
| 28 |
+
async def show_gallery(request: Request):
|
| 29 |
+
images = os.listdir(UPLOAD_DIR)
|
| 30 |
+
images.sort(reverse=True) # Newest first
|
| 31 |
+
recent_images = images[:5] # Only the latest 5
|
| 32 |
+
return templates.TemplateResponse("gallery.html", {"request": request, "images": recent_images})
|
| 33 |
+
|
| 34 |
+
@app.get("/")
|
| 35 |
+
def greet_json():
|
| 36 |
+
return {"Hello": "World!"}
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
jinja2
|
| 4 |
+
python-multipart
|
templates/gallery.html
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Uploaded Image Gallery</title>
|
| 5 |
+
<style>
|
| 6 |
+
body {
|
| 7 |
+
font-family: Arial, sans-serif;
|
| 8 |
+
padding: 20px;
|
| 9 |
+
}
|
| 10 |
+
#gallery {
|
| 11 |
+
display: flex;
|
| 12 |
+
flex-wrap: wrap;
|
| 13 |
+
gap: 10px;
|
| 14 |
+
}
|
| 15 |
+
.img-wrapper {
|
| 16 |
+
border: 1px solid #ccc;
|
| 17 |
+
padding: 5px;
|
| 18 |
+
background: #f1f1f1;
|
| 19 |
+
}
|
| 20 |
+
.img-wrapper img {
|
| 21 |
+
width: 240px;
|
| 22 |
+
height: auto;
|
| 23 |
+
}
|
| 24 |
+
</style>
|
| 25 |
+
</head>
|
| 26 |
+
<body>
|
| 27 |
+
<h2>Uploaded Image Gallery</h2>
|
| 28 |
+
<div id="gallery">
|
| 29 |
+
{% for image in images %}
|
| 30 |
+
<div class="img-wrapper">
|
| 31 |
+
<img src="/static/uploads/{{ image }}" alt="{{ image }}">
|
| 32 |
+
</div>
|
| 33 |
+
{% endfor %}
|
| 34 |
+
</div>
|
| 35 |
+
</body>
|
| 36 |
+
</html>
|