File size: 4,100 Bytes
cf7c183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from flask import Flask, redirect, render_template, request, url_for
from document_preprocessor import *
from indexing import *
from ranker import *
import pandas as pd
from image2text import *
app = Flask(__name__)


def initialize_all():
    MAIN_INDEX_STEMMED = "Index_stemmed"
    STOPWORD_PATH = "data/stopwords.txt"

    # stopwords
    stopwords = set()
    with open(STOPWORD_PATH, "r", encoding="utf-8") as file:
        for stopword in file:
            stopwords.add(stopword.strip())

    # index
    main_index = BasicInvertedIndex()
    main_index.load(MAIN_INDEX_STEMMED)

    # processor
    preprocessor = RegexTokenizer(token_regex=r"[\w\.-]+", stemming=True)

    bm25 = BM25(main_index)
    pipeline = Ranker(main_index, preprocessor, stopwords, bm25)
    return pipeline


def get_results_all(ranker, query, top_n, args=None):
    DATASET_CSV_PATH = "data/data.csv.zip"
    results = ranker.query(query)
    docids = [result[0] for result in results]
    df = pd.read_csv(DATASET_CSV_PATH)
    if args is None:
        df_results = df.iloc[docids]
    else:
        df_results = df.iloc[docids]
        for arg in args:
            if arg:
                arg = arg.split(",")
                prompt_filter = ""
                for tag in arg:
                    prompt_filter += (
                        r'df_results["prompt"].str.contains(fr"\b'
                        + tag
                        + r'\b", regex=True, case=False) | '
                    )
                df_results = df_results[eval(prompt_filter[:-3])]
    prompts = df_results["prompt"].tolist()[:top_n]
    urls = df_results["pic_url"].tolist()[:top_n]
    return prompts, urls


engine = initialize_all()


@app.route("/")
def home():
    query = "A mountain in spring"
    print(query)
    prompts, urls = get_results_all(engine, query, 200)
    result = list(zip(prompts, urls))
    return render_template("index.html", result=result)


@app.route("/search", methods=["POST", "GET"])
def search():
    if request.method == "POST":
        query = request.form.get("query")
        if not query:
            query = "A mountain in spring with white cloud"
        style = request.form.get("style")
        scene = request.form.get("scene")
        medium = request.form.get("medium")
        light = request.form.get("light")
        quality = request.form.get("quality")
        print(query)
        print(style)
        print(scene)
        print(medium)
        print(light)
        print(quality)
        args = [style, scene, medium, light, quality]
        prompts, urls = get_results_all(engine, query, 200, args)
        result = list(zip(prompts, urls))
        return render_template(
            "search.html",
            result=result,
            query=query,
            style=style,
            scene=scene,
            medium=medium,
            light=light,
            quality=quality,
        )
    return redirect(url_for("home"))

@app.route("/search_picture", methods=["POST", "GET"])
def search_picture():
    if request.method == "POST":
        query=request.files['img']
        query = image2textData(query)
        if not query:
            query = "A mountain in spring with white cloud"
        style = request.form.get("style")
        scene = request.form.get("scene")
        medium = request.form.get("medium")
        light = request.form.get("light")
        quality = request.form.get("quality")
        print(query)
        print(style)
        print(scene)
        print(medium)
        print(light)
        print(quality)
        args = [style, scene, medium, light, quality]
        prompts, urls = get_results_all(engine, query, 200, args)
        result = list(zip(prompts, urls))
        return render_template(
            "search.html",
            result=result,
            query=query,
            style=style,
            scene=scene,
            medium=medium,
            light=light,
            quality=quality,
        )
    return redirect(url_for("home"))


if __name__ == "__main__":
    # engine = initialize_all()
    app.run(debug=True)