Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
WebSocket no funciona al intentar enviar la respuesta generada por keras

Estoy implementando un chatbot simple usando keras y WebSockets. Ahora tengo un modelo que puede hacer una predicción sobre la entrada del usuario y enviar la respuesta correspondiente.

Cuando lo hago a través de la línea de comandos, funciona bien, sin embargo, cuando intento enviar la respuesta a través de mi WebSocket, el WebSocket ya ni siquiera se inicia.

Aquí está mi código WebSocket de trabajo:

 @sock.route('/api') def echo(sock): while True: # get user input from browser user_input = sock.receive() # print user input on console print(user_input) # read answer from console response = input() # send response to browser sock.send(response)

Aquí está mi código para comunicarme con el modelo keras en la línea de comandos:

 while True: question = input("") ints = predict(question) answer = response(ints, json_data) print(answer)

Los métodos utilizados son aquellos:

 def predict(sentence): bag_of_words = convert_sentence_in_bag_of_words(sentence) # pass bag as list and get index 0 prediction = model.predict(np.array([bag_of_words]))[0] ERROR_THRESHOLD = 0.25 accepted_results = [[tag, probability] for tag, probability in enumerate(prediction) if probability > ERROR_THRESHOLD] accepted_results.sort(key=lambda x: x[1], reverse=True) output = [] for accepted_result in accepted_results: output.append({'intent': classes[accepted_result[0]], 'probability': str(accepted_result[1])}) print(output) return output def response(intents, json): tag = intents[0]['intent'] intents_as_list = json['intents'] for i in intents_as_list: if i['tag'] == tag: res = random.choice(i['responses']) break return res

Entonces, cuando inicio el WebSocket con el código de trabajo, obtengo este resultado:

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Serving Flask app 'server' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on

Pero tan pronto como tengo algo de mi modelo en la clase server.py obtengo este resultado:

 2022-02-13 11:31:38.887640: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support. 2022-02-13 11:31:38.887734: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>) Metal device set to: Apple M1 systemMemory: 16.00 GB maxCacheSize: 5.33 GB

Es suficiente cuando solo tengo una importación en la parte superior como esta: from chatty import response, predict , aunque no se usen.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Estoy devastado, solo perdí 2 días en el problema más tonto posible (y lo solucioné)

todavía tenía el

 while True: question = input("") ints = predict(question) answer = response(ints, json_data) print(answer)

en mi archivo de modelo, por lo que el servidor no se inició. La solución fue eliminarlo y ahora funciona bien.

over 4 years ago · Santiago Trujillo Report

0

No hay problema con su ruta websocket. ¿Podría compartir cómo está activando esta ruta? Websocket es un protocolo diferente y sospecho que está utilizando un cliente HTTP para probar websocket. Por ejemplo en Cartero:

Nueva pantalla del cartero

Las solicitudes HTTP son diferentes a las solicitudes websocket. Por lo tanto, debe usar el cliente apropiado para probar websocket.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!