File size: 9,330 Bytes
7c4da79
df40eb2
8f2e1c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c4da79
8f2e1c5
 
 
 
 
 
7c4da79
8f2e1c5
7c4da79
 
8f2e1c5
 
7c4da79
8f2e1c5
 
 
 
 
 
 
 
 
 
 
 
 
 
7c4da79
8f2e1c5
 
 
 
 
 
7c4da79
8f2e1c5
 
 
7c4da79
 
 
 
 
 
8f2e1c5
3bb4577
 
 
 
 
8f2e1c5
7c4da79
 
8f2e1c5
 
 
 
7c4da79
 
 
 
8f2e1c5
7c4da79
8f2e1c5
55fd32b
7c4da79
8f2e1c5
 
 
7c4da79
 
8f2e1c5
 
 
 
 
 
 
 
7c4da79
 
8f2e1c5
 
7c4da79
 
8f2e1c5
 
7c4da79
 
 
 
8f2e1c5
 
 
 
 
7c4da79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f2e1c5
 
7c4da79
8f2e1c5
7c4da79
 
8f2e1c5
 
7c4da79
8f2e1c5
 
7c4da79
8f2e1c5
 
7c4da79
 
 
8f2e1c5
7c4da79
 
8f2e1c5
 
7c4da79
8f2e1c5
7c4da79
 
 
 
8f2e1c5
 
 
 
 
 
7c4da79
 
 
 
8f2e1c5
 
 
 
 
 
 
 
 
 
 
7c4da79
 
8f2e1c5
 
7c4da79
8f2e1c5
 
 
 
 
 
7c4da79
8f2e1c5
 
 
7c4da79
 
8f2e1c5
 
7c4da79
8f2e1c5
 
7c4da79
8f2e1c5
 
7c4da79
 
8f2e1c5
7c4da79
 
8f2e1c5
 
7c4da79
8f2e1c5
 
 
 
 
 
 
7c4da79
 
 
 
8f2e1c5
 
 
 
 
 
 
 
 
 
 
 
 
85bf311
 
 
7ef23f3
 
85bf311
 
8f2e1c5
 
df40eb2
 
7c4da79
 
 
 
 
 
 
 
 
 
 
df40eb2
aeee7ee
55fd32b
9aaa184
55fd32b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import os
import gradio
import pandas as pd
import psycopg2
import re
import nltk
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import unicodedata

nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('stopwords')


def get_paragraph(row, index):
    ans = ''
    for x in row[index]:
        ans = ans + ' ' + x.lower()
    return ans


def remove_accents(text):
    text = unicodedata.normalize('NFKD', text).encode(
        'ASCII', 'ignore').decode('utf-8')
    return text


def get_clean_text(row, index):
    if not isinstance(row[index], str):
        return ''
    if row[index] == "NULL":
        return ''
    clean_text = ''
    words = word_tokenize(row[index].lower())
    for word in words:
        word = word.replace(',', ' ')
        word = remove_accents(word)
        if re.match(r'^[a-zA-Z]+$', word) and word not in stop_words and len(word) > 1 and word[1] != '.':
            clean_text += ' ' + word
    return clean_text


def combine(row, indices):
    ans = ''
    for i in indices:
        ans = ans + ' ' + row[i]
    return ans


stop_words = set(stopwords.words('english'))
query = "SELECT * FROM base_springerdata"

CACHE = {}
SQL_KEY = 'sql'
JOURNAL_COMPLETE = 'journal_complete'
JOURNAL_PARTIAL = 'journal_partial'
VECTORIZER = 'vectorizer'
JOURNAL_TFIDF = 'journal_tfidf'

# Access the secrets
HOST = os.getenv('DATABASE_HOST')
DATABASE = os.getenv('DATABASE_NAME')
USER = os.getenv('DATABASE_USER')
PASSWORD = os.getenv('DATABASE_PASSWORD')
# load sql


def load_sql_data(query):
    if SQL_KEY in CACHE:
        return CACHE[SQL_KEY]
    conn = psycopg2.connect(
        host=HOST,
        database=DATABASE,
        user=USER,
        password=PASSWORD
    )
    df = pd.read_sql_query(query, conn)
    df = df.drop(['item_doi'], axis=1)

    # Close the database connection
    conn.close()
    CACHE[SQL_KEY] = df
    return df


# main_df
main_df = load_sql_data(query)


# load journal_df
def get_journal_df(df):
    if JOURNAL_PARTIAL in CACHE:
        return CACHE[JOURNAL_PARTIAL]
    journal_art = df.groupby('publication_title')['item_title'].apply(
        list).reset_index(name='Articles')
    journal_art.set_index(['publication_title'], inplace=True)

    journal_auth = df.groupby('publication_title')['authors'].apply(
        list).reset_index(name='authors')
    journal_auth.set_index('publication_title', inplace=True)

    journal_key = df.drop_duplicates(
        subset=["publication_title", "keywords"], keep='first')
    journal_key = journal_key.drop(
        ['item_title', 'authors', 'publication_year', 'url'], axis=1)
    journal_key.set_index(['publication_title'], inplace=True)

    journal_main = journal_art.join([journal_key, journal_auth])
    print('journal_main intial')
    journal_main.reset_index(inplace=True)
    journal_main['Articles'] = journal_main.apply(
        get_paragraph, index='Articles', axis=1)
    journal_main['Articles'] = journal_main.apply(
        get_clean_text, index='Articles', axis=1)
    journal_main['authors'] = journal_main.apply(
        get_paragraph, index='authors', axis=1)
    journal_main['authors'] = journal_main.apply(
        get_clean_text, index='authors', axis=1)
    journal_main['keywords'] = journal_main.apply(
        get_clean_text, index='keywords', axis=1)

    journal_main['Tags'] = journal_main.apply(
        combine, indices=['keywords', 'Articles', 'authors'], axis=1)
    journal_main['Tags'] = journal_main.apply(
        get_clean_text, index='Tags', axis=1)
    CACHE[JOURNAL_PARTIAL] = journal_main
    return journal_main


# Journal Dataframe
journal_main = get_journal_df(main_df)
print('journal_main processed')


# load tfidfs
def get_tfidfs(journal_main):
    if VECTORIZER and JOURNAL_TFIDF in CACHE:
        return CACHE[VECTORIZER], CACHE[JOURNAL_TFIDF]
    vectorizer = TfidfVectorizer(decode_error='ignore', strip_accents='ascii')
    journal_tfidf_matrix = vectorizer.fit_transform(journal_main['Tags'])
    CACHE[VECTORIZER] = vectorizer
    CACHE[JOURNAL_TFIDF] = journal_tfidf_matrix
    return vectorizer, journal_tfidf_matrix


vectorizer, journal_tfidf_matrix = get_tfidfs(journal_main)
print('tfids and vectorizer for journals completed')


def get_article_df(row):
    article = main_df.loc[main_df['publication_title'] ==
                          journal_main['publication_title'][row.name]].copy()
    article['item_title'] = article.apply(
        get_clean_text, index='item_title', axis=1)
    article['authors'] = article.apply(get_clean_text, index='authors', axis=1)
    article['Tokenized'] = article['item_title'].apply(word_tokenize)
    article['Tagged'] = article['Tokenized'].apply(pos_tag)
    article['Tags'] = article['Tagged'].apply(lambda x: [word for word, tag in x if
                                                         tag.startswith('NN') or tag.startswith('JJ') and word.lower() not in stop_words])
    article['Tags'] = article.apply(get_paragraph, index='Tags', axis=1)
    article['Tags'] = article.apply(
        lambda x: x['Tags'] + ' ' + x['authors'] + ' ' + str(x['publication_year']), axis=1)
    article = article.drop(['keywords', 'publication_title',
                           'Tokenized', 'Tagged', 'authors', 'publication_year'], axis=1)
    article.reset_index(inplace=True)
    article.set_index('index', inplace=True)
    return article


def get_vectorizer(row):
    vectorizer = TfidfVectorizer(decode_error='ignore', strip_accents='ascii')
    return vectorizer


def get_tfidf_matrix(row):
    tfidf_matrix = row['article_vectorizer'].fit_transform(
        row['article_df']['Tags'])
    return tfidf_matrix


def article_preprocessing(df):
    if JOURNAL_COMPLETE in CACHE:
        return CACHE[JOURNAL_COMPLETE]
    df['article_df'] = df.apply(get_article_df, axis=1)
    df['article_vectorizer'] = df.apply(get_vectorizer, axis=1)
    df['article_matrix'] = df.apply(get_tfidf_matrix, axis=1)
    CACHE[JOURNAL_COMPLETE] = df
    return df


journal_main = article_preprocessing(journal_main)
print('done')


# prediction
journal_threshold = 4


def get_journal_index(user_input):
    user_tfidf = vectorizer.transform([user_input])
    cosine_similarities = cosine_similarity(
        user_tfidf, journal_tfidf_matrix).flatten()
    indices = cosine_similarities.argsort()[::-1]
    top_recommendations = [i for i in indices if cosine_similarities[i] > 0][:min(
        journal_threshold, len(indices))]
    return top_recommendations


article_threshold = 10


def get_article_recommendations(user_input):
    recommended_journals = get_journal_index(user_input)
    recommendations = []
    for journal_id in recommended_journals:
        user_tfidf = journal_main['article_vectorizer'][journal_id].transform([
                                                                              user_input])
        cosine_similarities = cosine_similarity(
            user_tfidf, journal_main['article_matrix'][journal_id]).flatten()
        indices = cosine_similarities.argsort()[::-1]
        top_recommendation_articles = [(cosine_similarities[i], i, journal_id) for i in indices if
                                       cosine_similarities[i] > 0][:min(article_threshold, len(indices))]
        recommendations += top_recommendation_articles
    recommendations.sort(reverse=True)
    return recommendations


def get_links(user_input):
    recommendations = get_article_recommendations(user_input)
    links = []
    for article in recommendations:
        cosine_similarity, article_id, journal_id = article
        link = {
            "title": journal_main['article_df'][journal_id].iloc[article_id, 0],
            "url": journal_main['article_df'][journal_id].iloc[article_id, 1],
            "article_id": int(article_id),
            "journal_id": int(journal_id)
        }
        links.append(link)
    return links


gradio_interface = gradio.Interface(
    fn=get_links,
    inputs="text",
    outputs=gradio.outputs.JSON(),
    examples=[
        ["AI"],
        ["Biochemicals"],
        ["Rocket Science"]
    ],
    title="Sprinkler Article Generator API",
    description="This is a AI powered REST API with Gradio and Huggingface Spaces – for free! Based on [this article](https://www.tomsoderlund.com/ai/building-ai-powered-rest-api). See the **Use via API** link at the bottom of this page.",
    article="© ScholarSync 2023"
)


gradio_interface.launch()


def validation(user_input):
    user_words = set(user_input.lower().split())
    if any(word in stop_words for word in user_words):
        return "valid"
    else:
        return "invalid"

validation_interface = gradio.Interface(
    fn=validation,
    inputs="text",
    outputs="text",
    examples=[
        ["AI"],
        ["Sai"],
        ["Rocket Science"]
    ],
    title="Sprinkler Article Generator API",
    description="This is a AI powered REST API with Gradio and Huggingface Spaces – for free! Based on [this article](https://www.tomsoderlund.com/ai/building-ai-powered-rest-api). See the **Use via API** link at the bottom of this page.",
    article="© ScholarSync 2023"
)

validation_interface.launch()