gamza commited on
Commit
ebfca3b
Β·
1 Parent(s): b9e5f78

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
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()