import os import requests import json import dotenv from PIL import Image import io import base64 import numpy as np from gradio_client import Client # Load environment variables from .env file dotenv.load_dotenv() # Get Hugging Face token from environment hf_token = os.getenv("HF_TOKEN") # Create client for the Hugging Face Space with authentication client = Client("wjbmattingly/carcal-api", hf_token=hf_token) # Example usage if __name__ == "__main__": # Path to input image image_path = "test.jpg" # Check if image exists if not os.path.exists(image_path): print(f"Error: Image file not found: {image_path}") exit(1) # Print available API endpoints (for reference) print("Available API endpoints:") for endpoint in client.endpoints: print(f" - {endpoint}") print(f"\nProcessing image: {image_path}") # Convert the image to base64 with open(image_path, "rb") as image_file: image_bytes = image_file.read() # Convert to base64 string base64_image = base64.b64encode(image_bytes).decode("utf-8") # Format as expected by Gradio's ImageData model image_data = { "data": f"data:image/jpeg;base64,{base64_image}", "is_file": False } # Call the run_example function with the appropriate parameters result = client.predict( image_data, # Base64 encoded image "Qwen/Qwen2.5-VL-7B-Instruct", # Model selection False, # NER disabled "person, organization, location, date, event", # Default NER labels api_name="/run_example" # API endpoint name ) # Process and display the result if result: print("\nExtracted Text:") if isinstance(result, list): full_text = "" for segment in result: if isinstance(segment, list) and len(segment) > 0: text = segment[0] full_text += text print(text, end="") print("\n") else: print(result) else: print("No result returned from API")