-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
57 lines (47 loc) · 1.75 KB
/
main.py
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
from fastapi import FastAPI, File, UploadFile, Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from pathlib import Path
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
app = FastAPI()
# Mount static files
app.mount("/static", StaticFiles(directory="static"), name="static")
# Set up Jinja2 templates
templates = Jinja2Templates(directory="templates")
# Create a directory for animal images if it doesn't exist
Path("static/images").mkdir(parents=True, exist_ok=True)
# Sample animal images (you should replace these with actual image files)
animal_images = {
"cat": "/static/images/cat.jpg",
"dog": "/static/images/dog.jpg",
"elephant": "/static/images/elephant.jpg"
}
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
logger.debug("Received request for root path")
try:
response = templates.TemplateResponse("index.html", {"request": request})
logger.debug(f"Rendering template with context: {response.context}")
return response
except Exception as e:
logger.error(f"Error rendering template: {str(e)}")
raise
@app.get("/get_animal_image/{animal}")
async def get_animal_image(animal: str):
if animal in animal_images:
return {"image_url": animal_images[animal]}
return {"error": "Animal not found"}
@app.post("/upload_file")
async def upload_file(file: UploadFile = File(...)):
contents = await file.read()
return {
"filename": file.filename,
"size": len(contents),
"content_type": file.content_type
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)