Update app.py
Browse files
app.py
CHANGED
|
@@ -10,9 +10,9 @@ from shapely.geometry import Polygon
|
|
| 10 |
from PIL import Image
|
| 11 |
from io import BytesIO
|
| 12 |
from concurrent.futures import ThreadPoolExecutor
|
| 13 |
-
from google.
|
| 14 |
-
from
|
| 15 |
-
from
|
| 16 |
import gspread
|
| 17 |
import time
|
| 18 |
|
|
@@ -28,14 +28,38 @@ dpi_value = 300
|
|
| 28 |
with st.expander("⚙️ Advanced Settings", expanded=True):
|
| 29 |
model.confidence = st.slider("Model Confidence (%)", 20, 100, 80)
|
| 30 |
|
| 31 |
-
# 📁 Setup Google Drive and Sheets
|
| 32 |
scope = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets"]
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
sheet = sheets_client.open_by_url(st.secrets["feedback_sheet_url"]).sheet1
|
| 38 |
|
|
|
|
| 39 |
# 📌 Helper Functions
|
| 40 |
def calculate_polygon_area(points):
|
| 41 |
polygon = Polygon([(p['x'], p['y']) for p in points])
|
|
|
|
| 10 |
from PIL import Image
|
| 11 |
from io import BytesIO
|
| 12 |
from concurrent.futures import ThreadPoolExecutor
|
| 13 |
+
from google.auth.transport.requests import Request
|
| 14 |
+
from google.oauth2.credentials import Credentials
|
| 15 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 16 |
import gspread
|
| 17 |
import time
|
| 18 |
|
|
|
|
| 28 |
with st.expander("⚙️ Advanced Settings", expanded=True):
|
| 29 |
model.confidence = st.slider("Model Confidence (%)", 20, 100, 80)
|
| 30 |
|
| 31 |
+
# 📁 Setup Google Drive and Sheets (Híbrido: Local e Hugging Face)
|
| 32 |
scope = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets"]
|
| 33 |
+
creds = None
|
| 34 |
+
|
| 35 |
+
# Tenta carregar as credenciais a partir do segredo do Hugging Face
|
| 36 |
+
# st.secrets.get( ) retorna None se o segredo não existir, evitando erros.
|
| 37 |
+
token_json_secret = st.secrets.get("GOOGLE_TOKEN_JSON")
|
| 38 |
+
|
| 39 |
+
if token_json_secret:
|
| 40 |
+
# Se estiver no Hugging Face, carrega as credenciais a partir do segredo
|
| 41 |
+
creds = Credentials.from_authorized_user_info(json.loads(token_json_secret), scope)
|
| 42 |
+
else:
|
| 43 |
+
# Se estiver rodando localmente, usa o fluxo de arquivos token.json/credentials.json
|
| 44 |
+
if os.path.exists('token.json'):
|
| 45 |
+
creds = Credentials.from_authorized_user_file('token.json', scope)
|
| 46 |
+
|
| 47 |
+
if not creds or not creds.valid:
|
| 48 |
+
if creds and creds.expired and creds.refresh_token:
|
| 49 |
+
creds.refresh(Request())
|
| 50 |
+
else:
|
| 51 |
+
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', scope)
|
| 52 |
+
creds = flow.run_local_server(port=0)
|
| 53 |
+
# Salva o token para futuras execuções locais
|
| 54 |
+
with open('token.json', 'w') as token:
|
| 55 |
+
token.write(creds.to_json())
|
| 56 |
+
|
| 57 |
+
# Constrói os serviços com as credenciais obtidas
|
| 58 |
+
drive_service = build("drive", "v3", credentials=creds)
|
| 59 |
+
sheets_client = gspread.authorize(creds)
|
| 60 |
sheet = sheets_client.open_by_url(st.secrets["feedback_sheet_url"]).sheet1
|
| 61 |
|
| 62 |
+
|
| 63 |
# 📌 Helper Functions
|
| 64 |
def calculate_polygon_area(points):
|
| 65 |
polygon = Polygon([(p['x'], p['y']) for p in points])
|