Deepak Sahu commited on
Commit
38d1ee1
·
1 Parent(s): e77e4c7
Files changed (2) hide show
  1. README.md +6 -0
  2. app.py +7 -15
README.md CHANGED
@@ -156,6 +156,12 @@ The values of TOP_P and TOP_K (i.e. token sampling for our generator model) are
156
 
157
  MRR = 0.311 implies that there's a good change that the target book will be in rank (1/.311) ~ 3 (third rank) **i.e. within top 5 recommendations**
158
 
 
 
 
 
 
 
159
 
160
 
161
 
 
156
 
157
  MRR = 0.311 implies that there's a good change that the target book will be in rank (1/.311) ~ 3 (third rank) **i.e. within top 5 recommendations**
158
 
159
+ ## Inference View
160
+
161
+ 1. I rewrote the snippets from `z_evaluate.py` to `app.py` with minor changes to expriment with view.
162
+ 2. To make the HF space faster (previously: 234s; **NOW: 15s**) I added `gr.NO_RELOAD` context block in the module files when loading embedding and generation model.
163
+ Reference: https://www.gradio.app/guides/developing-faster-with-reload-mode
164
+
165
 
166
 
167
 
app.py CHANGED
@@ -53,22 +53,14 @@ def get_recommendation(book_title: str) -> dict:
53
  summaries = df_ranked["summaries"].to_list()[:N_RECOMMENDS]
54
  scores = similarity[ranks][:N_RECOMMENDS]
55
 
56
- output_data_model: list[dict] = [ {"Book": b, "Similarity Score": s, "Description": d} for b, s, d in zip(books, summaries, scores)]
 
 
 
57
 
 
 
58
  # Generate card-style HTML
59
- html = "<div style='display: flex; flex-wrap: wrap; gap: 1rem;'>"
60
- for rec in output_data_model:
61
- html += f"""
62
- <div style='border: 1px solid #ddd; border-radius: 8px; padding: 1rem; width: 200px; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);'>
63
- <h3 style='margin: 0;'>{rec['Book']}</h3>
64
- <p style='margin: 0.5rem 0;'>Similarity Score: {rec['Similarity Score']}</p>
65
- <p style='font-size: 0.9rem; color: #555;'>{rec['Description']}</p>
66
- </div>
67
- """
68
- html += "</div>"
69
- return html
70
-
71
- return {book: score for book, score in zip(books, scores)} # referene: https://huggingface.co/docs/hub/en/spaces-sdks-gradio
72
 
73
  return fake_summaries[0]
74
  # return str(value)
@@ -76,7 +68,7 @@ def get_recommendation(book_title: str) -> dict:
76
  # We instantiate the Textbox class
77
  textbox = gr.Textbox(label="Write random title", placeholder="The Man who knew", lines=2)
78
  # label = gr.Label(label="Result", num_top_classes=N_RECOMMENDS)
79
- output = gr.HTML(label="Recommended Books")
80
 
81
  demo = gr.Interface(fn=get_recommendation, inputs=textbox, outputs=output)
82
 
 
53
  summaries = df_ranked["summaries"].to_list()[:N_RECOMMENDS]
54
  scores = similarity[ranks][:N_RECOMMENDS]
55
 
56
+ # label wise similarity
57
+ label_similarity: dict = {book: score for book, score in zip(books, scores)}
58
+ #
59
+ book_summaries: list[str] = [f"**{book}** \n {summary}" for book, summary in zip(books, summaries)]
60
 
61
+ response = [label_similarity, ] + book_summaries
62
+ return response
63
  # Generate card-style HTML
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  return fake_summaries[0]
66
  # return str(value)
 
68
  # We instantiate the Textbox class
69
  textbox = gr.Textbox(label="Write random title", placeholder="The Man who knew", lines=2)
70
  # label = gr.Label(label="Result", num_top_classes=N_RECOMMENDS)
71
+ output = [gr.Label(label="Result", num_top_classes=N_RECOMMENDS)] + [gr.Textbox(label="Recommendation") for i in range(N_RECOMMENDS)]
72
 
73
  demo = gr.Interface(fn=get_recommendation, inputs=textbox, outputs=output)
74