Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,97 @@
|
|
| 1 |
-
import
|
| 2 |
-
import gradio as gr
|
| 3 |
from gradio_client import Client, handle_file
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if __name__ == "__main__":
|
| 55 |
-
|
|
|
|
| 1 |
+
import argparse
|
|
|
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
|
| 4 |
+
def create_account(client, username, password):
|
| 5 |
+
"""Create a new account."""
|
| 6 |
+
result = client.predict(
|
| 7 |
+
username=username,
|
| 8 |
+
password=password,
|
| 9 |
+
api_name="/create_account",
|
| 10 |
+
)
|
| 11 |
+
return result
|
| 12 |
+
|
| 13 |
+
def login(client, username, password):
|
| 14 |
+
"""Login to an existing account."""
|
| 15 |
+
result = client.predict(
|
| 16 |
+
username=username,
|
| 17 |
+
password=password,
|
| 18 |
+
api_name="/login",
|
| 19 |
+
)
|
| 20 |
+
return result
|
| 21 |
+
|
| 22 |
+
def create_token(client, session_id):
|
| 23 |
+
"""Generate an API key for the session."""
|
| 24 |
+
result = client.predict(
|
| 25 |
+
session_id=session_id,
|
| 26 |
+
api_name="/create_token",
|
| 27 |
+
)
|
| 28 |
+
return result
|
| 29 |
+
|
| 30 |
+
def predict(client, image_path, api_key, model="coldet-v1-mini.coldet"):
|
| 31 |
+
"""Run inference on an image."""
|
| 32 |
+
result = client.predict(
|
| 33 |
+
image=handle_file(image_path),
|
| 34 |
+
api_key=api_key,
|
| 35 |
+
model=model,
|
| 36 |
+
api_name="/predict",
|
| 37 |
+
)
|
| 38 |
+
return result
|
| 39 |
+
|
| 40 |
+
def main():
|
| 41 |
+
parser = argparse.ArgumentParser(description="Coldet CLI Tool")
|
| 42 |
+
parser.add_argument(
|
| 43 |
+
"--image",
|
| 44 |
+
type=str,
|
| 45 |
+
help="Path or URL to the image for inference",
|
| 46 |
+
)
|
| 47 |
+
parser.add_argument(
|
| 48 |
+
"--username",
|
| 49 |
+
type=str,
|
| 50 |
+
help="Username for login or account creation",
|
| 51 |
+
)
|
| 52 |
+
parser.add_argument(
|
| 53 |
+
"--password",
|
| 54 |
+
type=str,
|
| 55 |
+
help="Password for login or account creation",
|
| 56 |
+
)
|
| 57 |
+
parser.add_argument(
|
| 58 |
+
"--session-id",
|
| 59 |
+
type=str,
|
| 60 |
+
help="Session ID for generating API key",
|
| 61 |
+
)
|
| 62 |
+
parser.add_argument(
|
| 63 |
+
"--api-key",
|
| 64 |
+
type=str,
|
| 65 |
+
help="API key for inference",
|
| 66 |
+
)
|
| 67 |
+
args = parser.parse_args()
|
| 68 |
+
|
| 69 |
+
client = Client("Coldet/backend")
|
| 70 |
+
|
| 71 |
+
# Step 1: Login or Create Account
|
| 72 |
+
if args.username and args.password:
|
| 73 |
+
session_id = login(client, args.username, args.password)
|
| 74 |
+
print(f"Logged in! Session ID: {session_id}")
|
| 75 |
+
else:
|
| 76 |
+
print("No username/password provided. Skipping login.")
|
| 77 |
+
session_id = None
|
| 78 |
+
|
| 79 |
+
# Step 2: Generate API Key (if session_id is provided)
|
| 80 |
+
if args.session_id or session_id:
|
| 81 |
+
session_id = args.session_id or session_id
|
| 82 |
+
api_key = create_token(client, session_id)
|
| 83 |
+
print(f"API Key generated: {api_key}")
|
| 84 |
+
else:
|
| 85 |
+
print("No session ID provided. Skipping API key generation.")
|
| 86 |
+
api_key = None
|
| 87 |
+
|
| 88 |
+
# Step 3: Run Inference (if image and api_key are provided)
|
| 89 |
+
if args.image and (args.api_key or api_key):
|
| 90 |
+
api_key = args.api_key or api_key
|
| 91 |
+
result = predict(client, args.image, api_key)
|
| 92 |
+
print(f"Inference result: {result}")
|
| 93 |
+
else:
|
| 94 |
+
print("Missing image or API key. Cannot run inference.")
|
| 95 |
+
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
main()
|