from flask import Flask, request, jsonify from rasa.core.agent import Agent app = Flask(__name__) agent = Agent.load("models/20230608-162832.tar.gz") @app.route("/webhooks/rasa/webhook", methods=["POST"]) def webhook(): data = request.json message = data["message"] sender_id = data["sender"] response = agent.handle_text(message, sender_id=sender_id) text = response[0]["text"] return jsonify({"text": text}) if __name__ == "__main__": app.run(debug=True)发生错误ImportError: cannot import name 'EVENT_TYPE_OPENED' from 'watchdog.events' (C:\Users\Ljimmy\AppData\Local\Programs\Python\Python38\lib\site-packages\watchdog\events.py),修改代码
时间: 2024-01-03 13:03:26 浏览: 138
您可以尝试修改您的代码,使用 `EVENT_TYPE_CREATED` 替换 `EVENT_TYPE_OPENED`。
修改后的代码如下所示:
```python
from flask import Flask, request, jsonify
from rasa.core.agent import Agent
from watchdog.events import EVENT_TYPE_CREATED
import logging
app = Flask(__name__)
agent = Agent.load("models/20230608-162832.tar.gz")
logging.getLogger('werkzeug').disabled = True
@app.route("/webhooks/rasa/webhook", methods=["POST"])
def webhook():
data = request.json
message = data["message"]
sender_id = data["sender"]
response = agent.handle_text(message, sender_id=sender_id)
text = response[0]["text"]
return jsonify({"text": text})
if __name__ == "__main__":
app.run(debug=True)
```
在这个修改后的代码中,我们使用了 `EVENT_TYPE_CREATED` 替换了 `EVENT_TYPE_OPENED`,并且添加了禁用 Flask 日志打印的代码。这样您就可以避免 `watchdog` 模块导致的问题。
阅读全文