Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import BertTokenizerFast, BertModel
|
| 2 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
tokenizer_bert = BertTokenizerFast.from_pretrained("kykim/bert-kor-base")
|
| 6 |
+
model_bert = BertModel.from_pretrained("kykim/bert-kor-base")
|
| 7 |
+
|
| 8 |
+
df = pd.read_pickle('BookData_real_real_final.pkl')
|
| 9 |
+
df_emb = pd.read_pickle('review_emb.pkl')
|
| 10 |
+
|
| 11 |
+
title = "πκ³ λ―Ό ν΄κ²° λμ μΆμ² μ±λ΄π"
|
| 12 |
+
description = "λΉμ μ κ³ λ―Ό ν΄κ²°μ λμμ€ μ±
μ μΆμ² ν΄λ립λλ€"
|
| 13 |
+
examples = [["μμ¦ μ μ΄ μ μ"]]
|
| 14 |
+
|
| 15 |
+
def embed_text(text):
|
| 16 |
+
inputs = tokenizer_bert(text, return_tensors="pt")
|
| 17 |
+
outputs = model_bert(**inputs)
|
| 18 |
+
embeddings = outputs.last_hidden_state.mean(dim=1) # νκ· μλ² λ© μ¬μ©
|
| 19 |
+
return embeddings.detach().numpy()[0]
|
| 20 |
+
|
| 21 |
+
def recommend(message):
|
| 22 |
+
|
| 23 |
+
columns = ['거리']
|
| 24 |
+
list_df = pd.DataFrame(columns=columns)
|
| 25 |
+
|
| 26 |
+
emb = embed_text(message)
|
| 27 |
+
list_df['거리'] = df_emb['μνμλ² λ©'].map(lambda x: cosine_similarity([emb], [x]).squeeze())
|
| 28 |
+
answer = df.loc[list_df['거리'].idxmax()]
|
| 29 |
+
book_title = answer['μ λͺ©']
|
| 30 |
+
book_author = answer['μκ°']
|
| 31 |
+
book_publi = answer['μΆνμ¬']
|
| 32 |
+
return "[" + book_author + "] μκ°λμ γ" + book_title + "γ μΆμ²ν©λλ€π" + " (μΆνμ¬:" + book_publi + ")"
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
iface = gr.Interface(fn=recommend,
|
| 36 |
+
inputs="text",
|
| 37 |
+
outputs="text",
|
| 38 |
+
theme="finlaymacklon/boxy_violet",
|
| 39 |
+
title=title,
|
| 40 |
+
description=description,
|
| 41 |
+
examples=examples)
|
| 42 |
+
iface.launch()
|