Calling Python script from PHP does not pan out - php

I am trying to extract major keywords from a text or sentence using python. I am using python's RAKE module. The following python code works well in the console. But, when I am trying to call the python script from PHP, the script does not parse any new text or sentence which I stored in a php variable, and instead outputs the old text/sentence even though I commented out it within the python script and replaces it with sys.argv[1] argument. In various ways, within PHP I tried to solve this problem using PHP's exec and passthru commands without any luck and so I finally decided to post my problem here.
PHP script
$var1 = 'The extra sleep will help your body wash out stress hormones.';
Technique(1)
$output = exec("python rake_tutorial.py .$var1");
Technique(2)
$output = exec('python rake_tutorial.py ' .$var1, $result);
Technique(3)
$command = 'python rake_tutorial.py ' . $var1;
$output = passthru($command);
Technique(4)
$output = exec("python rake_tutorial.py $var1", $result);
echo '<pre>' . print_r($result, true);
Here is my Python code
__author__ = 'a_medelyan'
import rake
import operator
import sys
# EXAMPLE ONE - SIMPLE
stoppath = "SmartStoplist.txt"
# EXAMPLE TWO - BEHIND THE SCENES (from https://github.com/aneesha/RAKE/rake.py)
# 1. initialize RAKE by providing a path to a stopwords file
rake_object = rake.Rake(stoppath)
# text = "What you use depends on your baby's age and physical development."
# 1. Split text into sentences
sentenceList = rake.split_sentences(text)
# generate candidate keywords
stopwordpattern = rake.build_stop_word_regex(stoppath)
phraseList = rake.generate_candidate_keywords(sentenceList, stopwordpattern)
print "Phrases:", phraseList
# calculate individual word scores
wordscores = rake.calculate_word_scores(phraseList)
# generate candidate keyword scores
keywordcandidates = rake.generate_candidate_keyword_scores(phraseList, wordscores)
for candidate in keywordcandidates.keys():
print "Candidate: ", candidate, ", score: ", keywordcandidates.get(candidate)
# sort candidates by score to determine top-scoring keywords
sortedKeywords = sorted(keywordcandidates.iteritems(), key=operator.itemgetter(1), reverse=True)
totalKeywords = len(sortedKeywords)
# for example, you could just take the top third as the final keywords
for keyword in sortedKeywords[0:(totalKeywords / 3)]:
print "Keyword: ", keyword[0], ", score: ", keyword[1]
print rake_object.run(sys.argv[1])
sys.stdout.flush()
# print rake_object.run(text)

Looks like your exec command is going wrong.Try the following command:
$var1 = 'The extra sleep will help your body wash out stress hormones.';
$output = exec("python rake.py '".$var1."'");
Hope this helps!

Just thought I'd mention that there is a RAKE implementation in php https://github.com/artofzen/RAKE-PHP

Related

html not printing out output text

So i have two codes, one of them is just a simple code that i used to test out some php functions. I'm trying to print out the JSON string into my html, one of the codes is working but the other isn't.
So let's say that we have two sets of codes : first.php & first.py , second.php & second.py
first.php looks like this:
<?php
exec("C:/Users/hln/Anaconda3/envs/tensorflow1/python.exe C:/tensorflow1/models/research/object_detection/first.py", $output);
$someOutput = json_decode($output[0], true);
echo "<h3>" . $someOutput['rightCoordinate'] ."</h3>";
echo "<h3>" . $someOutput['leftCoordinate'] ."</h3>";?>
first.py looks like:
import json
a = 1 + 3
b = 5 + 5
x = {
"leftCoordinate": a,
"rightCoordinate": b
}
y = json.dumps(x)
print(y)
print()
second.php looks like this:
<?php
exec("C:/Users/hln/Anaconda3/envs/tensorflow1/python.exe C:/tensorflow1/models/research/object_detection/second.py C:/xampp/htdocs/w3layout/finalproject/uploads/10.PNG 10.PNG 2>&1",$output);
$someOutput = json_decode($output[0], true);
echo "<h3>" . $someOutput['theWidth'] ."</h3>"; ?>
second.py looks like:
outputvalues = {
"leftCoordinate" : x_min(each of these are already defined),
"rightCoordinate" : x_max,
"lowerCoordinate" : y_min,
"upperCoordinate" : y_max,
"numInjuries" : count,
"theWidth" : im_width,
"theHeight" : im_height
}
y = json.dumps(outputvalues)
print(y)
when i run them in command prompt, the first one will result:
{"leftCoordinate": 4, "rightCoordinate": 10}
and when i put it in my html it will print out 4 and 10
the second one have this result in command prompt:
{"leftCoordinate": 34.47790487855673, "rightCoordinate": 251.67991018295288, "lowerCoordinate": 208.6769086420536, "upperCoordinate": 388.4499931335449, "numInjuries": 1, "theWidth": 327, "theHeight": 503}
but it won't print out any result in html
is there anything that i should change?
This might not be a helpful answer because there does not seem to be anything wrong with your code.
I placed the following at the top of second.py:
import json
x_min = 34.47790487855673
x_max = 251.67991018295288
y_min = 208.6769086420536
y_max = 388.4499931335449
count = 1
im_width = 327
im_height = 503
and removed the absolute paths in second.php:
exec("second.py 10.PNG 10.PNG 2>&1",$output);
Now php second.php shows me <h3>327</h3>.
This is on Windows 10 with PHP 7.3.1 and Python 3.7.4.
Edit: Tried it on Ubuntu 19.04/PHP 7.2.19/Python 2.7.16 and got the same result.
Based on your comments the problem is the fact that the Python script generates a warning which is then passed to the PHP script.
One way to ignore the warnings is to add the -W option with a value ignore. The line in the PHP script would then look something like this:
exec("python -W ignore second.py", $output);

Rscript not found in mac

If I execute an R program through macOS Terminal it works fine, but if I execute that program through PHP exec() I get an error which says "Rscript not found".
submit.php:
<?php
// print "hello";
$a = $_POST['a'];
$b = $_POST['b'];
// echo shell_exec("calc");
$output = exec("Rscript /xampp/htdocs/demo/1.R ".$a." ".$b);
print $output
?>
1.R:
setwd("C:/xampp/htdocs/demo")
print("hello")
args=commandArgs(trailingOnly = TRUE)
a = args[1]
b = args[2]
c = as.numeric(a)+as.numeric(b)
cat(c)
You need to tell the command interpreter where Rscript is located. In a normal login, you'd have a long PATH variable to search through, but when running from PHP you have a much more limited path. To figure out where the executable is located, type which Rscript from Terminal. Use the resulting path in your PHP script.
Also note that dumping raw user input into shell commands is a very bad idea. You should always use escapeshellarg() to make sure the input is sanitized. I would also suggest capturing the full output in the event that R outputs more than one line.
<?php
$a = escapeshellarg($_POST["a"]);
$b = escapeshellarg($_POST["b"]);
$r = "/xampp/htdocs/demo/1.R";
$lastline = exec("/usr/local/bin/Rscript $r $a $b", $output, $return);
// you could check the value of $return for non-zero values as well
// full output is returned as an array
echo implode("\n", $output);
Finally, you'll want to check your setwd() command in the R script. Might have some trouble with C: on a Mac! ;)

Python Script from PHP

I am running a Python script from a PHP file. For a very a simple example, it works perfectly. Here is the Python script:
#!/usr/bin/env python
def test():
x = 2 + 1
print x + 4
return x
print test()
And PHP script:
<?php
$command = escapeshellcmd('/usr/bin/python2.7 /home/super/PycharmProjects/img_plus_text/helloworld.py');
$output = shell_exec($command);
echo $output;
echo "Finishing....!";
?>
Unfortunately, for more complicated script, It does not work. Here is the Python script
#!/usr/bin/env python
import numpy as np
import os
import random
import caffe
import cv2
from sklearn.externals import joblib
import create_dataset_final_mlp
import text_2_bow
def create_class_mapping():
with open("text_processing/encoded-classes.txt") as class_file:
lines = class_file.readlines()
dictionary = {}
for item in lines:
item = item.split(" ")
dictionary[item[0]] = item[1].replace("\n", "").replace("#", "")
return dictionary
def get_top5(probability_list):
result = []
probabilities = []
for i in range(0, 5):
top = probability_list.index(max(probability_list))
probabilities.insert(i, probability_list[top])
probability_list[top] = -1
result.insert(i, top)
return result, probabilities
def select_random_file(base_dir):
# Select random category
file_chosen = random.choice(os.listdir(base_dir))
# Select random file in that category
file_chosen = random.choice(os.listdir(base_dir + "/" + file_chosen))
return file_chosen
def get_top5_class_name(mapping, top5_list):
class_name_list = []
for i in range(0, 5):
index = top5_list[i]
class_name = mapping[str(index)]
class_name_list.insert(i, class_name)
return class_name_list
def main(image_to_classify):
dataset_path = "/images-test1"
image_file_chosen = create_dataset_final_mlp.find(image_to_classify, dataset_path)
if image_file_chosen is None:
image_file_chosen = select_random_file("%s" % dataset_path)
identifier = image_file_chosen.split(".")[0]
print "Identifier: " + identifier
dictionary = text_2_bow.create_dictionary("text_processing/dictionary-1000-words.csv")
# load CNN model from disk
classifier = create_dataset_final_mlp.load_model_from_disk("test/deploy.prototxt",
"test/snapshot_iter_1020.caffemodel", '256,256',
"test/converted_mean.npy")
# load the final MLP to classify patterns
print "loading model from disk..."
mlp = joblib.load("trained_model_9097_5000_3000_iter_100.model")
with open("export-ferramenta.csv") as export_file:
for line in export_file:
if identifier in line:
pattern = line
print "Pattern: " + pattern
break
# Show selected image
full_img_path = create_dataset_final_mlp.find(image_file_chosen, dataset_path)
# img = cv2.imread(full_img_path, cv2.IMREAD_COLOR)
# cv2.imshow("Image selected", img)
# cv2.waitKey(0)
# get the CNN features
inputs = [caffe.io.load_image(full_img_path)]
# the second parameter is used to switch for prediction from center crop alone instead of averaging predictions across crops (default).
classifier.predict(inputs, False)
features = classifier.blobs['fc6'].data[0]
extracted_cnn_features = create_dataset_final_mlp.get_cnn_features_as_vector(features)
extracted_cnn_features = extracted_cnn_features[:-1]
extracted_cnn_features = extracted_cnn_features.split(",")
# transform the text using text_2_bow functions
pattern = pattern.split(",")
text_pattern = pattern[1] + pattern[2]
matrix = text_2_bow.get_matrix(text_pattern, dictionary)
reshaped_matrix = np.reshape(matrix, 5000).tolist()
full_features_vector = extracted_cnn_features + reshaped_matrix
full_features_vector = np.asarray(full_features_vector, np.float64)
# use the MLP to classify
prediction = mlp.predict_proba([full_features_vector])
tolist = prediction[0].tolist()
mapping = create_class_mapping()
top5, probabilities = get_top5(tolist)
top_class_name = get_top5_class_name(mapping, top5)
print "Top 5 classes: ", top_class_name
print "Top 5 probs ", probabilities
return top_class_name, probabilities
if __name__ == "__main__":
main("")
PHP Script
<?php
echo exec('sudo /home/super/bin/caffe-nv/python /home/super/PycharmProjects/img_plus_text/demo_website.py 2>&1');
?>
ERROR
sh: 1: /home/super/bin/caffe-nv/python: Permission denied
I have read majority of the questions on the issue however I am stuck here. Please point out the mistake. Thank you.
The problem was with environment variables, I made a shell script as shown below
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/:$LD_LIBRARY_PATH
export PYTHONPATH=$PYTHONPATH:/home/super/bin/caffe-nv/python/
python /demo_website2.py $1
Then I run the shell script from PHP using exec
exec("/opt/lampp/htdocs/test/run.sh ". $param, $output);

Does python json output values have maximum length for json_decode (php) to handle?

I am trying to write a php code that takes coefficients from a html form, sends them to a python algorithm that returns a json object. That object is a list of player names, basically {"Ji" : "Firstname Lastname"} for i from 1 to 15.
The python code (interface.py) I have to create this json is :
import json
i=0
for joueur in best_team:
i+=1
output["J%s"%(i)]=joueur['nom']
out=json.dumps(output)
print(out)
best_team is a list of player dictionnaries with data on them. My player names don't involve any non ASCII characters or whatever.
My php code is the following :
$command = "python interface.py";
$command .= " $coeff1 $coeff2 $coeff3 $coeff4 $coeff5 $coeff6 $coeff7 $coeff8 $coeff9 $coeff10 2>&1";
$pid = popen( $command,"r");
while( !feof( $pid ) )
{
$data = fread($pid, 256);
$data= json_decode($data) ;
echo $data->J1;
flush();
ob_flush();
echo "<script>window.scrollTo(0,99999);</script>";
usleep(100000);
}
pclose($pid);
I call the coefficients from the html and then send back the results via a js file.
But I just get the following error : Notice: Trying to get property of non-object.
Nothing wrong with the js file because if I try instead :
$string = '{"foo": "bar", "cool": "attributlong"}';
$result = json_decode($string);
echo $result ->cool;
It works.
Also if I have instead in my python file :
out={"foo":"bar","word":"longerthaneightcharacters"}
out=json.dumps(out)
print(out)
It works as well (replacing J1 by word in php code of course).
And funny enough, if i have in python:
output={}
i=0
for joueur in best_team:
i+=1
output["J%s"%(i)]="short"
output["J%s"%(i)]=str(output["J%s"%(i)])
out=json.dumps(output)
print(out)
It works, and if I replace "short" by "longerthaneightcharacters" it doesn't work anymore.
So basically my question is, why is there a maximum number of characters in my output loop and how can I bypass it ? Thanks, I am very confused.

how can i assign values to my c program with php

I'm trying to run a C program of adding two numbers with PHP in a web browser. But when I run the command
exec"gcc name.c -o a & a" it
returns some garbage result like sum is : 8000542.00. It doesn't ask for any input.
I want to give inputs to scanf from the browser. Please suggest to me how can I resolve my problem.
I have tried this but couldn't handle it successfully.
$desc = array(0=> array ('pipe','w'), 1=> array ('pipe','r'));
$cmd = "C:\xampp\htdocs\add.exe";
$pipes=array();
$p = proc_open($cmd,$desc,$pipes);
if(is_resource($p))
{
echo stream_get_contents($pipes[0]);
fclose($pipes[0]);
$return_value=proc_close($p);
echo $return_value;

Categories