Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,21 @@ from gradio_client import Client, handle_file
|
|
| 4 |
|
| 5 |
# Fetch HF_TOKEN from environment variables
|
| 6 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 7 |
if not HF_TOKEN:
|
| 8 |
-
raise ValueError(
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Initialize
|
| 11 |
coldet_client = Client(
|
| 12 |
"Coldet/backend",
|
| 13 |
headers={"Authorization": f"Bearer {HF_TOKEN}"}
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# ---
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def create_account(username: str, password: str) -> str:
|
| 19 |
try:
|
|
@@ -22,7 +27,9 @@ def create_account(username: str, password: str) -> str:
|
|
| 22 |
password=password,
|
| 23 |
api_name="/create_account",
|
| 24 |
)
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
return f"Error: {str(e)}"
|
| 28 |
|
|
@@ -34,7 +41,9 @@ def login(username: str, password: str) -> str:
|
|
| 34 |
password=password,
|
| 35 |
api_name="/login",
|
| 36 |
)
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"Error: {str(e)}"
|
| 40 |
|
|
@@ -45,12 +54,17 @@ def create_token(session_id: str) -> str:
|
|
| 45 |
session_id=session_id,
|
| 46 |
api_name="/create_token",
|
| 47 |
)
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
return f"Error: {str(e)}"
|
| 51 |
|
| 52 |
|
| 53 |
-
#
|
|
|
|
|
|
|
|
|
|
| 54 |
def get_models():
|
| 55 |
try:
|
| 56 |
config = coldet_client.view_api(return_format="dict")
|
|
@@ -61,22 +75,26 @@ def get_models():
|
|
| 61 |
|
| 62 |
for param in params:
|
| 63 |
if param.get("parameter_name") == "model":
|
| 64 |
-
choices = param.get("python_type", "")
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
models = (
|
| 69 |
-
|
|
|
|
| 70 |
.replace("]", "")
|
| 71 |
.replace("'", "")
|
| 72 |
.split(", ")
|
| 73 |
)
|
|
|
|
| 74 |
return models
|
| 75 |
|
| 76 |
return ["coldet-v1-mini.coldet"]
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
print(f"Failed to fetch models: {e}")
|
|
|
|
| 80 |
return ["coldet-v1-mini.coldet"]
|
| 81 |
|
| 82 |
|
|
@@ -94,7 +112,15 @@ def refresh_models():
|
|
| 94 |
)
|
| 95 |
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
def predict(image, api_key: str, model: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
try:
|
| 99 |
result = coldet_client.predict(
|
| 100 |
image=handle_file(image),
|
|
@@ -102,28 +128,37 @@ def predict(image, api_key: str, model: str):
|
|
| 102 |
model=model,
|
| 103 |
api_name="/predict",
|
| 104 |
)
|
|
|
|
| 105 |
return result
|
|
|
|
| 106 |
except Exception as e:
|
| 107 |
return {"error": str(e)}
|
| 108 |
|
| 109 |
|
| 110 |
-
# ---
|
|
|
|
|
|
|
| 111 |
|
| 112 |
with gr.Blocks(
|
| 113 |
-
title="Coldet Backend Interface"
|
| 114 |
-
theme=gr.themes.Soft(),
|
| 115 |
) as demo:
|
| 116 |
|
| 117 |
gr.Markdown("# π Coldet Backend Interface")
|
| 118 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
# --- Account Tab ---
|
| 121 |
with gr.Tab("π Account"):
|
| 122 |
|
| 123 |
gr.Markdown("### Create Account or Login")
|
| 124 |
|
| 125 |
with gr.Row():
|
| 126 |
|
|
|
|
| 127 |
with gr.Column():
|
| 128 |
|
| 129 |
gr.Markdown("#### Create Account")
|
|
@@ -143,7 +178,8 @@ with gr.Blocks(
|
|
| 143 |
|
| 144 |
create_output = gr.Textbox(
|
| 145 |
label="Session ID",
|
| 146 |
-
interactive=False
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
create_btn.click(
|
|
@@ -152,6 +188,7 @@ with gr.Blocks(
|
|
| 152 |
outputs=create_output,
|
| 153 |
)
|
| 154 |
|
|
|
|
| 155 |
with gr.Column():
|
| 156 |
|
| 157 |
gr.Markdown("#### Login")
|
|
@@ -171,7 +208,8 @@ with gr.Blocks(
|
|
| 171 |
|
| 172 |
login_output = gr.Textbox(
|
| 173 |
label="Session ID",
|
| 174 |
-
interactive=False
|
|
|
|
| 175 |
)
|
| 176 |
|
| 177 |
login_btn.click(
|
|
@@ -180,21 +218,26 @@ with gr.Blocks(
|
|
| 180 |
outputs=login_output,
|
| 181 |
)
|
| 182 |
|
| 183 |
-
# ---
|
|
|
|
|
|
|
|
|
|
| 184 |
with gr.Tab("π API Key"):
|
| 185 |
|
| 186 |
gr.Markdown("### Generate API Key")
|
| 187 |
|
| 188 |
session_id_input = gr.Textbox(
|
| 189 |
label="Session ID",
|
| 190 |
-
placeholder="Paste your session ID here"
|
|
|
|
| 191 |
)
|
| 192 |
|
| 193 |
token_btn = gr.Button("Generate API Key")
|
| 194 |
|
| 195 |
token_output = gr.Textbox(
|
| 196 |
label="API Key",
|
| 197 |
-
interactive=False
|
|
|
|
| 198 |
)
|
| 199 |
|
| 200 |
token_btn.click(
|
|
@@ -203,7 +246,10 @@ with gr.Blocks(
|
|
| 203 |
outputs=token_output,
|
| 204 |
)
|
| 205 |
|
| 206 |
-
# ---
|
|
|
|
|
|
|
|
|
|
| 207 |
with gr.Tab("π€ Inference"):
|
| 208 |
|
| 209 |
gr.Markdown("### Run Inference")
|
|
@@ -219,13 +265,15 @@ with gr.Blocks(
|
|
| 219 |
|
| 220 |
api_key_input = gr.Textbox(
|
| 221 |
label="API Key",
|
| 222 |
-
placeholder="Paste your API key here"
|
| 223 |
)
|
| 224 |
|
| 225 |
model_input = gr.Dropdown(
|
| 226 |
label="Model",
|
| 227 |
choices=AVAILABLE_MODELS,
|
| 228 |
-
value=AVAILABLE_MODELS[0]
|
|
|
|
|
|
|
| 229 |
)
|
| 230 |
|
| 231 |
refresh_btn = gr.Button("π Refresh Models")
|
|
@@ -245,16 +293,30 @@ with gr.Blocks(
|
|
| 245 |
|
| 246 |
predict_btn.click(
|
| 247 |
fn=predict,
|
| 248 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
outputs=predict_output,
|
| 250 |
)
|
| 251 |
|
| 252 |
-
#
|
|
|
|
|
|
|
|
|
|
| 253 |
demo.load(
|
| 254 |
fn=refresh_models,
|
| 255 |
outputs=model_input,
|
| 256 |
)
|
| 257 |
|
| 258 |
-
#
|
|
|
|
|
|
|
|
|
|
| 259 |
if __name__ == "__main__":
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Fetch HF_TOKEN from environment variables
|
| 6 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 7 |
+
|
| 8 |
if not HF_TOKEN:
|
| 9 |
+
raise ValueError(
|
| 10 |
+
"HF_TOKEN environment variable not set. Please set it before running the app."
|
| 11 |
+
)
|
| 12 |
|
| 13 |
+
# Initialize Coldet backend client
|
| 14 |
coldet_client = Client(
|
| 15 |
"Coldet/backend",
|
| 16 |
headers={"Authorization": f"Bearer {HF_TOKEN}"}
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# -----------------------------
|
| 20 |
+
# Helper Functions
|
| 21 |
+
# -----------------------------
|
| 22 |
|
| 23 |
def create_account(username: str, password: str) -> str:
|
| 24 |
try:
|
|
|
|
| 27 |
password=password,
|
| 28 |
api_name="/create_account",
|
| 29 |
)
|
| 30 |
+
|
| 31 |
+
return f"Account created!\n\nSession ID:\n{result}"
|
| 32 |
+
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error: {str(e)}"
|
| 35 |
|
|
|
|
| 41 |
password=password,
|
| 42 |
api_name="/login",
|
| 43 |
)
|
| 44 |
+
|
| 45 |
+
return f"Logged in!\n\nSession ID:\n{result}"
|
| 46 |
+
|
| 47 |
except Exception as e:
|
| 48 |
return f"Error: {str(e)}"
|
| 49 |
|
|
|
|
| 54 |
session_id=session_id,
|
| 55 |
api_name="/create_token",
|
| 56 |
)
|
| 57 |
+
|
| 58 |
+
return f"API Key:\n{result}"
|
| 59 |
+
|
| 60 |
except Exception as e:
|
| 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")
|
|
|
|
| 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 |
|
|
|
|
| 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 {"error": "Please upload an image."}
|
| 123 |
+
|
| 124 |
try:
|
| 125 |
result = coldet_client.predict(
|
| 126 |
image=handle_file(image),
|
|
|
|
| 128 |
model=model,
|
| 129 |
api_name="/predict",
|
| 130 |
)
|
| 131 |
+
|
| 132 |
return result
|
| 133 |
+
|
| 134 |
except Exception as e:
|
| 135 |
return {"error": str(e)}
|
| 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 |
|
| 157 |
gr.Markdown("### Create Account or Login")
|
| 158 |
|
| 159 |
with gr.Row():
|
| 160 |
|
| 161 |
+
# Create Account
|
| 162 |
with gr.Column():
|
| 163 |
|
| 164 |
gr.Markdown("#### Create Account")
|
|
|
|
| 178 |
|
| 179 |
create_output = gr.Textbox(
|
| 180 |
label="Session ID",
|
| 181 |
+
interactive=False,
|
| 182 |
+
lines=4
|
| 183 |
)
|
| 184 |
|
| 185 |
create_btn.click(
|
|
|
|
| 188 |
outputs=create_output,
|
| 189 |
)
|
| 190 |
|
| 191 |
+
# Login
|
| 192 |
with gr.Column():
|
| 193 |
|
| 194 |
gr.Markdown("#### Login")
|
|
|
|
| 208 |
|
| 209 |
login_output = gr.Textbox(
|
| 210 |
label="Session ID",
|
| 211 |
+
interactive=False,
|
| 212 |
+
lines=4
|
| 213 |
)
|
| 214 |
|
| 215 |
login_btn.click(
|
|
|
|
| 218 |
outputs=login_output,
|
| 219 |
)
|
| 220 |
|
| 221 |
+
# -----------------------------
|
| 222 |
+
# API Key Tab
|
| 223 |
+
# -----------------------------
|
| 224 |
+
|
| 225 |
with gr.Tab("π API Key"):
|
| 226 |
|
| 227 |
gr.Markdown("### Generate API Key")
|
| 228 |
|
| 229 |
session_id_input = gr.Textbox(
|
| 230 |
label="Session ID",
|
| 231 |
+
placeholder="Paste your session ID here",
|
| 232 |
+
lines=2
|
| 233 |
)
|
| 234 |
|
| 235 |
token_btn = gr.Button("Generate API Key")
|
| 236 |
|
| 237 |
token_output = gr.Textbox(
|
| 238 |
label="API Key",
|
| 239 |
+
interactive=False,
|
| 240 |
+
lines=3
|
| 241 |
)
|
| 242 |
|
| 243 |
token_btn.click(
|
|
|
|
| 246 |
outputs=token_output,
|
| 247 |
)
|
| 248 |
|
| 249 |
+
# -----------------------------
|
| 250 |
+
# Inference Tab
|
| 251 |
+
# -----------------------------
|
| 252 |
+
|
| 253 |
with gr.Tab("π€ Inference"):
|
| 254 |
|
| 255 |
gr.Markdown("### Run Inference")
|
|
|
|
| 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 |
refresh_btn = gr.Button("π Refresh Models")
|
|
|
|
| 293 |
|
| 294 |
predict_btn.click(
|
| 295 |
fn=predict,
|
| 296 |
+
inputs=[
|
| 297 |
+
image_input,
|
| 298 |
+
api_key_input,
|
| 299 |
+
model_input
|
| 300 |
+
],
|
| 301 |
outputs=predict_output,
|
| 302 |
)
|
| 303 |
|
| 304 |
+
# -----------------------------
|
| 305 |
+
# Auto Refresh On Load
|
| 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 |
+
)
|