if not hasattr(watchdog.events, "EVENT_TYPE_OPENED"): watchdog.events.EVENT_TYPE_OPENED = 'created' app = Flask(name) qa_df = pd.read_excel('qa.xlsx') def process_text(text): text = text.lower().strip() text = re.sub(r'[^\w\s]', '', text) return text def match_question(text): for i, row in qa_df.iterrows(): if re.search(row['question'], text): return row['answer'] return None @app.route('/api/chatbot', methods=['POST']) def chatbot(): text = request.json['text'] processed_text = process_text(text) answer = match_question(processed_text) if answer: return jsonify({'response': answer}) else: return jsonify({'response': 'Sorry'}) if name == 'main': app.run(debug=True)发生错误File c:\users\ljimmy\appdata\local\programs\python\python38\lib\site-packages\werkzeug_reloader.py:456, in run_with_reloader(main_func, extra_files, exclude_patterns, interval, reloader_type) 454 reloader.run() 455 else: --> 456 sys.exit(reloader.restart_with_reloader()) 457 except KeyboardInterrupt: 458 pass SystemExit: 1如何解决
时间: 2024-03-11 13:48:15 浏览: 103
该错误是因为在代码的第二行中,使用了未定义的变量`name`,应该将其更正为`__name__`。同时,代码的最后一行`if name == 'main':`也应该更正为`if __name__ == '__main__':`。修改后的代码如下:
```
if not hasattr(watchdog.events, "EVENT_TYPE_OPENED"):
watchdog.events.EVENT_TYPE_OPENED = 'created'
app = Flask(__name__)
qa_df = pd.read_excel('qa.xlsx')
def process_text(text):
text = text.lower().strip()
text = re.sub(r'[^\w\s]', '', text)
return text
def match_question(text):
for i, row in qa_df.iterrows():
if re.search(row['question'], text):
return row['answer']
return None
@app.route('/api/chatbot', methods=['POST'])
def chatbot():
text = request.json['text']
processed_text = process_text(text)
answer = match_question(processed_text)
if answer:
return jsonify({'response': answer})
else:
return jsonify({'response': 'Sorry'})
if __name__ == '__main__':
app.run(debug=True)
```
修改后,应该就可以解决该错误了。
阅读全文