Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,127 @@
|
|
| 1 |
-
import
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
username=username,
|
| 8 |
-
password=password,
|
| 9 |
-
api_name="/create_account",
|
| 10 |
-
)
|
| 11 |
-
return result
|
| 12 |
|
| 13 |
-
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""Generate an API key for the session."""
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 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 |
-
|
| 41 |
-
|
| 42 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
|
|
|
| 96 |
if __name__ == "__main__":
|
| 97 |
-
|
|
|
|
| 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()
|