theguywhosucks commited on
Commit
085ad5d
Β·
verified Β·
1 Parent(s): 76ca705

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -84
app.py CHANGED
@@ -1,97 +1,127 @@
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()
 
1
+ import gradio as gr
2
  from gradio_client import Client, handle_file
3
 
4
+ # Initialize the client for the Coldet backend
5
+ HF_TOKEN = "YOUR_HF_TOKEN" # Replace with your Hugging Face token
6
+ coldet_client = Client("Coldet/backend", hf_token=HF_TOKEN)
 
 
 
 
 
7
 
8
+ # --- Helper Functions ---
 
 
 
 
 
 
 
9
 
10
+ def create_account(username: str, password: str) -> str:
11
+ """Create a new account and return the session ID."""
12
+ try:
13
+ result = coldet_client.predict(
14
+ username=username,
15
+ password=password,
16
+ api_name="/create_account",
17
+ )
18
+ return f"Account created! Session ID: {result}"
19
+ except Exception as e:
20
+ return f"Error: {str(e)}"
21
+
22
+ def login(username: str, password: str) -> str:
23
+ """Login and return the session ID."""
24
+ try:
25
+ result = coldet_client.predict(
26
+ username=username,
27
+ password=password,
28
+ api_name="/login",
29
+ )
30
+ return f"Logged in! Session ID: {result}"
31
+ except Exception as e:
32
+ return f"Error: {str(e)}"
33
+
34
+ def create_token(session_id: str) -> str:
35
  """Generate an API key for the session."""
36
+ try:
37
+ result = coldet_client.predict(
38
+ session_id=session_id,
39
+ api_name="/create_token",
40
+ )
41
+ return f"API Key: {result}"
42
+ except Exception as e:
43
+ return f"Error: {str(e)}"
44
+
45
+ def predict(image, api_key: str, model: str = "coldet-v1-mini.coldet") -> str:
46
+ """Run inference on the provided image."""
47
+ try:
48
+ result = coldet_client.predict(
49
+ image=handle_file(image),
50
+ api_key=api_key,
51
+ model=model,
52
+ api_name="/predict",
53
+ )
54
+ return f"Inference Result: {result}"
55
+ except Exception as e:
56
+ return f"Error: {str(e)}"
57
 
58
+ # --- Gradio Interface ---
 
 
 
 
 
 
 
 
59
 
60
+ with gr.Blocks(title="Coldet Backend Interface", theme=gr.themes.Soft()) as demo:
61
+ gr.Markdown("# πŸš€ Coldet Backend Interface")
62
+ gr.Markdown("Use this interface to interact with the Coldet backend API.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
+ # --- Account Tab ---
65
+ with gr.Tab("πŸ”‘ Account"):
66
+ gr.Markdown("### Create Account or Login")
67
+ with gr.Row():
68
+ with gr.Column():
69
+ gr.Markdown("#### Create Account")
70
+ create_username = gr.Textbox(label="Username", placeholder="Enter username")
71
+ create_password = gr.Textbox(label="Password", placeholder="Enter password", type="password")
72
+ create_btn = gr.Button("Create Account")
73
+ create_output = gr.Textbox(label="Session ID", interactive=False)
74
+ create_btn.click(
75
+ fn=create_account,
76
+ inputs=[create_username, create_password],
77
+ outputs=create_output,
78
+ )
79
 
80
+ with gr.Column():
81
+ gr.Markdown("#### Login")
82
+ login_username = gr.Textbox(label="Username", placeholder="Enter username")
83
+ login_password = gr.Textbox(label="Password", placeholder="Enter password", type="password")
84
+ login_btn = gr.Button("Login")
85
+ login_output = gr.Textbox(label="Session ID", interactive=False)
86
+ login_btn.click(
87
+ fn=login,
88
+ inputs=[login_username, login_password],
89
+ outputs=login_output,
90
+ )
91
 
92
+ # --- API Key Tab ---
93
+ with gr.Tab("πŸ” API Key"):
94
+ gr.Markdown("### Generate API Key")
95
+ session_id_input = gr.Textbox(label="Session ID", placeholder="Paste your session ID here")
96
+ token_btn = gr.Button("Generate API Key")
97
+ token_output = gr.Textbox(label="API Key", interactive=False)
98
+ token_btn.click(
99
+ fn=create_token,
100
+ inputs=session_id_input,
101
+ outputs=token_output,
102
+ )
103
 
104
+ # --- Inference Tab ---
105
+ with gr.Tab("πŸ€– Inference"):
106
+ gr.Markdown("### Run Inference")
107
+ with gr.Row():
108
+ with gr.Column():
109
+ image_input = gr.Image(label="Upload Image", type="filepath")
110
+ api_key_input = gr.Textbox(label="API Key", placeholder="Paste your API key here")
111
+ model_input = gr.Dropdown(
112
+ label="Model",
113
+ choices=["coldet-v1-mini.coldet"],
114
+ value="coldet-v1-mini.coldet",
115
+ )
116
+ predict_btn = gr.Button("Run Inference")
117
+ with gr.Column():
118
+ predict_output = gr.JSON(label="Inference Result")
119
+ predict_btn.click(
120
+ fn=predict,
121
+ inputs=[image_input, api_key_input, model_input],
122
+ outputs=predict_output,
123
+ )
124
 
125
+ # Launch the app
126
  if __name__ == "__main__":
127
+ demo.launch()