Commit ·
c6403bc
1
Parent(s): 78f9769
Added the app for the hugging Face
Browse files
main.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import io
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
import torchvision
|
| 4 |
from torchvision.transforms import InterpolationMode
|
|
@@ -45,6 +47,65 @@ transform = torchvision.transforms.Compose(
|
|
| 45 |
class_names = ["pizza", "steak", "sushi"]
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
@app.post("/predict")
|
| 49 |
async def predict(file: UploadFile = File(...)):
|
| 50 |
# I read the incoming bytes into a PIL image
|
|
|
|
| 1 |
import io
|
| 2 |
+
from fastapi import FastAPI, UploadFile, File
|
| 3 |
+
from fastapi.responses import HTMLResponse # Add this import
|
| 4 |
import torch
|
| 5 |
import torchvision
|
| 6 |
from torchvision.transforms import InterpolationMode
|
|
|
|
| 47 |
class_names = ["pizza", "steak", "sushi"]
|
| 48 |
|
| 49 |
|
| 50 |
+
@app.get("/", response_class=HTMLResponse)
|
| 51 |
+
async def read_root():
|
| 52 |
+
html_content = """
|
| 53 |
+
<!DOCTYPE html>
|
| 54 |
+
<html>
|
| 55 |
+
<head>
|
| 56 |
+
<title>Model Testing API</title>
|
| 57 |
+
<style>
|
| 58 |
+
body { font-family: sans-serif; max-width: 600px; margin: 40px auto; padding: 20px; background: #121212; color: #ffffff; }
|
| 59 |
+
.box { border: 1px solid #333; padding: 20px; border-radius: 8px; background: #1e1e1e; }
|
| 60 |
+
button { background: #3b82f6; color: white; border: none; padding: 10px 15px; border-radius: 4px; cursor: pointer; margin-top: 10px; }
|
| 61 |
+
button:hover { background: #2563eb; }
|
| 62 |
+
pre { background: #000; padding: 15px; border-radius: 4px; overflow-x: auto; color: #10b981; }
|
| 63 |
+
</style>
|
| 64 |
+
</head>
|
| 65 |
+
<body>
|
| 66 |
+
<div class="box">
|
| 67 |
+
<h2>Backend Diagnostic UI</h2>
|
| 68 |
+
<p>Upload an image to test the API directly.</p>
|
| 69 |
+
<input type="file" id="imageInput" accept="image/jpeg, image/png">
|
| 70 |
+
<button onclick="testAPI()">Run Prediction</button>
|
| 71 |
+
|
| 72 |
+
<h3 style="margin-top: 20px;">Response:</h3>
|
| 73 |
+
<pre id="output">Awaiting image...</pre>
|
| 74 |
+
</div>
|
| 75 |
+
|
| 76 |
+
<script>
|
| 77 |
+
async function testAPI() {
|
| 78 |
+
const fileInput = document.getElementById('imageInput');
|
| 79 |
+
const output = document.getElementById('output');
|
| 80 |
+
|
| 81 |
+
if (fileInput.files.length === 0) {
|
| 82 |
+
output.innerText = "Please select an image first.";
|
| 83 |
+
return;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
output.innerText = "Processing...";
|
| 87 |
+
|
| 88 |
+
const formData = new FormData();
|
| 89 |
+
formData.append("file", fileInput.files[0]);
|
| 90 |
+
|
| 91 |
+
try {
|
| 92 |
+
const response = await fetch("/predict", {
|
| 93 |
+
method: "POST",
|
| 94 |
+
body: formData
|
| 95 |
+
});
|
| 96 |
+
const data = await response.json();
|
| 97 |
+
output.innerText = JSON.stringify(data, null, 2);
|
| 98 |
+
} catch (error) {
|
| 99 |
+
output.innerText = "Error hitting API: " + error.message;
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
</script>
|
| 103 |
+
</body>
|
| 104 |
+
</html>
|
| 105 |
+
"""
|
| 106 |
+
return HTMLResponse(content=html_content)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
@app.post("/predict")
|
| 110 |
async def predict(file: UploadFile = File(...)):
|
| 111 |
# I read the incoming bytes into a PIL image
|