mateoluksenberg commited on
Commit
907e68e
1 Parent(s): be82676

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -4
app.py CHANGED
@@ -1,5 +1,5 @@
1
  from fastapi import FastAPI, File, UploadFile, HTTPException
2
- from fastapi.responses import JSONResponse
3
  from transformers import pipeline
4
  from PIL import Image
5
  import io
@@ -24,10 +24,49 @@ async def classify_image(file: UploadFile = File(...)):
24
  # Handle exceptions, for example: file not found, image format issues, etc.
25
  raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
26
 
27
- @app.get("/")
28
  async def home():
29
- return {"message": "Hello World"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Sample usage:
32
  # 1. Start the FastAPI server
33
- # 2. Use a tool like Postman or curl to send a POST request to /classify/ with an image file
 
1
  from fastapi import FastAPI, File, UploadFile, HTTPException
2
+ from fastapi.responses import HTMLResponse
3
  from transformers import pipeline
4
  from PIL import Image
5
  import io
 
24
  # Handle exceptions, for example: file not found, image format issues, etc.
25
  raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
26
 
27
+ @app.get("/", response_class=HTMLResponse)
28
  async def home():
29
+ html_content = """
30
+ <!DOCTYPE html>
31
+ <html>
32
+ <head>
33
+ <title>Image Classification</title>
34
+ </head>
35
+ <body>
36
+ <h1>Upload an Image for Classification</h1>
37
+ <form id="upload-form" enctype="multipart/form-data">
38
+ <input type="file" id="file" name="file" accept="image/*" required />
39
+ <button type="submit">Upload</button>
40
+ </form>
41
+ <div id="result"></div>
42
+ <script>
43
+ const form = document.getElementById('upload-form');
44
+ form.addEventListener('submit', async (e) => {
45
+ e.preventDefault();
46
+ const fileInput = document.getElementById('file');
47
+ const formData = new FormData();
48
+ formData.append('file', fileInput.files[0]);
49
+
50
+ const response = await fetch('/classify/', {
51
+ method: 'POST',
52
+ body: formData
53
+ });
54
+
55
+ const result = await response.json();
56
+
57
+ const resultDiv = document.getElementById('result');
58
+ if (response.ok) {
59
+ resultDiv.innerHTML = `<h2>Classification Result:</h2><p>${JSON.stringify(result.classification_result)}</p>`;
60
+ } else {
61
+ resultDiv.innerHTML = `<h2>Error:</h2><p>${result.detail}</p>`;
62
+ }
63
+ });
64
+ </script>
65
+ </body>
66
+ </html>
67
+ """
68
+ return HTMLResponse(content=html_content)
69
 
70
  # Sample usage:
71
  # 1. Start the FastAPI server
72
+ # 2. Open the browser and navigate to the root URL to upload an image and see the classification result