theguywhosucks commited on
Commit
76ca705
·
verified ·
1 Parent(s): 164107f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -52
app.py CHANGED
@@ -1,55 +1,97 @@
1
- import os
2
- import gradio as gr
3
  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("HF_TOKEN environment variable not set. Please set it before running the app.")
9
-
10
- # Initialize the client for the Coldet backend with HF_TOKEN in headers
11
- coldet_client = Client("Coldet/backend", headers={"Authorization": f"Bearer {HF_TOKEN}"})
12
-
13
- # --- Inference Function ---
14
-
15
- def predict(image, api_key: str, model: str = "coldet-v1-mini.coldet") -> str:
16
- """Run inference on the provided image."""
17
- try:
18
- result = coldet_client.predict(
19
- image=handle_file(image),
20
- api_key=api_key,
21
- model=model,
22
- api_name="/predict",
23
- )
24
- return result
25
- except Exception as e:
26
- return f"Error: {str(e)}"
27
-
28
- # --- Gradio Interface ---
29
-
30
- with gr.Blocks(title="Coldet Inference", theme=gr.themes.Soft()) as demo:
31
- gr.Markdown("# 🤖 Coldet Inference")
32
- gr.Markdown("Upload an image and paste your API key to run inference.")
33
-
34
- with gr.Row():
35
- with gr.Column():
36
- image_input = gr.Image(label="Upload Image", type="filepath")
37
- api_key_input = gr.Textbox(label="API Key", placeholder="Paste your API key here")
38
- model_input = gr.Dropdown(
39
- label="Model",
40
- choices=["coldet-v1-mini.coldet"],
41
- value="coldet-v1-mini.coldet",
42
- )
43
- predict_btn = gr.Button("Run Inference", variant="primary")
44
- with gr.Column():
45
- predict_output = gr.JSON(label="Inference Result")
46
-
47
- predict_btn.click(
48
- fn=predict,
49
- inputs=[image_input, api_key_input, model_input],
50
- outputs=predict_output,
51
- )
52
-
53
- # Launch the app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  if __name__ == "__main__":
55
- demo.launch()
 
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()