python中的内部服务器错误500

时间:2015-11-10 10:27:35

标签: javascript jquery python ajax

我检查了所有与cgi相关的内容,但我仍然收到此错误。

我尝试运行其他示例程序,从.py文件获取响应。哪个工作正常,但对于此代码,它只给出:

GET http://localhost/testapp/demos/classifier_demo.py?query= 500 (Internal Server Error)x.ajaxTransport.n.send @ jquery-1.10.2.min.js:6x.extend.ajax @ jquery-1.10.2.min.js:6call_fun @ demo.html:16
jquery-1.10.2.min.js:6 GET http://localhost/testapp/demos/classifier_demo.py?query=EASTERN. 500 (Internal Server Error)x.ajaxTransport.n.send @ jquery-1.10.2.min.js:6x.extend.ajax @ jquery-1.10.2.min.js:6call_fun @ demo.html:16

P.S我已经设置了权限777

我每隔5秒使用jquery调用python脚本。

<script>
    $( document ).ready(function() {
        function call_fun() {
            var text = $('textarea#trans').val();
                //alert(text)
                var data = {"query" : text};
                //alert(data);
                $.ajax({
                        url: "classifier_demo.py",
                        type: "POST",
                        data: data,
                        success: function(response) {
                        console.log(response)
                        }
                       })       
                    }

                setInterval(call_fun, 5000);
    });

</script>

这是python代码:

#!/usr/bin/python
"""
Using the words as features removing stopwords
"""
from sklearn.utils import check_random_state
from sklearn.datasets import load_files
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import accuracy_score, average_precision_score, f1_score, precision_score, recall_score
from sklearn.externals import joblib
from sklearn.feature_extraction.text import FeatureHasher
from nltk.corpus import stopwords
from nltk.stem.lancaster import LancasterStemmer
from nltk.tokenize import word_tokenize
import nltk
import numpy as np
from time import time
import pprint, pickle
import gearman
import nltk, json, cgi
import re
import os
import sys
"""
This class will train and test the data and will give polarity for various emotions
"""
class SentimentAnalyzer(object):
    """
    Init for SentimentAnalyzer
    """
    def __init__(self):
        self.root_dir = os.getcwd()
        self.trainClassifier()

    """
    Function to fetch the data from cache
    @cache  <dict>  consist of training data
    """
    def fetch_data(self, cache, data_home=None, subset='train', categories=None,
                       shuffle=True, random_state=42):
        if subset in ('train', 'test'):
            data = cache[subset]
        else:
            raise ValueError(
                "subset can only be 'train', 'test' or 'all', got '%s'" % subset) 
        if shuffle:
            random_state = check_random_state(random_state)
            indices = np.arange(data.target.shape[0])
            random_state.shuffle(indices)
            data.filenames = data.filenames[indices]
            data.target = data.target[indices]
            # Use an object array to shuffle: avoids memory copy
            data_lst = np.array(data.data, dtype=object)
            data_lst = data_lst[indices]
            data.data = data_lst.tolist()
        return data

    """
    For custom tokenizing the text, removed stop words from text
    @text   <type 'str'>    text which needs to get tokenized
    @return <type 'str'>    tokens
    """
    def token_ques(self, text):
        things_to_replace = ['?']
        #wh_tags = ['WP','WRB','MD','WDT']
        things_to_replace += stopwords.words('english')
        #wh_word = None
        for tok in text.split('\n'):
            original_query = tok
            query_pos_tags = nltk.pos_tag(word_tokenize(tok))     
            for word in things_to_replace:
                tok = tok.lower()
                tok = re.sub("\s"+word+"\s|\s?"+"\?"+"$",' ',tok)
                tok = tok.strip("  ")
                tok = tok.lstrip(" ")
                tok = tok.rstrip(" ")
            for word in word_tokenize(tok):
                yield word.lower()

    """
    Train classifier
    """
    def trainClassifier(self):
        try:
            t1 = time()
            start_time = time()
            self.hasher = FeatureHasher(input_type='string',non_negative=True)
            self.clf = MultinomialNB(alpha=0.001)
            self.hasher = FeatureHasher(input_type='string',non_negative=True)
            self.clf = MultinomialNB(alpha=0.001)

            data_folder = self.root_dir + "/emotions"
            train_dataset = load_files(data_folder)

            print("Time taken to load the data=>", time()-start_time)
            print("data loaded")

            cache = dict(train=train_dataset)
            self.data_train = self.fetch_data(cache, subset='train')

            try:
                X_train = pickle.load(open("x_result.pickle", "rb" ) )
                y_train = pickle.load(open("y_result.pickle", "rb" ) )
                self.clf.fit(X_train, y_train)
            except:

                print "Updating the classifier"
                training_data = []
                for text in self.data_train.data:
                    #text = self.modifyQuery(text.decode('utf-8','ignore'))
                    text = text.decode('utf-8','ignore')
                    training_data.append(text)

                raw_X = (self.token_ques(text) for text in training_data)  #Type of raw_X  <type 'generator'>
                #X_train = self.vectorizer.fit_transform(raw_X)
                X_train = self.hasher.transform(raw_X)
                y_train = self.data_train.target

                readx = open('x_result.pickle', 'wb')
                pickle.dump(X_train, readx)
                readx.close()

                readY = open('y_result.pickle', 'wb')
                pickle.dump(y_train, readY)
                readY.close()


                self.clf.fit(X_train, y_train)
            print("Classifier tained ...")
            print("time taken=>", time()-t1)

        except Exception:
            import traceback
            print traceback.format_exc()

    """
    Function to test classifier
    """
    def testClassifier(self, query):
        try:
            result = {}
            #To replace NE
            #query = self.modifyQuery(query)
            test_data = [query]
            raw_X = (self.token_ques(text) for text in test_data)
            X_test = self.hasher.transform(raw_X)
            #X_test = self.vectorizer.fit_transform(raw_X)
            pred = self.clf.predict(X_test)
            print("pred=>", pred)
            self.categories = self.data_train.target_names
            for doc, category in zip(test_data, pred):
                print('%r => %s' % (doc, self.categories[category]))
            index = 1
            predict_prob = self.clf.predict_proba(X_test)
            final_result = []
            for doc, category_list in zip(test_data, predict_prob):
                # print('\n\n')
                category_list = sorted(enumerate(category_list), key=lambda x:x[1], reverse=True)
                i = 0
                for val in category_list:
                    if float(val[1]) > float(0.05):
                        # print('%r => %s => %s' % (doc, self.categories[val[0]], str(val[1])))
                        result = {}
                        result[self.categories[val[0]]] = "%0.2f"%(float(val[1]) * 100)+"%"
                        final_result.append(result)
                        index += 1
        except Exception:
            import traceback
            print traceback.format_exc()
        import json
        # print result
        # print final_result
        return final_result


if __name__ == '__main__':
    fs = cgi.FieldStorage()
    text = fs['query'].value
    #query = fs.getvalue(query)
    #query = raw_input("Please enter the text to process:")
    query = "Love you man"
    result  = { "result" : text}
    #result = SentimentAnalyzer().testClassifier(query)
    json_result = json.dumps( result )
    print json_result

0 个答案:

没有答案