File size: 2,205 Bytes
1596eba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fdca7c
1596eba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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")