derek-thomas HF staff commited on
Commit
629069a
1 Parent(s): cf3e0e9

Refactoring jinja code

Browse files
Files changed (3) hide show
  1. app.py +16 -55
  2. templates/template.j2 +8 -0
  3. templates/template_html.j2 +60 -0
app.py CHANGED
@@ -1,60 +1,21 @@
1
  from time import perf_counter
2
 
3
  import gradio as gr
4
- from jinja2 import Template
5
 
6
  from backend.query_llm import generate
7
  from backend.semantic_search import qd_retriever
 
8
 
9
- template_string = """
10
- Instructions: Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
11
- Context:
12
- ---
13
- {% for doc in documents %}
14
- {{ doc.content }}
15
- ---
16
- {% endfor %}
17
- Query: {{ query }}
18
- """
19
-
20
- md_template_string = """
21
- <b>Instructions</b>:
22
- <span style="color: green;">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: green; font-weight: bold;">don't try to make up an answer.</span></span><br>
23
-
24
- <b>Context</b>:
25
- {% for doc in documents %}
26
- <div id=\"box{{ loop.index }}\" style=\"border: 2px solid #aaa; padding: 10px; margin-top: 10px; border-radius: 5px; background-color: #1E90FF; position: relative; cursor: pointer;\">
27
- <div style=\"font-size: 0.8em; position: absolute; top: 10px; left: 10px;\"><b>Doc {{ loop.index }}</b></div>
28
- <span id=\"doc{{ loop.index }}-short\" style=\"color: white; display: block; margin-top: 20px;\">{{ doc.content[:50] }}...</span>
29
- <span id=\"doc{{ loop.index }}-full\" style=\"color: white; display: none; margin-top: 20px;\">{{ doc.content }}</span>
30
- </div>
31
- {% endfor %}
32
- <b>Query</b>: <span style=\"color: yellow;\">{{ query }}</span>
33
- <script>
34
- document.addEventListener("DOMContentLoaded", function() {
35
- {% for doc in documents %}
36
- document.getElementById("box{{ loop.index }}").addEventListener("click", function() {
37
- toggleContent('doc{{ loop.index }}');
38
- });
39
- {% endfor %}
40
- });
41
-
42
- function toggleContent(docID) {
43
- var shortContent = document.getElementById(docID + '-short');
44
- var fullContent = document.getElementById(docID + '-full');
45
- if (fullContent.style.display === 'none') {
46
- shortContent.style.display = 'none';
47
- fullContent.style.display = 'block';
48
- } else {
49
- shortContent.style.display = 'block';
50
- fullContent.style.display = 'none';
51
- }
52
- }
53
- </script>
54
- """
55
-
56
- template = Template(template_string)
57
- md_template = Template(md_template_string)
58
 
59
 
60
  def add_text(history, text):
@@ -74,12 +35,12 @@ def bot(history, system_prompt=""):
74
 
75
  # Create Prompt
76
  prompt = template.render(documents=documents, query=query)
77
- md_prompt = md_template.render(documents=documents, query=query)
78
 
79
  history[-1][1] = ""
80
  for character in generate(prompt, history[:-1]):
81
  history[-1][1] = character
82
- yield history, md_prompt
83
 
84
 
85
  with gr.Blocks() as demo:
@@ -102,17 +63,17 @@ with gr.Blocks() as demo:
102
  )
103
  txt_btn = gr.Button(value="Submit text", scale=1)
104
 
105
- prompt_md = gr.HTML()
106
  # Turn off interactivity while generating if you hit enter
107
  txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
108
- bot, chatbot, [chatbot, prompt_md])
109
 
110
  # Turn it back on
111
  txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
112
 
113
  # Turn off interactivity while generating if you hit enter
114
  txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
115
- bot, chatbot, [chatbot, prompt_md])
116
 
117
  # Turn it back on
118
  txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
 
1
  from time import perf_counter
2
 
3
  import gradio as gr
4
+ from jinja2 import Environment, FileSystemLoader
5
 
6
  from backend.query_llm import generate
7
  from backend.semantic_search import qd_retriever
8
+ from pathlib import Path
9
 
10
+ proj_dir = Path(__file__).parent
11
+
12
+
13
+ # Set up the template environment with the templates directory
14
+ env = Environment(loader=FileSystemLoader(proj_dir/'templates'))
15
+
16
+ # Load the templates directly from the environment
17
+ template = env.get_template('template.j2')
18
+ template_html = env.get_template('template_html.j2')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
 
21
  def add_text(history, text):
 
35
 
36
  # Create Prompt
37
  prompt = template.render(documents=documents, query=query)
38
+ prompt_html = template_html.render(documents=documents, query=query)
39
 
40
  history[-1][1] = ""
41
  for character in generate(prompt, history[:-1]):
42
  history[-1][1] = character
43
+ yield history, prompt_html
44
 
45
 
46
  with gr.Blocks() as demo:
 
63
  )
64
  txt_btn = gr.Button(value="Submit text", scale=1)
65
 
66
+ prompt_html = gr.HTML()
67
  # Turn off interactivity while generating if you hit enter
68
  txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
69
+ bot, chatbot, [chatbot, prompt_html])
70
 
71
  # Turn it back on
72
  txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
73
 
74
  # Turn off interactivity while generating if you hit enter
75
  txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
76
+ bot, chatbot, [chatbot, prompt_html])
77
 
78
  # Turn it back on
79
  txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
templates/template.j2 ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ Instructions: Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
2
+ Context:
3
+ ---
4
+ {% for doc in documents %}
5
+ {{ doc.content }}
6
+ ---
7
+ {% endfor %}
8
+ Query: {{ query }}
templates/template_html.j2 ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <b>Instructions</b>:
2
+ <span style="color: green;">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: green; font-weight: bold;">don't try to make up an answer.</span></span><br>
3
+
4
+ <b>Context</b>:
5
+ {% for doc in documents %}
6
+ <div id="box{{ loop.index }}" class="doc-box">
7
+ <div class="doc-label"><b>Doc {{ loop.index }}</b></div>
8
+ <span id="doc{{ loop.index }}-short" class="doc-short">{{ doc.content[:50] }}...</span>
9
+ <span id="doc{{ loop.index }}-full" class="doc-full">{{ doc.content }}</span>
10
+ </div>
11
+ {% endfor %}
12
+ <b>Query</b>: <span style="color: yellow;">{{ query }}</span>
13
+
14
+ <style>
15
+ .doc-box {
16
+ border: 2px solid #aaa;
17
+ padding: 10px;
18
+ margin-top: 10px;
19
+ border-radius: 5px;
20
+ background-color: #1E90FF;
21
+ position: relative;
22
+ cursor: pointer;
23
+ }
24
+ .doc-label {
25
+ font-size: 0.8em;
26
+ position: absolute;
27
+ top: 10px;
28
+ left: 10px;
29
+ }
30
+ .doc-short, .doc-full {
31
+ color: white;
32
+ display: block;
33
+ margin-top: 20px;
34
+ }
35
+ .doc-full {
36
+ display: none;
37
+ }
38
+ </style>
39
+
40
+ <script>
41
+ document.addEventListener("DOMContentLoaded", function() {
42
+ {% for doc in documents %}
43
+ document.getElementById("box{{ loop.index }}").addEventListener("click", function() {
44
+ toggleContent('doc{{ loop.index }}');
45
+ });
46
+ {% endfor %}
47
+ });
48
+
49
+ function toggleContent(docID) {
50
+ var shortContent = document.getElementById(docID + '-short');
51
+ var fullContent = document.getElementById(docID + '-full');
52
+ if (fullContent.style.display === 'none') {
53
+ shortContent.style.display = 'none';
54
+ fullContent.style.display = 'block';
55
+ } else {
56
+ shortContent.style.display = 'block';
57
+ fullContent.style.display = 'none';
58
+ }
59
+ }
60
+ </script>