Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,25 +2,44 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from gradio_client import Client, handle_file
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
| 6 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 7 |
|
| 8 |
if not HF_TOKEN:
|
| 9 |
raise ValueError(
|
| 10 |
-
"HF_TOKEN environment variable not set.
|
| 11 |
)
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
| 14 |
coldet_client = Client(
|
| 15 |
"Coldet/backend",
|
| 16 |
-
headers={
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# -----------------------------
|
| 20 |
-
#
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
def create_account(username: str, password: str) -> str:
|
| 24 |
try:
|
| 25 |
result = coldet_client.predict(
|
| 26 |
username=username,
|
|
@@ -34,7 +53,8 @@ def create_account(username: str, password: str) -> str:
|
|
| 34 |
return f"Error: {str(e)}"
|
| 35 |
|
| 36 |
|
| 37 |
-
def login(username: str, password: str)
|
|
|
|
| 38 |
try:
|
| 39 |
result = coldet_client.predict(
|
| 40 |
username=username,
|
|
@@ -48,7 +68,8 @@ def login(username: str, password: str) -> str:
|
|
| 48 |
return f"Error: {str(e)}"
|
| 49 |
|
| 50 |
|
| 51 |
-
def create_token(session_id: str)
|
|
|
|
| 52 |
try:
|
| 53 |
result = coldet_client.predict(
|
| 54 |
session_id=session_id,
|
|
@@ -61,65 +82,12 @@ def create_token(session_id: str) -> str:
|
|
| 61 |
return f"Error: {str(e)}"
|
| 62 |
|
| 63 |
|
| 64 |
-
# -----------------------------
|
| 65 |
-
# Auto Fetch Models
|
| 66 |
-
# -----------------------------
|
| 67 |
-
|
| 68 |
-
def get_models():
|
| 69 |
-
try:
|
| 70 |
-
config = coldet_client.view_api(return_format="dict")
|
| 71 |
-
|
| 72 |
-
predict_endpoint = config["named_endpoints"]["/predict"]
|
| 73 |
-
|
| 74 |
-
params = predict_endpoint["parameters"]
|
| 75 |
-
|
| 76 |
-
for param in params:
|
| 77 |
-
if param.get("parameter_name") == "model":
|
| 78 |
-
|
| 79 |
-
python_type = param.get("python_type", "")
|
| 80 |
-
|
| 81 |
-
if "Literal[" in python_type:
|
| 82 |
-
|
| 83 |
-
models = (
|
| 84 |
-
python_type
|
| 85 |
-
.replace("Literal[", "")
|
| 86 |
-
.replace("]", "")
|
| 87 |
-
.replace("'", "")
|
| 88 |
-
.split(", ")
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
return models
|
| 92 |
-
|
| 93 |
-
return ["coldet-v1-mini.coldet"]
|
| 94 |
-
|
| 95 |
-
except Exception as e:
|
| 96 |
-
print(f"Failed to fetch models: {e}")
|
| 97 |
-
|
| 98 |
-
return ["coldet-v1-mini.coldet"]
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
AVAILABLE_MODELS = get_models()
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
def refresh_models():
|
| 105 |
-
global AVAILABLE_MODELS
|
| 106 |
-
|
| 107 |
-
AVAILABLE_MODELS = get_models()
|
| 108 |
-
|
| 109 |
-
return gr.update(
|
| 110 |
-
choices=AVAILABLE_MODELS,
|
| 111 |
-
value=AVAILABLE_MODELS[0] if AVAILABLE_MODELS else None,
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
# -----------------------------
|
| 116 |
-
# Predict
|
| 117 |
-
# -----------------------------
|
| 118 |
-
|
| 119 |
def predict(image, api_key: str, model: str):
|
| 120 |
|
| 121 |
if image is None:
|
| 122 |
-
return {
|
|
|
|
|
|
|
| 123 |
|
| 124 |
try:
|
| 125 |
result = coldet_client.predict(
|
|
@@ -132,25 +100,27 @@ def predict(image, api_key: str, model: str):
|
|
| 132 |
return result
|
| 133 |
|
| 134 |
except Exception as e:
|
| 135 |
-
return {
|
| 136 |
-
|
|
|
|
| 137 |
|
| 138 |
-
# -----------------------------
|
| 139 |
# UI
|
| 140 |
-
# -----------------------------
|
| 141 |
|
| 142 |
with gr.Blocks(
|
| 143 |
title="Coldet Backend Interface"
|
| 144 |
) as demo:
|
| 145 |
|
| 146 |
gr.Markdown("# π Coldet Backend Interface")
|
|
|
|
| 147 |
gr.Markdown(
|
| 148 |
"Use this interface to interact with the Coldet backend API."
|
| 149 |
)
|
| 150 |
|
| 151 |
-
# -----------------------------
|
| 152 |
# Account Tab
|
| 153 |
-
# -----------------------------
|
| 154 |
|
| 155 |
with gr.Tab("π Account"):
|
| 156 |
|
|
@@ -158,7 +128,10 @@ with gr.Blocks(
|
|
| 158 |
|
| 159 |
with gr.Row():
|
| 160 |
|
|
|
|
| 161 |
# Create Account
|
|
|
|
|
|
|
| 162 |
with gr.Column():
|
| 163 |
|
| 164 |
gr.Markdown("#### Create Account")
|
|
@@ -174,7 +147,9 @@ with gr.Blocks(
|
|
| 174 |
type="password"
|
| 175 |
)
|
| 176 |
|
| 177 |
-
create_btn = gr.Button(
|
|
|
|
|
|
|
| 178 |
|
| 179 |
create_output = gr.Textbox(
|
| 180 |
label="Session ID",
|
|
@@ -184,11 +159,17 @@ with gr.Blocks(
|
|
| 184 |
|
| 185 |
create_btn.click(
|
| 186 |
fn=create_account,
|
| 187 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
| 188 |
outputs=create_output,
|
| 189 |
)
|
| 190 |
|
|
|
|
| 191 |
# Login
|
|
|
|
|
|
|
| 192 |
with gr.Column():
|
| 193 |
|
| 194 |
gr.Markdown("#### Login")
|
|
@@ -204,7 +185,9 @@ with gr.Blocks(
|
|
| 204 |
type="password"
|
| 205 |
)
|
| 206 |
|
| 207 |
-
login_btn = gr.Button(
|
|
|
|
|
|
|
| 208 |
|
| 209 |
login_output = gr.Textbox(
|
| 210 |
label="Session ID",
|
|
@@ -214,13 +197,16 @@ with gr.Blocks(
|
|
| 214 |
|
| 215 |
login_btn.click(
|
| 216 |
fn=login,
|
| 217 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
| 218 |
outputs=login_output,
|
| 219 |
)
|
| 220 |
|
| 221 |
-
# -----------------------------
|
| 222 |
# API Key Tab
|
| 223 |
-
# -----------------------------
|
| 224 |
|
| 225 |
with gr.Tab("π API Key"):
|
| 226 |
|
|
@@ -232,7 +218,9 @@ with gr.Blocks(
|
|
| 232 |
lines=2
|
| 233 |
)
|
| 234 |
|
| 235 |
-
token_btn = gr.Button(
|
|
|
|
|
|
|
| 236 |
|
| 237 |
token_output = gr.Textbox(
|
| 238 |
label="API Key",
|
|
@@ -246,9 +234,9 @@ with gr.Blocks(
|
|
| 246 |
outputs=token_output,
|
| 247 |
)
|
| 248 |
|
| 249 |
-
# -----------------------------
|
| 250 |
# Inference Tab
|
| 251 |
-
# -----------------------------
|
| 252 |
|
| 253 |
with gr.Tab("π€ Inference"):
|
| 254 |
|
|
@@ -265,20 +253,18 @@ with gr.Blocks(
|
|
| 265 |
|
| 266 |
api_key_input = gr.Textbox(
|
| 267 |
label="API Key",
|
| 268 |
-
placeholder="Paste your API key here"
|
| 269 |
)
|
| 270 |
|
| 271 |
model_input = gr.Dropdown(
|
| 272 |
label="Model",
|
| 273 |
choices=AVAILABLE_MODELS,
|
| 274 |
-
value=AVAILABLE_MODELS[0]
|
| 275 |
-
if AVAILABLE_MODELS
|
| 276 |
-
else None,
|
| 277 |
)
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
|
| 283 |
with gr.Column():
|
| 284 |
|
|
@@ -286,11 +272,6 @@ with gr.Blocks(
|
|
| 286 |
label="Inference Result"
|
| 287 |
)
|
| 288 |
|
| 289 |
-
refresh_btn.click(
|
| 290 |
-
fn=refresh_models,
|
| 291 |
-
outputs=model_input,
|
| 292 |
-
)
|
| 293 |
-
|
| 294 |
predict_btn.click(
|
| 295 |
fn=predict,
|
| 296 |
inputs=[
|
|
@@ -301,22 +282,16 @@ with gr.Blocks(
|
|
| 301 |
outputs=predict_output,
|
| 302 |
)
|
| 303 |
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
demo.load(
|
| 309 |
-
fn=refresh_models,
|
| 310 |
-
outputs=model_input,
|
| 311 |
-
)
|
| 312 |
|
| 313 |
-
|
| 314 |
-
# Launch
|
| 315 |
-
# -----------------------------
|
| 316 |
|
| 317 |
if __name__ == "__main__":
|
| 318 |
|
| 319 |
demo.launch(
|
| 320 |
server_name="0.0.0.0",
|
| 321 |
-
server_port=7860
|
|
|
|
| 322 |
)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from gradio_client import Client, handle_file
|
| 4 |
|
| 5 |
+
# ---------------------------------------------------
|
| 6 |
+
# Environment
|
| 7 |
+
# ---------------------------------------------------
|
| 8 |
+
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
|
| 11 |
if not HF_TOKEN:
|
| 12 |
raise ValueError(
|
| 13 |
+
"HF_TOKEN environment variable not set."
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# ---------------------------------------------------
|
| 17 |
+
# Coldet Client
|
| 18 |
+
# ---------------------------------------------------
|
| 19 |
+
|
| 20 |
coldet_client = Client(
|
| 21 |
"Coldet/backend",
|
| 22 |
+
headers={
|
| 23 |
+
"Authorization": f"Bearer {HF_TOKEN}"
|
| 24 |
+
}
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# ---------------------------------------------------
|
| 28 |
+
# Static Model List
|
| 29 |
+
# (FASTEST METHOD)
|
| 30 |
+
# ---------------------------------------------------
|
| 31 |
+
|
| 32 |
+
AVAILABLE_MODELS = [
|
| 33 |
+
"coldet-v1-mini.coldet",
|
| 34 |
+
"coldet-v2-mini.coldet",
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
# ---------------------------------------------------
|
| 38 |
+
# API Functions
|
| 39 |
+
# ---------------------------------------------------
|
| 40 |
+
|
| 41 |
+
def create_account(username: str, password: str):
|
| 42 |
|
|
|
|
| 43 |
try:
|
| 44 |
result = coldet_client.predict(
|
| 45 |
username=username,
|
|
|
|
| 53 |
return f"Error: {str(e)}"
|
| 54 |
|
| 55 |
|
| 56 |
+
def login(username: str, password: str):
|
| 57 |
+
|
| 58 |
try:
|
| 59 |
result = coldet_client.predict(
|
| 60 |
username=username,
|
|
|
|
| 68 |
return f"Error: {str(e)}"
|
| 69 |
|
| 70 |
|
| 71 |
+
def create_token(session_id: str):
|
| 72 |
+
|
| 73 |
try:
|
| 74 |
result = coldet_client.predict(
|
| 75 |
session_id=session_id,
|
|
|
|
| 82 |
return f"Error: {str(e)}"
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
def predict(image, api_key: str, model: str):
|
| 86 |
|
| 87 |
if image is None:
|
| 88 |
+
return {
|
| 89 |
+
"error": "Please upload an image."
|
| 90 |
+
}
|
| 91 |
|
| 92 |
try:
|
| 93 |
result = coldet_client.predict(
|
|
|
|
| 100 |
return result
|
| 101 |
|
| 102 |
except Exception as e:
|
| 103 |
+
return {
|
| 104 |
+
"error": str(e)
|
| 105 |
+
}
|
| 106 |
|
| 107 |
+
# ---------------------------------------------------
|
| 108 |
# UI
|
| 109 |
+
# ---------------------------------------------------
|
| 110 |
|
| 111 |
with gr.Blocks(
|
| 112 |
title="Coldet Backend Interface"
|
| 113 |
) as demo:
|
| 114 |
|
| 115 |
gr.Markdown("# π Coldet Backend Interface")
|
| 116 |
+
|
| 117 |
gr.Markdown(
|
| 118 |
"Use this interface to interact with the Coldet backend API."
|
| 119 |
)
|
| 120 |
|
| 121 |
+
# ---------------------------------------------------
|
| 122 |
# Account Tab
|
| 123 |
+
# ---------------------------------------------------
|
| 124 |
|
| 125 |
with gr.Tab("π Account"):
|
| 126 |
|
|
|
|
| 128 |
|
| 129 |
with gr.Row():
|
| 130 |
|
| 131 |
+
# -----------------------------
|
| 132 |
# Create Account
|
| 133 |
+
# -----------------------------
|
| 134 |
+
|
| 135 |
with gr.Column():
|
| 136 |
|
| 137 |
gr.Markdown("#### Create Account")
|
|
|
|
| 147 |
type="password"
|
| 148 |
)
|
| 149 |
|
| 150 |
+
create_btn = gr.Button(
|
| 151 |
+
"Create Account"
|
| 152 |
+
)
|
| 153 |
|
| 154 |
create_output = gr.Textbox(
|
| 155 |
label="Session ID",
|
|
|
|
| 159 |
|
| 160 |
create_btn.click(
|
| 161 |
fn=create_account,
|
| 162 |
+
inputs=[
|
| 163 |
+
create_username,
|
| 164 |
+
create_password
|
| 165 |
+
],
|
| 166 |
outputs=create_output,
|
| 167 |
)
|
| 168 |
|
| 169 |
+
# -----------------------------
|
| 170 |
# Login
|
| 171 |
+
# -----------------------------
|
| 172 |
+
|
| 173 |
with gr.Column():
|
| 174 |
|
| 175 |
gr.Markdown("#### Login")
|
|
|
|
| 185 |
type="password"
|
| 186 |
)
|
| 187 |
|
| 188 |
+
login_btn = gr.Button(
|
| 189 |
+
"Login"
|
| 190 |
+
)
|
| 191 |
|
| 192 |
login_output = gr.Textbox(
|
| 193 |
label="Session ID",
|
|
|
|
| 197 |
|
| 198 |
login_btn.click(
|
| 199 |
fn=login,
|
| 200 |
+
inputs=[
|
| 201 |
+
login_username,
|
| 202 |
+
login_password
|
| 203 |
+
],
|
| 204 |
outputs=login_output,
|
| 205 |
)
|
| 206 |
|
| 207 |
+
# ---------------------------------------------------
|
| 208 |
# API Key Tab
|
| 209 |
+
# ---------------------------------------------------
|
| 210 |
|
| 211 |
with gr.Tab("π API Key"):
|
| 212 |
|
|
|
|
| 218 |
lines=2
|
| 219 |
)
|
| 220 |
|
| 221 |
+
token_btn = gr.Button(
|
| 222 |
+
"Generate API Key"
|
| 223 |
+
)
|
| 224 |
|
| 225 |
token_output = gr.Textbox(
|
| 226 |
label="API Key",
|
|
|
|
| 234 |
outputs=token_output,
|
| 235 |
)
|
| 236 |
|
| 237 |
+
# ---------------------------------------------------
|
| 238 |
# Inference Tab
|
| 239 |
+
# ---------------------------------------------------
|
| 240 |
|
| 241 |
with gr.Tab("π€ Inference"):
|
| 242 |
|
|
|
|
| 253 |
|
| 254 |
api_key_input = gr.Textbox(
|
| 255 |
label="API Key",
|
| 256 |
+
placeholder="Paste your API key here"
|
| 257 |
)
|
| 258 |
|
| 259 |
model_input = gr.Dropdown(
|
| 260 |
label="Model",
|
| 261 |
choices=AVAILABLE_MODELS,
|
| 262 |
+
value=AVAILABLE_MODELS[0],
|
|
|
|
|
|
|
| 263 |
)
|
| 264 |
|
| 265 |
+
predict_btn = gr.Button(
|
| 266 |
+
"Run Inference"
|
| 267 |
+
)
|
| 268 |
|
| 269 |
with gr.Column():
|
| 270 |
|
|
|
|
| 272 |
label="Inference Result"
|
| 273 |
)
|
| 274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
predict_btn.click(
|
| 276 |
fn=predict,
|
| 277 |
inputs=[
|
|
|
|
| 282 |
outputs=predict_output,
|
| 283 |
)
|
| 284 |
|
| 285 |
+
# ---------------------------------------------------
|
| 286 |
+
# Queue + Launch
|
| 287 |
+
# ---------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
+
demo.queue()
|
|
|
|
|
|
|
| 290 |
|
| 291 |
if __name__ == "__main__":
|
| 292 |
|
| 293 |
demo.launch(
|
| 294 |
server_name="0.0.0.0",
|
| 295 |
+
server_port=7860,
|
| 296 |
+
ssr_mode=False
|
| 297 |
)
|