ParthivM1 commited on
Commit
bef097a
·
1 Parent(s): 6a2a7ae

Add application file

Browse files
Files changed (3) hide show
  1. Dockerfile +13 -0
  2. app.py +99 -0
  3. requirements.txt +10 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ WORKDIR /app
8
+
9
+ COPY --chown=user ./requirements.txt requirements.txt
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ COPY --chown=user . /app
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from supabase import create_client, Client
2
+ import os
3
+ import uuid
4
+ from fractions import Fraction
5
+ import exifread
6
+ import uvicorn
7
+ from fastapi import FastAPI, UploadFile, File, HTTPException
8
+ from fastapi.middleware.cors import CORSMiddleware
9
+ import os
10
+ from dotenv import load_dotenv
11
+
12
+ load_dotenv()
13
+
14
+ app = FastAPI()
15
+
16
+ app.add_middleware(
17
+ CORSMiddleware,
18
+ allow_origins=["*"],
19
+ allow_credentials=True,
20
+ allow_methods=["*"],
21
+ allow_headers=["*"]
22
+ )
23
+
24
+ SUPABASE_URL = os.getenv("SUPABASE_URL")
25
+ SUPABASE_KEY = os.getenv("SUPABASE_KEY")
26
+
27
+ if not SUPABASE_URL or not SUPABASE_KEY:
28
+ raise ValueError("Supabase credentials not found. Please set them in your environment variables.")
29
+
30
+ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
31
+
32
+ #Helper Functions
33
+
34
+ def convert_to_decimal(coord):
35
+ deg = float(coord.values[0])
36
+ min = float(coord.values[1]) / 60
37
+ sec = float(Fraction(str(coord.values[2]))) / 3600
38
+ return deg + min + sec
39
+
40
+ def get_gps_location(file_stream):
41
+ tags = exifread.process_file(file_stream)
42
+ gps_latitude = tags.get("GPS GPSLatitude")
43
+ gps_latitude_ref = tags.get("GPS GPSLatitudeRef")
44
+ gps_longitude = tags.get("GPS GPSLongitude")
45
+ gps_longitude_ref = tags.get("GPS GPSLongitudeRef")
46
+
47
+ if gps_latitude and gps_latitude_ref and gps_longitude and gps_longitude_ref:
48
+ lat = convert_to_decimal(gps_latitude)
49
+ lon = convert_to_decimal(gps_longitude)
50
+ if gps_latitude_ref.values[0] != 'N': lat = -lat
51
+ if gps_longitude_ref.values[0] != 'E': lon = -lon
52
+ return lat, lon
53
+ return None, None
54
+
55
+
56
+
57
+ @app.post("/upload")
58
+
59
+ async def upload_image(file: UploadFile = File(...)):
60
+
61
+ image_data = await file.read()
62
+
63
+ bucket_name = 'pothole-images'
64
+ file_extension = os.path.splitext(file.filename)[1]
65
+ uuid_path = f"{uuid.uuid4()}{file_extension}"
66
+
67
+ try:
68
+ supabase.storage.from_(bucket_name).upload(
69
+ path=uuid_path,
70
+ file=image_data,
71
+ file_options={"content-type": file.content_type}
72
+ )
73
+
74
+ url = supabase.storage.from_(bucket_name).get_public_url(uuid_path)
75
+ except Exception as e:
76
+ raise HTTPException(status_code=500, detail=f"Storage Error: {str(e)}")
77
+
78
+
79
+ lat, lon = get_gps_location(file.file)
80
+
81
+ if lat and lon:
82
+ location_point = f"Point({lon:.6f} {lat:.6f})"
83
+ else:
84
+ location_point = None
85
+
86
+
87
+ try:
88
+ response = (
89
+ supabase.table("potholes")
90
+ .insert({"image_name": file.filename, "location": location_point, "image_url": url})
91
+ .execute()
92
+ )
93
+ except Exception as e:
94
+ raise HTTPException(status_code=500, detail=f"Database Error: {str(e)}")
95
+
96
+ return {"message": "Upload successful", "image_url": url, "db_response": response.data}
97
+
98
+ if __name__ == "__main__":
99
+ uvicorn.run(app, port=8000, host="127.0.0.1")
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ gunicorn
4
+ python-dotenv
5
+ supabase
6
+ exifread
7
+ ultralytics
8
+ torch
9
+ torchvision
10
+ python-multipart