【问题标题】:Flask and Clarifai API not working when inputting a search term or image url输入搜索词或图像 URL 时,Flask 和 Clarifai API 不起作用
【发布时间】:2021-01-12 01:01:34
【问题描述】:

我刚刚在 YouTube 上完成了关于 Clarifai API 和 Flask 的 this 简短教程,但是当我尝试在 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 -

testing.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')

index.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

【问题讨论】:

    标签: python python-2.7 flask clarifai


    【解决方案1】:

    您是否将输入(图像)插入到您正在使用的关键 Clarifai 应用程序中?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-26
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多