我刚刚在 YouTube 上完成了这个关于 Clarifai API 和 Flask 的简短教程,但是当我尝试在 http://localhost:5000/ 上的任一输入位置输入搜索结果时,我的 Web 应用程序无法正常工作,因为输入我的任何一个后都没有显示任何内容搜索词或图片网址。也就是说,index.html 正在显示,但是,例如,当我尝试在第一个输入中输入“动物”并按下“搜索”按钮时,什么也没有发生。
下面是回溯、代码本身和我的目录结构。任何帮助将不胜感激。
追溯
/usr/local/bin/python3.8 /Users/lyons/Desktop/testing.py
* Serving Flask app "testing" (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: off
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [25/Sep/2020 11:45:03] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [25/Sep/2020 11:45:10] "POST / HTTP/1.1" 200 -
测试.py
from clarifai.rest import ClarifaiApp
from flask import Flask, request, render_template
clarifaiKEY = 'REDACTEDFORPRIVACY'
myAI = ClarifaiApp(api_key=clarifaiKEY)
app = Flask(__name__)
@app.route('/')
def index():
return render_template('/index.html', len=0)
@app.route('/', methods=['POST'])
def search():
if request.form['searchByConcept']:
searchTerm = request.form['searchByConcept']
searchResults = myAI.inputs.search_by_predicted_concepts(concept=searchTerm)
return render_template('/index.html', len=len(searchResults), searchResults=searchResults)
elif request.form['searchByImage']:
refImage = request.form['searchByImage']
searchResults = myAI.inputs.search_by_image(url=refImage)
return render_template('/index.html', len=len(searchResults), searchResults=searchResults)
if __name__ == '__main__':
app.run(host='0.0.0.0')
索引.html
<h1>Can I help you find something?</h1>
<form action="." method="post">
<h2>Search By Concept</h2>
<input type="text" name="searchByConcept">
<input type="submit" name="searchByConcept" value="Search">
<h2>Search By Image</h2>
<input type="text" name="searchByImage" >
<input type="submit" name="searchByImage" value="Search">
</form>
{%for i in range(0,len)%}
<li><img src="{{searchResults[i].url}}" alt=""></li>
{%endfor%}
我的目录结构如下
/testing.py
/templates
/index.html