NinaAchache
commited on
Commit
•
6a89c2d
1
Parent(s):
fd3331f
Correct path
Browse files
app.py
CHANGED
@@ -5,18 +5,17 @@ from haystack.nodes import EmbeddingRetriever
|
|
5 |
import numpy as np
|
6 |
import openai
|
7 |
|
8 |
-
|
|
|
|
|
|
|
9 |
|
|
|
10 |
system_template = {
|
11 |
"role": "system",
|
12 |
-
"content": "You have been a climate change expert for 30 years. You answer questions about climate change in an educationnal and concise manner.",
|
13 |
}
|
14 |
|
15 |
-
|
16 |
-
document_store = FAISSDocumentStore.load(
|
17 |
-
index_path=f"./documents/climate_gpt.faiss",
|
18 |
-
config_path=f"./documents/climate_gpt.json",
|
19 |
-
)
|
20 |
dense = EmbeddingRetriever(
|
21 |
document_store=document_store,
|
22 |
embedding_model="sentence-transformers/multi-qa-mpnet-base-dot-v1",
|
@@ -48,9 +47,11 @@ def gen_conv(query: str, history=[system_template], ipcc=True):
|
|
48 |
if retrieve:
|
49 |
docs = dense.retrieve(query=query, top_k=5)
|
50 |
sources = "\n\n".join(
|
51 |
-
[
|
|
|
|
|
52 |
+ [
|
53 |
-
f"{d.meta['
|
54 |
for d in docs
|
55 |
]
|
56 |
)
|
@@ -65,19 +66,18 @@ def gen_conv(query: str, history=[system_template], ipcc=True):
|
|
65 |
|
66 |
if retrieve:
|
67 |
messages.pop()
|
68 |
-
answer = "(top 5 documents retrieved) " + answer
|
69 |
sources = "\n\n".join(
|
70 |
-
f"{d.meta['
|
71 |
for d in docs
|
72 |
)
|
73 |
-
|
74 |
messages.append({"role": "assistant", "content": answer})
|
75 |
-
|
76 |
gradio_format = make_pairs([a["content"] for a in messages[1:]])
|
77 |
|
78 |
return gradio_format, messages, sources
|
79 |
|
80 |
|
|
|
81 |
def connect(text):
|
82 |
openai.api_key = text
|
83 |
return "You're all set"
|
@@ -92,10 +92,9 @@ with gr.Blocks(title="Eki IPCC Explorer") as demo:
|
|
92 |
result = gr.Textbox(label="Connection")
|
93 |
|
94 |
connect_btn.click(connect, inputs=api_key, outputs=result, api_name="Connection")
|
95 |
-
|
96 |
gr.Markdown(
|
97 |
"""
|
98 |
-
# Ask me anything, I'm
|
99 |
"""
|
100 |
)
|
101 |
|
@@ -121,3 +120,4 @@ with gr.Blocks(title="Eki IPCC Explorer") as demo:
|
|
121 |
)
|
122 |
|
123 |
demo.launch(share=True)
|
|
|
|
5 |
import numpy as np
|
6 |
import openai
|
7 |
|
8 |
+
document_store = FAISSDocumentStore.load(
|
9 |
+
index_path=f"./documents/climate_gpt.faiss",
|
10 |
+
config_path=f"./documents/climate_gpt.json",
|
11 |
+
)
|
12 |
|
13 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
14 |
system_template = {
|
15 |
"role": "system",
|
16 |
+
"content": "You have been a climate change expert for 30 years. You answer questions about climate change in an educationnal and concise manner. Whenever possible your answers are backed up by facts and numbers from scientific reports.",
|
17 |
}
|
18 |
|
|
|
|
|
|
|
|
|
|
|
19 |
dense = EmbeddingRetriever(
|
20 |
document_store=document_store,
|
21 |
embedding_model="sentence-transformers/multi-qa-mpnet-base-dot-v1",
|
|
|
47 |
if retrieve:
|
48 |
docs = dense.retrieve(query=query, top_k=5)
|
49 |
sources = "\n\n".join(
|
50 |
+
[
|
51 |
+
"If relevant, use those extracts in your answer and give the reference of the information you used."
|
52 |
+
]
|
53 |
+ [
|
54 |
+
f"{d.meta['file_name']} Page {d.meta['page_number']}\n{d.content}"
|
55 |
for d in docs
|
56 |
]
|
57 |
)
|
|
|
66 |
|
67 |
if retrieve:
|
68 |
messages.pop()
|
69 |
+
# answer = "(top 5 documents retrieved) " + answer
|
70 |
sources = "\n\n".join(
|
71 |
+
f"{d.meta['file_name']} Page {d.meta['page_number']}:\n{d.content}"
|
72 |
for d in docs
|
73 |
)
|
|
|
74 |
messages.append({"role": "assistant", "content": answer})
|
|
|
75 |
gradio_format = make_pairs([a["content"] for a in messages[1:]])
|
76 |
|
77 |
return gradio_format, messages, sources
|
78 |
|
79 |
|
80 |
+
# Gradio
|
81 |
def connect(text):
|
82 |
openai.api_key = text
|
83 |
return "You're all set"
|
|
|
92 |
result = gr.Textbox(label="Connection")
|
93 |
|
94 |
connect_btn.click(connect, inputs=api_key, outputs=result, api_name="Connection")
|
|
|
95 |
gr.Markdown(
|
96 |
"""
|
97 |
+
# Ask me anything, I'm a climate expert
|
98 |
"""
|
99 |
)
|
100 |
|
|
|
120 |
)
|
121 |
|
122 |
demo.launch(share=True)
|
123 |
+
|