derek-thomas HF staff commited on
Commit
0d73887
1 Parent(s): 9a37e5b

Turning HyDe on!

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -2,6 +2,7 @@ import subprocess
2
 
3
  subprocess.run(["pip", "install", "--upgrade", "transformers[torch,sentencepiece]==4.34.1"])
4
 
 
5
  import logging
6
  from pathlib import Path
7
  from time import perf_counter
@@ -25,7 +26,7 @@ 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
 
@@ -44,12 +45,16 @@ def bot(history, hyde=False):
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...')
54
 
55
  # Create Prompt
@@ -124,14 +129,14 @@ with gr.Blocks() as demo:
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)
 
2
 
3
  subprocess.run(["pip", "install", "--upgrade", "transformers[torch,sentencepiece]==4.34.1"])
4
 
5
+ from functools import partial
6
  import logging
7
  from pathlib import Path
8
  from time import perf_counter
 
26
  template_html = env.get_template('template_html.j2')
27
 
28
  # Examples
29
+ examples = ['What is the capital of China?',
30
  'Why is the sky blue?',
31
  'Who won the mens world cup in 2014?', ]
32
 
 
45
  # Retrieve documents relevant to query
46
  document_start = perf_counter()
47
  if hyde:
48
+ hyde_document = ""
49
  generator = generate(f"Write a wikipedia article intro paragraph to answer this query: {query}", history)
50
  for output_chunk in generator:
51
+ hyde_document = output_chunk
52
+
53
+ logger.warning(hyde_document)
54
+ documents = qd_retriever.retrieve(hyde_document, top_k=top_k)
55
+ else:
56
+ documents = qd_retriever.retrieve(query, top_k=top_k)
57
+ document_time = perf_counter() - document_start
58
  logger.warning(f'Finished Retrieving documents in {round(document_time, 2)} seconds...')
59
 
60
  # Create Prompt
 
129
  hyde_prompt_html = gr.HTML()
130
  # Turn off interactivity while generating if you click
131
  hyde_txt_msg = hyde_txt_btn.click(add_text, [hyde_chatbot, hyde_txt], [hyde_chatbot, hyde_txt], queue=False).then(
132
+ partial(bot, hyde=True), [hyde_chatbot], [hyde_chatbot, hyde_prompt_html])
133
 
134
  # Turn it back on
135
  hyde_txt_msg.then(lambda: gr.Textbox(interactive=True), None, [hyde_txt], queue=False)
136
 
137
  # Turn off interactivity while generating if you hit enter
138
  hyde_txt_msg = hyde_txt.submit(add_text, [hyde_chatbot, hyde_txt], [hyde_chatbot, hyde_txt], queue=False).then(
139
+ partial(bot, hyde=True), [hyde_chatbot], [hyde_chatbot, hyde_prompt_html])
140
 
141
  # Turn it back on
142
  hyde_txt_msg.then(lambda: gr.Textbox(interactive=True), None, [hyde_txt], queue=False)