derek-thomas HF staff commited on
Commit
378c66b
1 Parent(s): 4ecb059

Adding HyDE and updating the colors

Browse files
Files changed (2) hide show
  1. app.py +53 -7
  2. templates/template_html.j2 +64 -17
app.py CHANGED
@@ -24,6 +24,11 @@ env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
24
  template = env.get_template('template.j2')
25
  template_html = env.get_template('template_html.j2')
26
 
 
 
 
 
 
27
 
28
  def add_text(history, text):
29
  history = [] if history is None else history
@@ -31,13 +36,18 @@ def add_text(history, text):
31
  return history, gr.Textbox(value="", interactive=False)
32
 
33
 
34
- def bot(history, system_prompt=""):
35
  top_k = 4
36
  query = history[-1][0]
37
 
38
  logger.warning('Retrieving documents...')
39
  # Retrieve documents relevant to query
40
  document_start = perf_counter()
 
 
 
 
 
41
  documents = qd_retriever.retrieve(query, top_k=top_k)
42
  document_time = document_start - perf_counter()
43
  logger.warning(f'Finished Retrieving documents in {round(document_time, 2)} seconds...')
@@ -45,7 +55,6 @@ def bot(history, system_prompt=""):
45
  # Create Prompt
46
  prompt = template.render(documents=documents, query=query)
47
  prompt_html = template_html.render(documents=documents, query=query)
48
- logger.warning(prompt)
49
 
50
  history[-1][1] = ""
51
  for character in generate(prompt, history[:-1]):
@@ -54,7 +63,7 @@ def bot(history, system_prompt=""):
54
 
55
 
56
  with gr.Blocks() as demo:
57
- with gr.Tab("Application"):
58
  chatbot = gr.Chatbot(
59
  [],
60
  elem_id="chatbot",
@@ -75,7 +84,7 @@ with gr.Blocks() as demo:
75
  txt_btn = gr.Button(value="Submit text", scale=1)
76
 
77
  prompt_html = gr.HTML()
78
- # Turn off interactivity while generating if you hit enter
79
  txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
80
  bot, chatbot, [chatbot, prompt_html])
81
 
@@ -89,9 +98,46 @@ with gr.Blocks() as demo:
89
  # Turn it back on
90
  txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
91
 
92
- gr.Examples(['What is the capital of China, I think its Shanghai?',
93
- 'Why is the sky blue?',
94
- 'Who won the mens world cup in 2014?',], txt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  demo.queue()
97
  demo.launch(debug=True)
 
24
  template = env.get_template('template.j2')
25
  template_html = env.get_template('template_html.j2')
26
 
27
+ # Examples
28
+ examples = ['What is the capital of China, I think its Shanghai?',
29
+ 'Why is the sky blue?',
30
+ 'Who won the mens world cup in 2014?', ]
31
+
32
 
33
  def add_text(history, text):
34
  history = [] if history is None else history
 
36
  return history, gr.Textbox(value="", interactive=False)
37
 
38
 
39
+ def bot(history, hyde=False):
40
  top_k = 4
41
  query = history[-1][0]
42
 
43
  logger.warning('Retrieving documents...')
44
  # Retrieve documents relevant to query
45
  document_start = perf_counter()
46
+ if hyde:
47
+ complete_output = ""
48
+ generator = generate(f"Write a wikipedia article intro paragraph to answer this query: {query}", history)
49
+ for output_chunk in generator:
50
+ complete_output += output_chunk
51
  documents = qd_retriever.retrieve(query, top_k=top_k)
52
  document_time = document_start - perf_counter()
53
  logger.warning(f'Finished Retrieving documents in {round(document_time, 2)} seconds...')
 
55
  # Create Prompt
56
  prompt = template.render(documents=documents, query=query)
57
  prompt_html = template_html.render(documents=documents, query=query)
 
58
 
59
  history[-1][1] = ""
60
  for character in generate(prompt, history[:-1]):
 
63
 
64
 
65
  with gr.Blocks() as demo:
66
+ with gr.Tab("RAGDemo"):
67
  chatbot = gr.Chatbot(
68
  [],
69
  elem_id="chatbot",
 
84
  txt_btn = gr.Button(value="Submit text", scale=1)
85
 
86
  prompt_html = gr.HTML()
87
+ # Turn off interactivity while generating if you click
88
  txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
89
  bot, chatbot, [chatbot, prompt_html])
90
 
 
98
  # Turn it back on
99
  txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
100
 
101
+ # Examples
102
+ gr.Examples(examples, txt)
103
+
104
+ with gr.Tab("RAGDemo + HyDE"):
105
+ hyde_chatbot = gr.Chatbot(
106
+ [],
107
+ elem_id="chatbot",
108
+ avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
109
+ 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
110
+ bubble_full_width=False,
111
+ show_copy_button=True,
112
+ show_share_button=True,
113
+ )
114
+
115
+ with gr.Row():
116
+ hyde_txt = gr.Textbox(
117
+ scale=3,
118
+ show_label=False,
119
+ placeholder="Enter text and press enter",
120
+ container=False,
121
+ )
122
+ hyde_txt_btn = gr.Button(value="Submit text", scale=1)
123
+
124
+ hyde_prompt_html = gr.HTML()
125
+ # Turn off interactivity while generating if you click
126
+ hyde_txt_msg = hyde_txt_btn.click(add_text, [hyde_chatbot, hyde_txt], [hyde_chatbot, hyde_txt], queue=False).then(
127
+ bot, hyde_chatbot, [hyde_chatbot, hyde_prompt_html])
128
+
129
+ # Turn it back on
130
+ hyde_txt_msg.then(lambda: gr.Textbox(interactive=True), None, [hyde_txt], queue=False)
131
+
132
+ # Turn off interactivity while generating if you hit enter
133
+ hyde_txt_msg = hyde_txt.submit(add_text, [hyde_chatbot, hyde_txt], [hyde_chatbot, hyde_txt], queue=False).then(
134
+ bot, hyde_chatbot, [hyde_chatbot, hyde_prompt_html])
135
+
136
+ # Turn it back on
137
+ hyde_txt_msg.then(lambda: gr.Textbox(interactive=True), None, [hyde_txt], queue=False)
138
+
139
+ # Examples
140
+ gr.Examples(examples, hyde_txt)
141
 
142
  demo.queue()
143
  demo.launch(debug=True)
templates/template_html.j2 CHANGED
@@ -1,7 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <h2>Prompt</h2>
2
  Below is the prompt that is given to the model. <hr>
3
  <h2>Instructions</h2>
4
- <span style="color: #FF00FF;">Use the following pieces of context to answer the question at the end.<br>If you don't know the answer, just say that you don't know, <span style="color: #FF00FF; font-weight: bold;">don't try to make up an answer.</span></span><br>
5
  <h2>Context</h2>
6
  {% for doc in documents %}
7
  <details class="doc-box">
@@ -12,22 +70,9 @@ Below is the prompt that is given to the model. <hr>
12
  </details>
13
  {% endfor %}
14
 
15
- <h2>Query</h2> <span style="color: #801616;">{{ query }}</span>
16
-
17
- <style>
18
- .doc-box {
19
- padding: 10px;
20
- margin-top: 10px;
21
- background-color: #48a3ff;
22
- border: none;
23
- }
24
- .doc-short, .doc-full {
25
- color: white;
26
- }
27
- summary::-webkit-details-marker {
28
- color: white;
29
- }
30
- </style>
31
 
32
  <script>
33
  document.addEventListener("DOMContentLoaded", function() {
@@ -45,3 +90,5 @@ document.addEventListener("DOMContentLoaded", function() {
45
  });
46
  });
47
  </script>
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Information Page</title>
7
+ <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&amp;display=swap">
8
+ <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600&amp;display=swap">
9
+ <style>
10
+ * {
11
+ font-family: "Source Sans Pro";
12
+ }
13
+
14
+ .dark {
15
+ background: #111;
16
+ color: white;
17
+ }
18
+
19
+ .doc-box {
20
+ padding: 10px;
21
+ margin-top: 10px;
22
+ background-color: #baecc2;
23
+ border-radius: 6px;
24
+ color: #111;
25
+ max-width: 700px;
26
+ box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 2px 0px;
27
+ }
28
+
29
+ .doc-full {
30
+ margin: 10px 14px;
31
+ line-height: 1.6rem;
32
+ }
33
+
34
+ .instructions {
35
+ color: #111;
36
+ background: #b7bdfd;
37
+ display: block;
38
+ border-radius: 6px;
39
+ padding: 6px 10px;
40
+ line-height: 1.6rem;
41
+ max-width: 700px;
42
+ box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 2px 0px;
43
+ }
44
+
45
+ .query {
46
+ color: #111;
47
+ background: #ffbcbc;
48
+ display: block;
49
+ border-radius: 6px;
50
+ padding: 6px 10px;
51
+ line-height: 1.6rem;
52
+ max-width: 700px;
53
+ box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 2px 0px;
54
+ }
55
+ </style>
56
+ </head>
57
+ <body>
58
+ <div class="prose svelte-1ybaih5" id="component-6">
59
  <h2>Prompt</h2>
60
  Below is the prompt that is given to the model. <hr>
61
  <h2>Instructions</h2>
62
+ <span class="instructions">Use the following pieces of context to answer the question at the end.<br>If you don't know the answer, just say that you don't know, <span style="font-weight: bold;">don't try to make up an answer.</span></span><br>
63
  <h2>Context</h2>
64
  {% for doc in documents %}
65
  <details class="doc-box">
 
70
  </details>
71
  {% endfor %}
72
 
73
+ <h2>Query</h2>
74
+ <span class="query">{{ query }}</span>
75
+ </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  <script>
78
  document.addEventListener("DOMContentLoaded", function() {
 
90
  });
91
  });
92
  </script>
93
+ </body>
94
+ </html>