Nina commited on
Commit
bf93486
1 Parent(s): 611845e

Adding explanation & background & utils file

Browse files
Files changed (2) hide show
  1. app.py +47 -42
  2. utils.py +46 -0
app.py CHANGED
@@ -5,11 +5,14 @@ from haystack.nodes import EmbeddingRetriever
5
  import numpy as np
6
  import openai
7
  import os
8
-
 
 
 
9
 
10
  document_store = FAISSDocumentStore.load(
11
- index_path=f"./documents/climate_gpt.faiss",
12
- config_path=f"./documents/climate_gpt.json",
13
  )
14
 
15
  classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
@@ -22,22 +25,19 @@ dense = EmbeddingRetriever(
22
  )
23
 
24
 
25
- def is_climate_change_related(sentence: str) -> bool:
26
- results = classifier(
27
- sequences=sentence,
28
- candidate_labels=["climate change related", "non climate change related"],
29
- )
30
- return results["labels"][np.argmax(results["scores"])] == "climate change related"
31
-
32
 
33
- def make_pairs(lst):
34
- """from a list of even lenght, make tupple pairs"""
35
- return [(lst[i], lst[i + 1]) for i in range(0, len(lst), 2)]
 
36
 
 
 
 
 
37
 
38
- def gen_conv(query: str, history=[system_template], ipcc=True):
39
- """return (answer:str, history:list[dict], sources:str)"""
40
- retrieve = ipcc and is_climate_change_related(query)
41
  sources = ""
42
  messages = history + [
43
  {"role": "user", "content": query},
@@ -68,55 +68,60 @@ def gen_conv(query: str, history=[system_template], ipcc=True):
68
  f"{d.meta['file_name']} Page {d.meta['page_number']}:\n{d.content}"
69
  for d in docs
70
  )
 
 
 
71
  messages.append({"role": "assistant", "content": answer})
72
  gradio_format = make_pairs([a["content"] for a in messages[1:]])
73
 
74
  return gradio_format, messages, sources
75
 
76
 
77
- def set_openai_api_key(text):
78
- """Set the api key and return chain.
79
- If no api_key, then None is returned.
80
- """
81
- openai.api_key = os.environ["api_key"]
82
-
83
- if text.startswith("sk-") and len(text) > 10:
84
- openai.api_key = text
85
- return f"You're all set: this is your api key: {openai.api_key}"
86
 
 
87
 
88
- # Gradio
89
- with gr.Blocks(title="Eki IPCC Explorer") as demo:
90
  openai.api_key = os.environ["api_key"]
91
- gr.Markdown("# Climate GPT")
92
- # with gr.Row():
93
- # gr.Markdown("First step: Add your OPENAI api key")
94
- # openai_api_key_textbox = gr.Textbox(
95
- # placeholder="Paste your OpenAI API key (sk-...) and hit Enter",
96
- # show_label=False,
97
- # lines=1,
98
- # type="password",
99
- # )
100
-
101
- gr.Markdown("""# Ask me anything, I'm a climate expert""")
 
 
 
 
 
102
  with gr.Row():
103
  with gr.Column(scale=2):
104
- chatbot = gr.Chatbot()
105
  state = gr.State([system_template])
106
 
107
  with gr.Row():
108
  ask = gr.Textbox(
109
- show_label=False, placeholder="Enter text and press enter"
 
 
110
  ).style(container=False)
 
111
 
112
  with gr.Column(scale=1, variant="panel"):
113
-
114
  gr.Markdown("### Sources")
115
  sources_textbox = gr.Textbox(
116
  interactive=False, show_label=False, max_lines=50
117
  )
 
118
  ask.submit(
119
- fn=gen_conv, inputs=[ask, state], outputs=[chatbot, state, sources_textbox]
 
 
120
  )
121
  with gr.Accordion("Add your personal openai api key", open=False):
122
  openai_api_key_textbox = gr.Textbox(
 
5
  import numpy as np
6
  import openai
7
  import os
8
+ from datasets import load_dataset
9
+ from datasets import Dataset
10
+ import time
11
+ from utils import is_climate_change_related, make_pairs, set_openai_api_key
12
 
13
  document_store = FAISSDocumentStore.load(
14
+ index_path="./documents/climate_gpt.faiss",
15
+ config_path="./documents/climate_gpt.json",
16
  )
17
 
18
  classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
 
25
  )
26
 
27
 
28
+ def gen_conv(query: str, history=[system_template], ipcc=True):
29
+ """return (answer:str, history:list[dict], sources:str)
 
 
 
 
 
30
 
31
+ Args:
32
+ query (str): _description_
33
+ history (list, optional): _description_. Defaults to [system_template].
34
+ ipcc (bool, optional): _description_. Defaults to True.
35
 
36
+ Returns:
37
+ _type_: _description_
38
+ """
39
+ retrieve = ipcc and is_climate_change_related(query, classifier)
40
 
 
 
 
41
  sources = ""
42
  messages = history + [
43
  {"role": "user", "content": query},
 
68
  f"{d.meta['file_name']} Page {d.meta['page_number']}:\n{d.content}"
69
  for d in docs
70
  )
71
+ else:
72
+ sources = "No environmental report was used to provide this answer."
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
+ css_code = ".gradio-container {background-image: url('file=background.png')}"
 
 
 
 
 
 
 
82
 
83
+ with gr.Blocks(title="🌍 ClimateGPT Ekimetrics", css=css_code) as demo:
84
 
 
 
85
  openai.api_key = os.environ["api_key"]
86
+ # gr.Markdown("# Climate GPT")
87
+ gr.Markdown("### Welcome to Climate GPT 🌍 ! ")
88
+ gr.Markdown(
89
+ """
90
+ Climate GPT is an interactive exploration tool designed to help you easily find relevant information based on of Environmental reports such as IPCCs and ??.
91
+
92
+ IPCC is a United Nations body that assesses the science related to climate change, including its impacts and possible response options. The IPCC is considered the leading scientific authority on all things related to global climate change.
93
+ """
94
+ )
95
+ gr.Markdown(
96
+ "**How does it work:** This Chatbot is a combination of two technologies. FAISS search applied to a vast amount of scientific climate reports and TurboGPT to generate human-like text from the part of the document extracted from the database."
97
+ )
98
+ gr.Markdown(
99
+ "⚠️ Warning: Always refer to the source (on the right side) to ensure the validity of the information communicated"
100
+ )
101
+ # gr.Markdown("""### Ask me anything, I'm a climate expert""")
102
  with gr.Row():
103
  with gr.Column(scale=2):
104
+ chatbot = gr.Chatbot(css=".gradio-container {background-color: blue}")
105
  state = gr.State([system_template])
106
 
107
  with gr.Row():
108
  ask = gr.Textbox(
109
+ show_label=False,
110
+ placeholder="Enter text and press enter",
111
+ sample_inputs=["which country polutes the most ?"],
112
  ).style(container=False)
113
+ print(f"Type from ask textbox {ask.type}")
114
 
115
  with gr.Column(scale=1, variant="panel"):
 
116
  gr.Markdown("### Sources")
117
  sources_textbox = gr.Textbox(
118
  interactive=False, show_label=False, max_lines=50
119
  )
120
+
121
  ask.submit(
122
+ fn=gen_conv,
123
+ inputs=[ask, state],
124
+ outputs=[chatbot, state, sources_textbox],
125
  )
126
  with gr.Accordion("Add your personal openai api key", open=False):
127
  openai_api_key_textbox = gr.Textbox(
utils.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import openai
3
+ import os
4
+
5
+
6
+ def is_climate_change_related(sentence: str, classifier) -> bool:
7
+ """_summary_
8
+
9
+ Args:
10
+ sentence (str): _description_
11
+ classifier (_type_): _description_
12
+
13
+ Returns:
14
+ bool: _description_
15
+ """
16
+ results = classifier(
17
+ sequences=sentence,
18
+ candidate_labels=["climate change related", "non climate change related"],
19
+ )
20
+ print(f" ## Result from is climate change related {results}")
21
+ return results["labels"][np.argmax(results["scores"])] == "climate change related"
22
+
23
+
24
+ def make_pairs(lst):
25
+ """From a list of even lenght, make tupple pairs
26
+ Args:
27
+ lst (list): _description_
28
+ Returns:
29
+ list: _description_
30
+ """
31
+ return [(lst[i], lst[i + 1]) for i in range(0, len(lst), 2)]
32
+
33
+
34
+ def set_openai_api_key(text):
35
+ """Set the api key and return chain.If no api_key, then None is returned.
36
+ To do : add raise error & Warning message
37
+ Args:
38
+ text (str): openai api key
39
+ Returns:
40
+ str: Result of connection
41
+ """
42
+ openai.api_key = os.environ["api_key"]
43
+
44
+ if text.startswith("sk-") and len(text) > 10:
45
+ openai.api_key = text
46
+ return f"You're all set: this is your api key: {openai.api_key}"