Spaces:
Build error
Build error
Commit
·
8de88bd
1
Parent(s):
91f49a8
Adding beautiful formatting
Browse files- app.py +60 -9
- requirements.txt +0 -1
- templates/template_html.j2 +106 -42
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import logging
|
2 |
from pathlib import Path
|
3 |
from time import perf_counter
|
@@ -24,6 +25,11 @@ template_html = env.get_template('template_html.j2')
|
|
24 |
# Initialize tokenizer
|
25 |
tokenizer = AutoTokenizer.from_pretrained('derek-thomas/jais-13b-chat-hf')
|
26 |
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def add_text(history, text):
|
29 |
history = [] if history is None else history
|
@@ -31,15 +37,24 @@ def add_text(history, text):
|
|
31 |
return history, gr.Textbox(value="", interactive=False)
|
32 |
|
33 |
|
34 |
-
def bot(history,
|
35 |
top_k = 5
|
36 |
query = history[-1][0]
|
37 |
|
38 |
logger.warning('Retrieving documents...')
|
39 |
# Retrieve documents relevant to query
|
40 |
document_start = perf_counter()
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
logger.warning(f'Finished Retrieving documents in {round(document_time, 2)} seconds...')
|
44 |
|
45 |
|
@@ -67,8 +82,8 @@ def bot(history, system_prompt=""):
|
|
67 |
|
68 |
|
69 |
with gr.Blocks() as demo:
|
70 |
-
|
71 |
-
|
72 |
chatbot = gr.Chatbot(
|
73 |
[],
|
74 |
elem_id="chatbot",
|
@@ -88,8 +103,9 @@ with gr.Blocks() as demo:
|
|
88 |
)
|
89 |
txt_btn = gr.Button(value="Submit text", scale=1)
|
90 |
|
|
|
91 |
prompt_html = gr.HTML()
|
92 |
-
# Turn off interactivity while generating if you
|
93 |
txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
94 |
bot, chatbot, [chatbot, prompt_html])
|
95 |
|
@@ -103,9 +119,44 @@ with gr.Blocks() as demo:
|
|
103 |
# Turn it back on
|
104 |
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
demo.queue()
|
111 |
demo.launch(debug=True)
|
|
|
1 |
+
from functools import partial
|
2 |
import logging
|
3 |
from pathlib import Path
|
4 |
from time import perf_counter
|
|
|
25 |
# Initialize tokenizer
|
26 |
tokenizer = AutoTokenizer.from_pretrained('derek-thomas/jais-13b-chat-hf')
|
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 |
+
|
33 |
|
34 |
def add_text(history, text):
|
35 |
history = [] if history is None else history
|
|
|
37 |
return history, gr.Textbox(value="", interactive=False)
|
38 |
|
39 |
|
40 |
+
def bot(history, hyde=False):
|
41 |
top_k = 5
|
42 |
query = history[-1][0]
|
43 |
|
44 |
logger.warning('Retrieving documents...')
|
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}")
|
50 |
+
for output_chunk in generator:
|
51 |
+
hyde_document = output_chunk
|
52 |
+
|
53 |
+
logger.warning(hyde_document)
|
54 |
+
documents = retriever(hyde_document, top_k=top_k)
|
55 |
+
else:
|
56 |
+
documents = retriever(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 |
|
|
|
82 |
|
83 |
|
84 |
with gr.Blocks() as demo:
|
85 |
+
endpoint_status = gr.Textbox(check_endpoint_status, label="Endpoint Status (send chat to wake up)", every=1)
|
86 |
+
with gr.Tab("Arabic-RAG"):
|
87 |
chatbot = gr.Chatbot(
|
88 |
[],
|
89 |
elem_id="chatbot",
|
|
|
103 |
)
|
104 |
txt_btn = gr.Button(value="Submit text", scale=1)
|
105 |
|
106 |
+
gr.Examples(examples, txt)
|
107 |
prompt_html = gr.HTML()
|
108 |
+
# Turn off interactivity while generating if you click
|
109 |
txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
110 |
bot, chatbot, [chatbot, prompt_html])
|
111 |
|
|
|
119 |
# Turn it back on
|
120 |
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
121 |
|
122 |
+
|
123 |
+
with gr.Tab("Arabic-RAG + HyDE"):
|
124 |
+
hyde_chatbot = gr.Chatbot(
|
125 |
+
[],
|
126 |
+
elem_id="chatbot",
|
127 |
+
avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
|
128 |
+
'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
|
129 |
+
bubble_full_width=False,
|
130 |
+
show_copy_button=True,
|
131 |
+
show_share_button=True,
|
132 |
+
)
|
133 |
+
|
134 |
+
with gr.Row():
|
135 |
+
hyde_txt = gr.Textbox(
|
136 |
+
scale=3,
|
137 |
+
show_label=False,
|
138 |
+
placeholder="Enter text and press enter",
|
139 |
+
container=False,
|
140 |
+
)
|
141 |
+
hyde_txt_btn = gr.Button(value="Submit text", scale=1)
|
142 |
+
|
143 |
+
gr.Examples(examples, hyde_txt)
|
144 |
+
hyde_prompt_html = gr.HTML()
|
145 |
+
# Turn off interactivity while generating if you click
|
146 |
+
hyde_txt_msg = hyde_txt_btn.click(add_text, [hyde_chatbot, hyde_txt], [hyde_chatbot, hyde_txt], queue=False).then(
|
147 |
+
partial(bot, hyde=True), [hyde_chatbot], [hyde_chatbot, hyde_prompt_html])
|
148 |
+
|
149 |
+
# Turn it back on
|
150 |
+
hyde_txt_msg.then(lambda: gr.Textbox(interactive=True), None, [hyde_txt], queue=False)
|
151 |
+
|
152 |
+
# Turn off interactivity while generating if you hit enter
|
153 |
+
hyde_txt_msg = hyde_txt.submit(add_text, [hyde_chatbot, hyde_txt], [hyde_chatbot, hyde_txt], queue=False).then(
|
154 |
+
partial(bot, hyde=True), [hyde_chatbot], [hyde_chatbot, hyde_prompt_html])
|
155 |
+
|
156 |
+
# Turn it back on
|
157 |
+
hyde_txt_msg.then(lambda: gr.Textbox(interactive=True), None, [hyde_txt], queue=False)
|
158 |
+
|
159 |
+
# Examples
|
160 |
|
161 |
demo.queue()
|
162 |
demo.launch(debug=True)
|
requirements.txt
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
wikiextractor==3.0.6
|
2 |
sentence-transformers>2.2.0
|
3 |
-
qdrant-haystack==1.0.10
|
4 |
ipywidgets==8.1.1
|
5 |
tqdm==4.66.1
|
6 |
aiohttp==3.8.6
|
|
|
1 |
wikiextractor==3.0.6
|
2 |
sentence-transformers>2.2.0
|
|
|
3 |
ipywidgets==8.1.1
|
4 |
tqdm==4.66.1
|
5 |
aiohttp==3.8.6
|
templates/template_html.j2
CHANGED
@@ -1,47 +1,111 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
<
|
4 |
-
<
|
5 |
-
<
|
6 |
-
|
7 |
-
<
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
<script>
|
33 |
-
document.addEventListener("DOMContentLoaded", function() {
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
44 |
});
|
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"
|
8 |
+
href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap">
|
9 |
+
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600&display=swap">
|
10 |
+
<style>
|
11 |
+
* {
|
12 |
+
font-family: "Source Sans Pro";
|
13 |
+
}
|
14 |
+
|
15 |
+
.instructions > * {
|
16 |
+
color: #111 !important;
|
17 |
+
}
|
18 |
+
|
19 |
+
details.doc-box * {
|
20 |
+
color: #111 !important;
|
21 |
+
}
|
22 |
+
|
23 |
+
details.doc-box a {
|
24 |
+
color: #0000EE !important; /* This is a common blue color for links */
|
25 |
+
text-decoration: underline; /* This underlines the link */
|
26 |
+
}
|
27 |
+
|
28 |
+
.dark {
|
29 |
+
background: #111;
|
30 |
+
color: white;
|
31 |
+
}
|
32 |
+
|
33 |
+
.doc-box {
|
34 |
+
padding: 10px;
|
35 |
+
margin-top: 10px;
|
36 |
+
background-color: #baecc2;
|
37 |
+
border-radius: 6px;
|
38 |
+
color: #111 !important;
|
39 |
+
max-width: 700px;
|
40 |
+
box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 2px 0px;
|
41 |
+
}
|
42 |
+
|
43 |
+
.doc-full {
|
44 |
+
margin: 10px 14px;
|
45 |
+
line-height: 1.6rem;
|
46 |
+
}
|
47 |
+
|
48 |
+
.instructions {
|
49 |
+
color: #111 !important;
|
50 |
+
background: #b7bdfd;
|
51 |
+
display: block;
|
52 |
+
border-radius: 6px;
|
53 |
+
padding: 6px 10px;
|
54 |
+
line-height: 1.6rem;
|
55 |
+
max-width: 700px;
|
56 |
+
box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 2px 0px;
|
57 |
+
}
|
58 |
+
|
59 |
+
.query {
|
60 |
+
color: #111 !important;
|
61 |
+
background: #ffbcbc;
|
62 |
+
display: block;
|
63 |
+
border-radius: 6px;
|
64 |
+
padding: 6px 10px;
|
65 |
+
line-height: 1.6rem;
|
66 |
+
max-width: 700px;
|
67 |
+
box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 2px 0px;
|
68 |
+
}
|
69 |
+
</style>
|
70 |
+
</head>
|
71 |
+
<body>
|
72 |
+
<div class="prose svelte-1ybaih5" id="component-6">
|
73 |
+
<h2>Prompt</h2>
|
74 |
+
Below is the prompt that is given to the model.
|
75 |
+
<hr>
|
76 |
+
<h2>Instructions</h2>
|
77 |
+
<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
|
78 |
+
style="font-weight: bold;">don't try to make up an answer.</span></span><br>
|
79 |
+
<h2>Context</h2>
|
80 |
+
{% for doc in documents %}
|
81 |
+
<details class="doc-box" dir="rtl">
|
82 |
+
<summary>
|
83 |
+
<b>وثيقة_{{ loop.index }} | <a href="{{ doc.meta.url }}">{{ doc.meta.title }}</a>:</b> <span
|
84 |
+
class="doc-short">{{ doc.content[:50] }}...</span>
|
85 |
+
</summary>
|
86 |
+
<div class="doc-full"><a href="{{ doc.meta.url }}">{{ doc.meta.title }}</a>: {{ doc.content }}</div>
|
87 |
+
</details>
|
88 |
+
{% endfor %}
|
89 |
+
|
90 |
+
<h2>Query</h2>
|
91 |
+
<span class="query">{{ query }}</span>
|
92 |
+
</div>
|
93 |
|
94 |
<script>
|
95 |
+
document.addEventListener("DOMContentLoaded", function () {
|
96 |
+
const detailsElements = document.querySelectorAll('.doc-box');
|
97 |
+
|
98 |
+
detailsElements.forEach(detail => {
|
99 |
+
detail.addEventListener('toggle', function () {
|
100 |
+
const docShort = this.querySelector('.doc-short');
|
101 |
+
if (this.open) {
|
102 |
+
docShort.style.display = 'none';
|
103 |
+
} else {
|
104 |
+
docShort.style.display = 'inline';
|
105 |
+
}
|
106 |
+
});
|
107 |
});
|
108 |
});
|
|
|
109 |
</script>
|
110 |
+
</body>
|
111 |
+
</html>
|