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