danx994 Posted November 17, 2023 Share Posted November 17, 2023 Hi all! Guys, can anyone help me write code for a request to the huggingface API in php? For example on:https://huggingface.co/spaces/choimirai/whisper-large-v3 I try everything in PHP but nothing works. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/317458-request-to-huggingface-api-in-php/ Share on other sites More sharing options...
requinix Posted November 17, 2023 Share Posted November 17, 2023 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 Quote Link to comment https://forums.phpfreaks.com/topic/317458-request-to-huggingface-api-in-php/#findComment-1612955 Share on other sites More sharing options...
danx994 Posted November 18, 2023 Author Share Posted November 18, 2023 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 $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; Quote Link to comment https://forums.phpfreaks.com/topic/317458-request-to-huggingface-api-in-php/#findComment-1612969 Share on other sites More sharing options...
maxxd Posted November 18, 2023 Share Posted November 18, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317458-request-to-huggingface-api-in-php/#findComment-1612973 Share on other sites More sharing options...
danx994 Posted November 18, 2023 Author Share Posted November 18, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317458-request-to-huggingface-api-in-php/#findComment-1612975 Share on other sites More sharing options...
kicken Posted November 18, 2023 Share Posted November 18, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317458-request-to-huggingface-api-in-php/#findComment-1612978 Share on other sites More sharing options...
jbezza123 Posted November 19, 2023 Share Posted November 19, 2023 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! Quote Link to comment https://forums.phpfreaks.com/topic/317458-request-to-huggingface-api-in-php/#findComment-1612989 Share on other sites More sharing options...
kicken Posted November 19, 2023 Share Posted November 19, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317458-request-to-huggingface-api-in-php/#findComment-1612992 Share on other sites More sharing options...
jbezza123 Posted November 20, 2023 Share Posted November 20, 2023 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! Quote Link to comment https://forums.phpfreaks.com/topic/317458-request-to-huggingface-api-in-php/#findComment-1612995 Share on other sites More sharing options...
phpiodore Posted November 26, 2023 Share Posted November 26, 2023 On 11/20/2023 at 4:09 AM, jbezza123 said: not really sure why i wasn't thinking me every single day aha also I'm glad the issue was resolved! I was about to start typing ahaha Quote Link to comment https://forums.phpfreaks.com/topic/317458-request-to-huggingface-api-in-php/#findComment-1613119 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.