Spaces:
Running
Running
Deepak Sahu
commited on
Commit
·
9062ce3
1
Parent(s):
86af19f
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,6 @@
|
|
1 |
-
#
|
2 |
-
|
3 |
-
|
4 |
-
# # CONST
|
5 |
-
# SUMMARY_VECTORS = "app_cache/summary_vectors.npy"
|
6 |
-
# BOOKS_CSV = "clean_books_summary.csv"
|
7 |
|
8 |
# def get_recommendation(book_title: str) -> str:
|
9 |
# return book_title
|
@@ -31,22 +28,39 @@
|
|
31 |
# Reference: https://huggingface.co/learn/nlp-course/en/chapter9/2
|
32 |
|
33 |
import gradio as gr
|
34 |
-
|
35 |
from z_hypothetical_summary import generate_summaries
|
|
|
|
|
|
|
36 |
|
37 |
-
|
|
|
38 |
global generator_model
|
39 |
# return "Hello"
|
40 |
# # Generate hypothetical summary
|
41 |
# value = generator_model("hello", max_length=50)
|
42 |
fake_summaries = generate_summaries(book_title=book_title, n_samples=5) # other parameters are set to default in the function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
return fake_summaries[0]
|
44 |
# return str(value)
|
45 |
|
46 |
# We instantiate the Textbox class
|
47 |
-
textbox = gr.Textbox(label="Write
|
48 |
-
|
49 |
|
50 |
-
demo = gr.Interface(fn=get_recommendation, inputs=textbox, outputs=
|
51 |
|
52 |
demo.launch()
|
|
|
1 |
+
# CONST
|
2 |
+
CLEAN_DF_UNIQUE_TITLES = "unique_titles_books_summary.csv"
|
3 |
+
N_RECOMMENDS = 5
|
|
|
|
|
|
|
4 |
|
5 |
# def get_recommendation(book_title: str) -> str:
|
6 |
# return book_title
|
|
|
28 |
# Reference: https://huggingface.co/learn/nlp-course/en/chapter9/2
|
29 |
|
30 |
import gradio as gr
|
31 |
+
from z_similarity import computes_similarity_w_hypothetical
|
32 |
from z_hypothetical_summary import generate_summaries
|
33 |
+
from z_utils import get_dataframe
|
34 |
+
|
35 |
+
books_df = get_dataframe(CLEAN_DF_UNIQUE_TITLES)
|
36 |
|
37 |
+
|
38 |
+
def get_recommendation(book_title: str) -> dict:
|
39 |
global generator_model
|
40 |
# return "Hello"
|
41 |
# # Generate hypothetical summary
|
42 |
# value = generator_model("hello", max_length=50)
|
43 |
fake_summaries = generate_summaries(book_title=book_title, n_samples=5) # other parameters are set to default in the function
|
44 |
+
|
45 |
+
# Compute Simialrity
|
46 |
+
similarity, ranks = computes_similarity_w_hypothetical(hypothetical_summaries=fake_summaries)
|
47 |
+
|
48 |
+
# Get ranked Documents
|
49 |
+
df_ranked = books_df.iloc[ranks]
|
50 |
+
df_ranked = df_ranked.reset_index()
|
51 |
+
|
52 |
+
books = df_ranked["book_name"][:N_RECOMMENDS]
|
53 |
+
scores = similarity[ranks][:N_RECOMMENDS]
|
54 |
+
|
55 |
+
return {book: score for book, score in zip(books, scores)} # referene: https://huggingface.co/docs/hub/en/spaces-sdks-gradio
|
56 |
+
|
57 |
return fake_summaries[0]
|
58 |
# return str(value)
|
59 |
|
60 |
# We instantiate the Textbox class
|
61 |
+
textbox = gr.Textbox(label="Write random title", placeholder="The Man who knew", lines=2)
|
62 |
+
label = gr.Label(label="Result", num_top_classes=N_RECOMMENDS)
|
63 |
|
64 |
+
demo = gr.Interface(fn=get_recommendation, inputs=textbox, outputs=label)
|
65 |
|
66 |
demo.launch()
|