Spaces:
Runtime error
Runtime error
login modifieed
Browse files
app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
-
from fastapi import FastAPI, File, UploadFile
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
from model import YOLOModel
|
| 9 |
import shutil
|
|
@@ -15,6 +18,92 @@ UPLOAD_FOLDER.mkdir(exist_ok=True)
|
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@app.post("/upload")
|
| 19 |
async def upload_image(image: UploadFile = File(...)):
|
| 20 |
# print(f'\n\t\tUPLOADED!!!!')
|
|
@@ -34,10 +123,36 @@ async def upload_image(image: UploadFile = File(...)):
|
|
| 34 |
except Exception as e:
|
| 35 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# code to accept the localhost to get images from
|
| 38 |
app.add_middleware(
|
| 39 |
CORSMiddleware,
|
| 40 |
-
allow_origins=["http://192.168.56.1:3000", "http://192.168.56.1:3001"],
|
| 41 |
allow_methods=["*"],
|
| 42 |
allow_headers=["*"],
|
| 43 |
)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Response, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse, FileResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
| 7 |
+
import sqlite3
|
| 8 |
+
from pydantic import BaseModel, EmailStr
|
| 9 |
+
|
| 10 |
from pathlib import Path
|
| 11 |
from model import YOLOModel
|
| 12 |
import shutil
|
|
|
|
| 18 |
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
+
cropped_images_dir = "cropped_images"
|
| 22 |
+
|
| 23 |
+
# Initialize SQLite database
|
| 24 |
+
def init_db():
|
| 25 |
+
conn = sqlite3.connect('users.db')
|
| 26 |
+
c = conn.cursor()
|
| 27 |
+
c.execute('''
|
| 28 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 29 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 30 |
+
firstName TEXT NOT NULL,
|
| 31 |
+
lastName TEXT NOT NULL,
|
| 32 |
+
country TEXT,
|
| 33 |
+
phone TEXT, -- Phone number stored as TEXT to allow various formats
|
| 34 |
+
email TEXT UNIQUE NOT NULL, -- Email should be unique and non-null
|
| 35 |
+
password TEXT NOT NULL -- Password will be stored as a string (hashed ideally)
|
| 36 |
+
)
|
| 37 |
+
''')
|
| 38 |
+
conn.commit()
|
| 39 |
+
conn.close()
|
| 40 |
+
|
| 41 |
+
init_db()
|
| 42 |
+
|
| 43 |
+
class UserSignup(BaseModel):
|
| 44 |
+
firstName: str
|
| 45 |
+
lastName: str
|
| 46 |
+
country: str
|
| 47 |
+
phone: str
|
| 48 |
+
email: EmailStr
|
| 49 |
+
password: str
|
| 50 |
+
|
| 51 |
+
class UserLogin(BaseModel):
|
| 52 |
+
email: str
|
| 53 |
+
password: str
|
| 54 |
+
|
| 55 |
+
@app.post("/signup")
|
| 56 |
+
async def signup(user_data: UserSignup):
|
| 57 |
+
try:
|
| 58 |
+
conn = sqlite3.connect('users.db')
|
| 59 |
+
c = conn.cursor()
|
| 60 |
+
|
| 61 |
+
# Check if user already exists
|
| 62 |
+
c.execute("SELECT * FROM users WHERE email = ?", (user_data.email,))
|
| 63 |
+
if c.fetchone():
|
| 64 |
+
raise HTTPException(status_code=400, detail="Email already registered")
|
| 65 |
+
|
| 66 |
+
# Insert new user
|
| 67 |
+
c.execute("""
|
| 68 |
+
INSERT INTO users (firstName, lastName, country, number, email, password)
|
| 69 |
+
VALUES (?, ?, ?, ?)
|
| 70 |
+
""", (user_data.firstName, user_data.lastName, user_data.country, user_data.number, user_data.email, user_data.password))
|
| 71 |
+
|
| 72 |
+
conn.commit()
|
| 73 |
+
conn.close()
|
| 74 |
+
|
| 75 |
+
return {"message": "User registered successfully", "email": user_data.email}
|
| 76 |
+
except Exception as e:
|
| 77 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 78 |
+
|
| 79 |
+
@app.post("/login")
|
| 80 |
+
async def login(user_data: UserLogin):
|
| 81 |
+
try:
|
| 82 |
+
conn = sqlite3.connect('users.db')
|
| 83 |
+
c = conn.cursor()
|
| 84 |
+
|
| 85 |
+
# Find user
|
| 86 |
+
c.execute("SELECT * FROM users WHERE email = ? AND password = ?",
|
| 87 |
+
(user_data.email, user_data.password))
|
| 88 |
+
user = c.fetchone()
|
| 89 |
+
|
| 90 |
+
conn.close()
|
| 91 |
+
|
| 92 |
+
if not user:
|
| 93 |
+
raise HTTPException(status_code=401, detail="Invalid credentials")
|
| 94 |
+
|
| 95 |
+
return {
|
| 96 |
+
"message": "Login successful",
|
| 97 |
+
"user": {
|
| 98 |
+
"firstName": user[1],
|
| 99 |
+
"lastName": user[2],
|
| 100 |
+
"email": user[3]
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
except Exception as e:
|
| 104 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 105 |
+
|
| 106 |
+
|
| 107 |
@app.post("/upload")
|
| 108 |
async def upload_image(image: UploadFile = File(...)):
|
| 109 |
# print(f'\n\t\tUPLOADED!!!!')
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 125 |
|
| 126 |
+
|
| 127 |
+
def cleanup_images(directory: str):
|
| 128 |
+
"""Remove all images in the directory."""
|
| 129 |
+
for file in Path(directory).glob("*"):
|
| 130 |
+
file.unlink()
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
# @app.post("/upload")
|
| 134 |
+
# async def upload_image(image: UploadFile = File(...)):
|
| 135 |
+
# # print(f'\n\t\tUPLOADED!!!!')
|
| 136 |
+
# try:
|
| 137 |
+
# file_path = UPLOAD_FOLDER / image.filename
|
| 138 |
+
# with file_path.open("wb") as buffer:
|
| 139 |
+
# shutil.copyfileobj(image.file, buffer)
|
| 140 |
+
# # print(f'Starting to pass into model, {file_path}')
|
| 141 |
+
# # Perform YOLO inference
|
| 142 |
+
# predictions = yolo.predict(str(file_path))
|
| 143 |
+
# print(f'\n\n\n{predictions}\n\n\ \n\t\t\t\tare predictions')
|
| 144 |
+
# # Clean up uploaded file
|
| 145 |
+
# file_path.unlink() # Remove file after processing
|
| 146 |
+
# return JSONResponse(content={"items": predictions})
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
except Exception as e:
|
| 150 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 151 |
+
|
| 152 |
# code to accept the localhost to get images from
|
| 153 |
app.add_middleware(
|
| 154 |
CORSMiddleware,
|
| 155 |
+
allow_origins=["http://192.168.56.1:3000", "http://192.168.56.1:3001", "https://recognizethis.netlify.app/"],
|
| 156 |
allow_methods=["*"],
|
| 157 |
allow_headers=["*"],
|
| 158 |
)
|