def nlcnewx(request):
  # This is a call that is not done through REST. 
  # The request is for a new classifier. The file is sent through a form 
  # The return is a redirect back to the classifier list, which forces it to refresh.
  wdc = WDCService('LC')
  service_creds = wdc.getCreds()   

  if request.POST:  
    form = UploadClassifierForm(request.FILES)	
    
    if request.FILES and 'classInputFile' in request.FILES:
      f = request.FILES['classInputFile']	  
	  
      module_dir = os.path.dirname(__file__)  
      file_path = os.path.join(module_dir, '../static/', 'xx.json')		  
      with open(file_path, 'wb+') as destination:
        for chunk in f.chunks():
          destination.write(chunk)	
		  
      destination.close()	  

      with open(file_path) as fj:
        data = json.loads(fj.read())

      wdc = WDCService('LC')
      service_creds = wdc.getCreds() 
      nlcService = wdc.nlcService()
      if nlcService is not None:
        nlcResults = nlcService.createClassifier(data)
        if nlcResults and 'error' in nlcResults:
            service_creds['error'] = nlcResults['description']		
            return render(request, 'Watson/lcindex.html', service_creds)
  
  return redirect('watson:nlclassifier')  
def piindex(request):  
  wdc = WDCService('PI')
  service_creds = wdc.getCreds()
  
  form = Form_pi()    
  service_creds['form'] = form 
  return render(request, 'Watson/piindex.html', service_creds)
def lcindex(request):   
  # These are the views for the Natural Language Classifier portion of the application. 
  # It makes use of both the NLC and a Twitter service, and all calls to the services
  # are done from the client via AJAX calls to REST APIs  
  wdc = WDCService('LC')
  service_creds = wdc.getCreds()  
  nlcService = wdc.nlcService() 
  return render(request, 'Watson/lcindex.html', service_creds)
def staudio(request):
  # This request receives an Audio BLOB file which is passed to the
  # Speech to text service. The response is then forwarded to the 
  # classifier service.
  results = {}
  theData = {"error": "Error detected in REST API"} 	  
  module_dir = os.path.dirname(__file__)  
  if request.POST and request.FILES:  
    
    form = UploadAudioForm(request.POST, request.FILES)    
    # Don't bother checking the form, as it is always invalid
    #if form.is_valid():	
    #  print("Valid Form")  
    #else:
    #   print("Invalid Form")
	
    filename = ""
    classURL = ""
	
    if "fname" in request.POST:
      filename = request.POST["fname"]

    if "classifierurl" in request.POST:
      classURL = request.POST["classifierurl"]
	
    # Saving the file and reading it again, as this ensures that all the data has
    # been received. This gives a better result from the service.	
    f = request.FILES['data']	
    if f:
       file_path = os.path.join(module_dir, '../static/', filename)	
       with open(file_path, 'wb+') as destination:
         for chunk in f.chunks():
           destination.write(chunk)			  
       destination.close()	  	   
	 
    # Remember to switch	 
    yy_file_path = os.path.join(module_dir, '../static/', filename)	
    #yy_file_path = os.path.join(module_dir, '../static/', 'yy.wav')	
    with open(yy_file_path, 'rb') as fj:
      audiodata = fj.read()   
      if audiodata:
        wdc = WDCService('ST')
        service_creds = wdc.getCreds()
        stService = wdc.stService()
        if stService is None:
          theData = {"error": "No Speech to Text service found"} 	  
        else:
          theData = stService.processAudio(audiodata)
          if "results" in theData:
            if list is type(theData["results"]):
              res = theData["results"][0]
              if "alternatives" in res:
                alt = res["alternatives"][0]
                if "transcript" in alt:
                  theData = classifyTranscript(classURL, alt["transcript"])
      fj.close()	   
  results["results"] = theData		
  return HttpResponse(json.dumps(results), content_type="application/json") 
def taindex(request):  
  """
     This is the initial (or refresh) page request, not much to do other than send the form 
  """
  wdc = WDCService('TA')
  service_creds = wdc.getCreds()
  
  form = Form_ta()    
  service_creds['form'] = form 
  return render(request, 'Watson/taindex.html', service_creds)
def processPIRequest(request, theData):
  results = {}
  data = {}
  errorFound = False
  wdc = WDCService('PI')
  service_creds = wdc.getCreds()
  
  personality = theData["personality"]  
  performTwitterScan = theData["twitterScan"] 

  if performTwitterScan and personality and 0 < len(personality):
    # Data needs to come from twitter
    pulledData = pullTwitterTimeline(personality)
    if "error" in pulledData:
      errorFound = True	
    elif "data" in pulledData:
      data = pulledData["data"]
  else:    
    data = theData["data"].encode('utf8', 'replace')
 
  if not errorFound:  
    results = wdc.performOperation(data)  
    service_creds['results'] = results
    pi = Pidata(results)
	
    keys = pi.big5keys()  
    traits = {}
    for trait in keys:  
      traits[trait] = pi.getTraitValue(trait, False)  

    service_creds["traits"] = traits    
    savePersonality(personality, pi)	  
    results = service_creds	
  else:
    results["results"] = data
 
  return results
Beispiel #7
0
def staudio_with_nlc(request):
  # This request receives an Audio BLOB file which is passed to the
  # Speech to text service. The response is then forwarded to the 
  # classifier service.
  results = {}
  tts_input = ''
  theData = {"error": "Error detected in REST API"} 	  
  module_dir = os.path.dirname(__file__)  
  if request.POST and request.FILES:  
    
    form = UploadAudioForm(request.POST, request.FILES)    
    # Don't bother checking the form, as it is always invalid
    #if form.is_valid():	
    #  print("Valid Form")  
    #else:
    #   print("Invalid Form")
	
    filename = ""
    classURL = ""
    textscript = ""
    if "fname" in request.POST:
      filename = request.POST["fname"]
      
    if "textscript" in request.POST:
      textscript = request.POST["textscript"]

    if "classifierurl" in request.POST:
      classURL = request.POST["classifierurl"]
	
    # Saving the file and reading it again, as this ensures that all the data has
    # been received. This gives a better result from the service.	
    f = request.FILES['data']	
    if f:
       file_path = os.path.join(module_dir, '../static/', filename)	
       with open(file_path, 'wb+') as destination:
         for chunk in f.chunks():
           destination.write(chunk)			  
       destination.close()	  	   
	 
    # Remember to switch	 
    yy_file_path = os.path.join(module_dir, '../static/', filename)	
    #yy_file_path = os.path.join(module_dir, '../static/', 'yy.wav')	
    with open(yy_file_path, 'rb') as fj:
      audiodata = fj.read()   
      if audiodata:
        wdc = WDCService('ST')
        service_creds = wdc.getCreds()
        stService = wdc.stService()
        if stService is None:
          theData = {"error": "No Speech to Text service found"} 	  
        else:
          theData = stService.processAudio(audiodata)
          if "results" in theData:
            if list is type(theData["results"]):
              res = theData["results"][0]
              if "alternatives" in res:
                alt = res["alternatives"][0]
                if "transcript" in alt:
                  question = alt["transcript"]
                  theData = classifyTranscript(classURL, alt["transcript"])
                  data = {}
                  if textscript != "":
                      data = {"txt": textscript,
                        "conversation_id":"",
                        "client_id":""
                      }
                  else:
                      data = {"txt": alt["transcript"],
                        "conversation_id":"",
                        "client_id":""
                      }
                  if not 'classification' in theData:
                    raise Exception('Classificatio failed: {}'.format(theData))
                  category = theData["classification"]["top_class"]
                  res = sendDialogAPI(category, {'data': json.dumps(data)}) 
                  theData = res['results']
                  theData['category'] = category
                  theData['question'] = question
                  tts_input = theData['conversationData']['response']
      fj.close()	   
  results["results"] = theData
  textToSpeech(tts_input)
  return HttpResponse(json.dumps(results), content_type="application/json")