File size: 2,659 Bytes
ee4fe67 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
from flask import Flask, request, jsonify
from model import load_model, find_similar_images,extract_filenames
import os
import difflib
import pandas as pd
from flask_cors import CORS
import json
app = Flask(__name__)
CORS(app)
data = pd.read_csv('kitpot.categoriesNew.csv', encoding='ISO-8859-1')
titles = data['DATA'].tolist()
lowercase_titles = [str(title).lower() if isinstance(title, str) else '' for title in titles]
data = list(filter(None, lowercase_titles))
def find_closest_match(input_word, data):
if input_word in data:
return [input_word]
closest_match = difflib.get_close_matches(input_word, data)
return closest_match
def process_search_term(search_term, data):
words = search_term.split(' ')
processed_words = []
for word in words:
if word.lower() in data:
processed_words.append(word)
else:
closest_match = find_closest_match(word.lower(), data)
processed_words.append(closest_match[0] if closest_match else word)
processed_sentence = ' '.join(processed_words)
return processed_sentence
@app.route('/get_closest_match', methods=['GET'])
def get_closest_match():
search_term = request.args.get('search_key', default='', type=str)
if not search_term:
response = {
'message': 'Search term is empty.',
}
return jsonify(response)
processed_sentence = process_search_term(search_term, data)
if processed_sentence:
response = {
'result': processed_sentence,
}
else:
response = {
'message': 'No similar words found.',
}
return jsonify(response)
@app.route('/similar_images', methods=['POST'])
def get_similar_images():
if 'imageSearch' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['imageSearch']
if file.filename == '':
return jsonify({'error': 'No selected file'})
if file:
filename = file.filename
upload_folder = 'uploads'
if not os.path.exists(upload_folder):
os.makedirs(upload_folder)
filepath = os.path.join(upload_folder, filename)
file.save(filepath)
similar_images = find_similar_images(filepath)
actualFormatedData=extract_filenames(similar_images)
# actualFormatedData = convert_file_paths(similar_images)
os.remove(filepath)
return jsonify({'result': actualFormatedData})
else:
return jsonify({'error': 'File format not supported'})
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000,debug=True)
|