CU1-X / examples /api_example.py
Abdelkader HASSINE
Deploy CU1-X to Hugging Face Spaces
ff03012
raw
history blame
3.14 kB
"""
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()