Android Question Hugging face curl to b4x ?

GJREDITOR

Member
I am trying to use hugging face inference API to get speed from the speedometer image. The curl command is:
curl command:
curl https://api-inference.huggingface.co/models/jinhybr/OCR-Donut-CORD \
    -X POST \
    --data-binary '@speed.png' \
    -H "Authorization: Bearer xxxxxJkTBOAKfDiyopHNNeRQPMyqxxxv"
Can someone convert the above to b4x post command?
Also is the following code to convert image to binary valid?
image to binary:
Dim img () As Byte
    img = File.ReadBytes(File.DirAssets, "speed.png")
Thank you
 
Solution
This should work:
B4X:
    Dim url As String = "https://api-inference.huggingface.co/models/jinhybr/OCR-Donut-CORD"
   
    Dim j As HttpJob : j.Initialize("",Me)
    j.PostBytes(url,ConvertImage2Binary(File.DirAssets,"dfsdsfdsf.JPG"))
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("Authorization","Bearer " & "")
   
    Wait For (j) JobDone(j As HttpJob)
   
    If j.Success Then
           
    Else

    End If

Alexander Stolte

Expert
Licensed User
Longtime User
Can someone convert the above to b4x post command?
Something like:
B4X:
    Dim url As String = "https://api-inference.huggingface.co/models/jinhybr/OCR-Donut-CORD"
    
    Dim json As JSONGenerator
    json.Initialize(CreateMap("data-binary":"speed.png"))
    
    Dim j As HttpJob : j.Initialize("",Me)
    j.PostString(url,json.ToString)
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("Authorization","Bearer " & AccessToken)
    
    Wait For (j) JobDone(j As HttpJob)
    
    If j.Success Then           
            
    Else

    End If
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
(A related wish.)
 
Upvote 0

GJREDITOR

Member
Something like: ..
Thank you It connects without problem but throws input error - probably because I have no idea how to pass on the "speed.png" image to the API. I tried these variations but all returned 'input error'.
B4X:
Dim img () As Byte
    img=Bit.InputStreamToBytes(File.OpenInput(File.DirAssets, "speed.png"))
json.Initialize(CreateMap("data-binary":img))

B4X:
Dim img () As Byte
    img=Bit.InputStreamToBytes(File.OpenInput(File.DirAssets, "speed.png"))
    Dim su As StringUtils
    Dim encoded As String
    encoded = su.EncodeBase64(img)
    json.Initialize(CreateMap("data-binary":encoded))
B4X:
Dim img () As Byte
    img = File.ReadBytes(File.DirAssets, "speed.png")
json.Initialize(CreateMap("data-binary":img))
B4X:
img= xui.LoadBitmap(File.DirAssets,"speed.png")
    json.Initialize(CreateMap("data-binary":img))
Any help in posting the image will be appreciated !
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
B4X:
    Dim url As String = "https://api-inference.huggingface.co/models/jinhybr/OCR-Donut-CORD"
    
    Dim json As JSONGenerator
    json.Initialize(CreateMap("data-binary":ConvertImage2Binary(File.DirAssets,"dfsdsfdsf.JPG")))
    
    Dim j As HttpJob : j.Initialize("",Me)
    j.PostString(url,json.ToString)
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("Authorization","Bearer " & "")
    
    Wait For (j) JobDone(j As HttpJob)
    
    If j.Success Then
            
    Else

    End If

B4X:
Sub ConvertImage2Binary(Dir As String, FileName As String) As Byte()
    Return Bit.InputStreamToBytes(File.OpenInput(Dir, FileName))
End Sub
 
Upvote 0

GJREDITOR

Member
Thanks for the code However I still get error:
ResponseError. Reason: Bad Request, Response: {"error":["Error in `inputs`: Invalid image: {'data-binary': [-119,... -126]}"]}
My B4X Code:
Hugging Face API Code:
private Sub QueryOCR
    Dim json As JSONGenerator
    json.Initialize(CreateMap("data-binary":ConvertImage2Binary(File.DirAssets,"speed.png")))
    Dim HttpClient As HttpJob
    HttpClient.Initialize("", Me)   
    HttpClient.PostString(API_URL,json.ToString)
    HttpClient.GetRequest.SetContentType("application/json")
    HttpClient.GetRequest.SetHeader("Authorization","Bearer "&AcessToken)
    Wait For (HttpClient) JobDone(HttpClient As HttpJob)
If HttpClient.Success Then
    'The result is a json string. We parse it and log the OCR txt.
    Dim jp As JSONParser
    jp.Initialize(HttpClient.GetString)
    Dim txtRes As List = jp.NextArray
    For Each res As Map In txtRes
    Log("Speed: " & res.Get("generated_text"))
    Next
    Else
        Log(HttpClient.ErrorMessage)
End If
HttpClient.Release
End Sub
Sub ConvertImage2Binary(Dir As String, FileName As String) As Byte()
    Return Bit.InputStreamToBytes(File.OpenInput(Dir, FileName))
End Sub
However this simple python code works . May be there is a clue here?
Hugging Face python Code:
import requests
import json

API_URL = "https://api-inference.huggingface.co/models/jinhybr/OCR-Donut-CORD"
headers = {"Authorization": "Bearer hf_XXXXXXXXXXMyqvbTYv"}

def query(filename):
    with open(filename, "rb") as f:
        data = f.read()
    response = requests.post(API_URL, headers=headers, data=data)
    return response.json()

output = query("speed.png")

output_json = json.dumps(output, indent=4)

# Print JSON output
print(output_json)
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
This should work:
B4X:
    Dim url As String = "https://api-inference.huggingface.co/models/jinhybr/OCR-Donut-CORD"
   
    Dim j As HttpJob : j.Initialize("",Me)
    j.PostBytes(url,ConvertImage2Binary(File.DirAssets,"dfsdsfdsf.JPG"))
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("Authorization","Bearer " & "")
   
    Wait For (j) JobDone(j As HttpJob)
   
    If j.Success Then
           
    Else

    End If
 
Last edited:
Upvote 1
Solution

GJREDITOR

Member
Thank you Works ! ! From server side there is 32 second delay sometimes and sometimes the models says 'still loading' but is good enough for free service. More models can be explored now
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…