File size: 3,142 Bytes
ff03012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
"""
Example: Using CU1-X API from Hugging Face Space

This example shows how to call the CU1-X API deployed on Hugging Face Spaces.
"""

from gradio_client import Client
import json

# Configuration
SPACE_URL = "AI-DrivenTesting/CU1-X"  # Remplacez par votre Space URL

def detect_ui_elements(image_path: str):
    """
    Détecte les éléments UI dans une image via l'API HF Space
    
    Args:
        image_path: Chemin vers l'image à analyser
        
    Returns:
        Tuple (annotated_image, summary, detections_json)
    """
    # Créer le client Gradio
    client = Client(SPACE_URL)
    
    # Appeler l'API
    result = client.predict(
        image_path,                          # image
        0.35,                                # confidence_threshold
        2,                                   # thickness
        True,                                # enable_clip (classification)
        True,                                # enable_ocr (extraction texte)
        False,                               # enable_blip (descriptions)
        False,                               # ocr_only
        "Only image & button",              # blip_scope
        False,                               # preprocess
        "RF-DETR Optimized (Recommended)",  # preprocess_mode
        "standard",                          # preprocess_preset
        api_name="/predict"
    )
    
    # Déballer les résultats
    annotated_image, summary, detections_json = result
    
    return annotated_image, summary, detections_json


def main():
    """Exemple d'utilisation"""
    
    print("🚀 CU1-X API Example")
    print("=" * 50)
    
    # Chemin vers votre image de test
    test_image = "screenshot.png"  # Remplacez par votre image
    
    try:
        print(f"\n📤 Uploading image: {test_image}")
        print("⏳ Processing... (this may take 30-60 seconds)")
        
        # Appeler l'API
        annotated_image, summary, detections = detect_ui_elements(test_image)
        
        # Afficher les résultats
        print("\n✅ Detection completed!")
        print("\n📊 Summary:")
        print(summary)
        
        print("\n🔍 Detections:")
        if isinstance(detections, str):
            detections = json.loads(detections)
        
        print(f"   Total: {detections.get('total_detections', 0)} elements")
        
        if 'type_distribution' in detections:
            print("\n📈 Type Distribution:")
            for elem_type, count in detections['type_distribution'].items():
                print(f"   {elem_type}: {count}")
        
        print("\n💾 Saving annotated image...")
        # annotated_image est un fichier temporaire, vous pouvez le copier
        print(f"   Annotated image saved at: {annotated_image}")
        
    except Exception as e:
        print(f"\n❌ Error: {e}")
        print("\nTroubleshooting:")
        print("1. Vérifiez que votre Space est déployé et en ligne")
        print("2. Vérifiez que SPACE_URL est correct")
        print("3. Assurez-vous d'avoir installé: pip install gradio_client")


if __name__ == "__main__":
    main()