File size: 1,478 Bytes
311b3f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
import gradio as gr
from gradio_client import Client
import os

# Configuration
HF_SPACE_ID = "YOUR_USERNAME/Arch"  # Update this with your actual Space ID

def inspect_part_remote(image, threshold):
    """
    Client function to call the Hugging Face Space
    """
    if image is None:
        return None, "Please provide an image"
    
    try:
        # Initialize the client
        client = Client(HF_SPACE_ID)
        
        # Call the 'detect' endpoint
        result = client.predict(
            image=image,
            threshold=threshold,
            api_name="/detect"
        )
        
        # result[0] is the formatted text, result[1] is the matched label
        return result[1], result[0]
    except Exception as e:
        return None, f"Error: {str(e)}"

# Standalone testing interface
with gr.Blocks(title="HF Model Client") as demo:
    gr.Markdown(f"# 🔗 TMTL Hugging Face Connector\nConnecting to: `{HF_SPACE_ID}`")
    
    with gr.Row():
        with gr.Column():
            img_input = gr.Image(type="pil")
            threshold = gr.Slider(0.5, 0.99, value=0.7, label="Threshold")
            btn = gr.Button("Analyze Remote")
        with gr.Column():
            label_output = gr.Label(label="Matched Part")
            text_output = gr.Markdown()
    
    btn.click(
        fn=inspect_part_remote,
        inputs=[img_input, threshold],
        outputs=[label_output, text_output]
    )

if __name__ == "__main__":
    demo.launch()