Jump to content

Request to huggingface API in php


danx994

Recommended Posts

41 minutes ago, danx994 said:

Guys, can anyone help me write code for a request to the huggingface API in php?

Which API?

41 minutes ago, danx994 said:

I try everything in PHP but nothing works.

If you're having trouble making your PHP code work then you'll have to post (the relevant parts of) your PHP code. Because :psychic:

Link to comment
Share on other sites

12 hours ago, requinix said:

Which API?

If you're having trouble making your PHP code work then you'll have to post (the relevant parts of) your PHP code. Because :psychic:

$api_url = "https://choimirai-whisper-large-v3.hf.space/predict_2";

$data = array(
    'input_text' => 'https://www.youtube.com/watch?v=rotd-kaCX8c&t',
    'task_name' => 'transcribe',
    'return_timestamps' => true,
    'language' => 'English'
);

$ch = curl_init($api_url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);

curl_close($ch);

echo $response;

Link to comment
Share on other sites

1 hour ago, maxxd said:

OK, that looks like legitimate code that should work. What's the problem? What errors are you getting? What do you expect to happen, and what's actually happening? Help us help you.

This code outputs "{"detail":"Not Found"}", a similar request in Python works. 
I don't understand what the problem is with PHP.

Link to comment
Share on other sites

I don't think their API works like a traditional one where you just hit a URL.  They seem to open a websocket connection to a server and then issue requests to it for the API.  You would probably have to re-implement that in PHP, which would probably just have to be done by reverse engineering their code.  Searching packagist shows one package that might already do this: https://packagist.org/packages/sergix44/gradio-client-php.  You could try that package first.

Otherwise, the easiest thing to do might be to use either their python or JS packages to make a script that does what you need, then interface with that script using PHP.

Link to comment
Share on other sites

The problem is CORS when making the request with Python, it's executed on the user's machine, and the server interprets it as a regular user browsing the web. However, when attempting the same with PHP, CORS comes into play, blocking the request due to the security policy that restricts cross-origin requests. This restriction aims to prevent potential security vulnerabilities, as injecting custom code becomes a concern when making requests from different domains, run it like this.

$api_url = "https://cors-anywhere.herokuapp.com/https://choimirai-whisper-large-v3.hf.space/predict_2";

$data = array(
    'input_text' => 'https://www.youtube.com/watch?v=rotd-kaCX8c&t',
    'task_name' => 'transcribe',
    'return_timestamps' => true,
    'language' => 'English'
);

$ch = curl_init($api_url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);

curl_close($ch);

echo $response;

but before you do go here click https://cors-anywhere.herokuapp.com/corsdemo the button to give your ip temp access to do it, its a bummer but you could also use a cloud server setup python on it with this

 

from flask import Flask, jsonify, request
import requests

app = Flask(__name__)

@app.route('/transcribe', methods=['POST'])
def transcribe():
    api_url = "https://choimirai-whisper-large-v3.hf.space/predict_2"
    
    data = {
        'input_text': request.json['input_text'],
        'task_name': 'transcribe',
        'return_timestamps': True,
        'language': 'English'
    }

    response = requests.post(api_url, json=data)
    return jsonify(response.json())

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

this way you free up your machine, hope this helps!

Link to comment
Share on other sites

1 hour ago, jbezza123 said:

However, when attempting the same with PHP, CORS comes into play,

No, it does not.  PHP doesn't do CORS.  You can make whatever request you want to whatever destination you want with PHP.  CORS is something implemented by browsers to restrict JavaScript's ability to create requests, it has no affect on server-side requests like those made from PHP, Python, NodeJS, etc.

Link to comment
Share on other sites

34 minutes ago, kicken said:

No, it does not.  PHP doesn't do CORS.  You can make whatever request you want to whatever destination you want with PHP.  CORS is something implemented by browsers to restrict JavaScript's ability to create requests, it has no affect on server-side requests like those made from PHP, Python, NodeJS, etc.

oh dam yh your right!, not really sure why i wasn't thinking!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.