Demo750's picture
Upload folder using huggingface_hub
569f484 verified
from flask import Flask, request, render_template, redirect, url_for, send_from_directory, jsonify
import os
import sys
root = '/Users/jianpingye/Desktop/Marketing_Research/XGBoost_Gaze_Prediction_Platform/Gaze-Time-Prediction-for-Advertisement/XGBoost_Prediction_Model'
sys.path.append(root)
import Predict
import numpy as np
General_Category = {'Potatoes / Vegetables / Fruit': 0, 'Chemical products': 1, 'Photo / Film / Optical items': 2, 'Catering industry': 3, 'Industrial products other': 4, 'Media': 5, 'Real estate': 6, 'Government': 7, 'Personnel advertisements': 8, 'Cars / Commercial vehicles': 9, 'Cleaning products': 10, 'Retail': 11, 'Fragrances': 12, 'Footwear / Leather goods': 13, 'Software / Automation': 14, 'Telecommunication equipment': 15, 'Tourism': 16, 'Transport/Communication companies': 17, 'Transport services': 18, 'Insurances': 19, 'Meat / Fish / Poultry': 20, 'Detergents': 21, 'Foods General': 22, 'Other services': 23, 'Banks and Financial Services': 24, 'Office Products': 25, 'Household Items': 26, 'Non-alcoholic beverages': 27, 'Hair, Oral and Personal Care': 28, 'Fashion and Clothing': 29, 'Other products and Services': 30, 'Paper products': 31, 'Alcohol and Other Stimulants': 32, 'Medicines': 33, 'Recreation and Leisure': 34, 'Electronics': 35, 'Home Furnishings': 36, 'Products for Business Use': 37}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), 'uploads')
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # Set max upload size to 16MB
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'image' not in request.files:
return redirect(request.url)
file = request.files['image']
if file.filename == '':
return redirect(request.url)
if file:
filename = file.filename
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
# Get additional input numbers and option from the form
brand_size = request.form.get('brand_size', type=float)
pictorial_size = request.form.get('pictorial_size', type=float)
text_size = request.form.get('text_size', type=float)
ad_size = request.form.get('ad_size', type=float)
ad_location = request.form.get('ad_location')
gaze_type = request.form.get('gaze_type')
option = request.form.get('option')
processed_value = process_image_function(filepath, brand_size, pictorial_size, text_size, ad_size, option, ad_location, gaze_type)
formatted_value = f"{processed_value:.2f} sec"
return render_template('index.html', filename=filename, processed_value=formatted_value)
def process_image_function(image_path, brand_size, pictorial_size, text_size, ad_size, option, ad_location, gaze_type):
text_detection_model_path = 'EAST-Text-Detection/frozen_east_text_detection.pb'
LDA_model_pth = 'LDA_Model_trained/lda_model_best_tot.model'
training_ad_text_dictionary_path = 'LDA_Model_trained/object_word_dictionary'
training_lang_preposition_path = 'LDA_Model_trained/dutch_preposition'
global General_Category
prod_group = np.zeros(38)
prod_group[General_Category[option]] = 1
if ad_location == 'left':
ad_loc = 0
elif ad_location == 'right':
ad_loc = 1
else:
ad_loc = None
predicted_gaze = Predict.Ad_Gaze_Prediction(input_ad_path=image_path, input_ctpg_path=None, ad_location=ad_loc,
text_detection_model_path=text_detection_model_path, LDA_model_pth=LDA_model_pth,
training_ad_text_dictionary_path=training_ad_text_dictionary_path, training_lang_preposition_path=training_lang_preposition_path, training_language='dutch',
surface_sizes=[brand_size,pictorial_size,text_size,ad_size], Product_Group=prod_group,
obj_detection_model_pth=None, num_topic=20, Gaze_Time_Type=gaze_type)
return predicted_gaze
@app.route('/uploads/<filename>')
def uploaded_file(filename):
temp = send_from_directory(app.config['UPLOAD_FOLDER'], filename)
return temp #send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == '__main__':
app.run(port=5000)