Nina
commited on
Commit
Β·
3afc54c
1
Parent(s):
5fbb7f0
add feedbacks and logs to azure
Browse files
app.py
CHANGED
|
@@ -6,8 +6,12 @@ import os
|
|
| 6 |
from utils import (
|
| 7 |
make_pairs,
|
| 8 |
set_openai_api_key,
|
| 9 |
-
|
| 10 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
system_template = {"role": "system", "content": os.environ["content"]}
|
| 13 |
|
|
@@ -19,6 +23,7 @@ retrieve_all = EmbeddingRetriever(
|
|
| 19 |
embedding_model="sentence-transformers/multi-qa-mpnet-base-dot-v1",
|
| 20 |
model_format="sentence_transformers",
|
| 21 |
)
|
|
|
|
| 22 |
retrieve_giec = EmbeddingRetriever(
|
| 23 |
document_store=FAISSDocumentStore.load(
|
| 24 |
index_path="./documents/climate_gpt_only_giec.faiss",
|
|
@@ -28,9 +33,23 @@ retrieve_giec = EmbeddingRetriever(
|
|
| 28 |
model_format="sentence_transformers",
|
| 29 |
)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def chat(
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
) -> tuple:
|
| 35 |
"""retrieve relevant documents in the document store then query gpt-turbo
|
| 36 |
|
|
@@ -61,7 +80,9 @@ def chat(
|
|
| 61 |
)
|
| 62 |
|
| 63 |
if sources:
|
| 64 |
-
messages.append(
|
|
|
|
|
|
|
| 65 |
|
| 66 |
response = openai.ChatCompletion.create(
|
| 67 |
model="gpt-3.5-turbo",
|
|
@@ -75,11 +96,22 @@ def chat(
|
|
| 75 |
complete_response = ""
|
| 76 |
else:
|
| 77 |
sources = "No environmental report was used to provide this answer."
|
| 78 |
-
complete_response =
|
| 79 |
-
"No relevant documents found, for a sourced answer you may want to try a more specific question.\n\n"
|
| 80 |
-
)
|
| 81 |
|
| 82 |
messages.append({"role": "assistant", "content": complete_response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
for chunk in response:
|
| 84 |
if chunk_message := chunk["choices"][0]["delta"].get("content", None):
|
| 85 |
complete_response += chunk_message
|
|
@@ -88,21 +120,36 @@ def chat(
|
|
| 88 |
yield gradio_format, messages, sources
|
| 89 |
|
| 90 |
|
| 91 |
-
def
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
def reset_textbox():
|
| 96 |
return gr.update(value="")
|
| 97 |
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
# Gradio
|
| 100 |
css_code = ".gradio-container {background-image: url('file=background.png');background-position: top right}"
|
| 101 |
|
| 102 |
with gr.Blocks(title="π ClimateGPT Ekimetrics", css=css_code) as demo:
|
| 103 |
|
| 104 |
openai.api_key = os.environ["api_key"]
|
| 105 |
-
user_id =
|
|
|
|
| 106 |
|
| 107 |
with gr.Tab("App"):
|
| 108 |
gr.Markdown("# Welcome to Climate GPT π !")
|
|
@@ -124,15 +171,16 @@ with gr.Blocks(title="π ClimateGPT Ekimetrics", css=css_code) as demo:
|
|
| 124 |
placeholder="Enter text and press enter",
|
| 125 |
sample_inputs=["which country polutes the most ?"],
|
| 126 |
).style(container=False)
|
| 127 |
-
print(f"Type from ask textbox {ask.type}")
|
| 128 |
|
| 129 |
with gr.Column(scale=1, variant="panel"):
|
| 130 |
gr.Markdown("### Sources")
|
| 131 |
-
sources_textbox = gr.Textbox(
|
| 132 |
-
|
|
|
|
| 133 |
ask.submit(
|
| 134 |
fn=chat,
|
| 135 |
inputs=[
|
|
|
|
| 136 |
ask,
|
| 137 |
state,
|
| 138 |
gr.inputs.Dropdown(
|
|
@@ -149,7 +197,11 @@ with gr.Blocks(title="π ClimateGPT Ekimetrics", css=css_code) as demo:
|
|
| 149 |
gr.Markdown("Please complete some feedbacks π")
|
| 150 |
feedback = gr.Textbox()
|
| 151 |
feedback_save = gr.Button(value="submit feedback")
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
with gr.Accordion("Add your personal openai api key - Option", open=False):
|
| 155 |
openai_api_key_textbox = gr.Textbox(
|
|
@@ -158,8 +210,12 @@ with gr.Blocks(title="π ClimateGPT Ekimetrics", css=css_code) as demo:
|
|
| 158 |
lines=1,
|
| 159 |
type="password",
|
| 160 |
)
|
| 161 |
-
openai_api_key_textbox.change(
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
with gr.Tab("Information"):
|
| 165 |
gr.Markdown(
|
|
|
|
| 6 |
from utils import (
|
| 7 |
make_pairs,
|
| 8 |
set_openai_api_key,
|
| 9 |
+
create_user_id,
|
| 10 |
)
|
| 11 |
+
import numpy as np
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
from azure.storage.fileshare import ShareServiceClient
|
| 14 |
+
|
| 15 |
|
| 16 |
system_template = {"role": "system", "content": os.environ["content"]}
|
| 17 |
|
|
|
|
| 23 |
embedding_model="sentence-transformers/multi-qa-mpnet-base-dot-v1",
|
| 24 |
model_format="sentence_transformers",
|
| 25 |
)
|
| 26 |
+
|
| 27 |
retrieve_giec = EmbeddingRetriever(
|
| 28 |
document_store=FAISSDocumentStore.load(
|
| 29 |
index_path="./documents/climate_gpt_only_giec.faiss",
|
|
|
|
| 33 |
model_format="sentence_transformers",
|
| 34 |
)
|
| 35 |
|
| 36 |
+
credential = {
|
| 37 |
+
"account_key": os.environ["account_key"],
|
| 38 |
+
"account_name": os.environ["account_name"],
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
account_url = os.environ["account_url"]
|
| 42 |
+
file_share_name = "climategpt"
|
| 43 |
+
service = ShareServiceClient(account_url=account_url, credential=credential)
|
| 44 |
+
share_client = service.get_share_client(file_share_name)
|
| 45 |
+
|
| 46 |
|
| 47 |
def chat(
|
| 48 |
+
user_id: str,
|
| 49 |
+
query: str,
|
| 50 |
+
history: list = [system_template],
|
| 51 |
+
report_type: str = "All available",
|
| 52 |
+
threshold: float = 0.559,
|
| 53 |
) -> tuple:
|
| 54 |
"""retrieve relevant documents in the document store then query gpt-turbo
|
| 55 |
|
|
|
|
| 80 |
)
|
| 81 |
|
| 82 |
if sources:
|
| 83 |
+
messages.append(
|
| 84 |
+
{"role": "system", "content": f"{os.environ['sources']}\n\n{sources}"}
|
| 85 |
+
)
|
| 86 |
|
| 87 |
response = openai.ChatCompletion.create(
|
| 88 |
model="gpt-3.5-turbo",
|
|
|
|
| 96 |
complete_response = ""
|
| 97 |
else:
|
| 98 |
sources = "No environmental report was used to provide this answer."
|
| 99 |
+
complete_response = "No relevant documents found, for a sourced answer you may want to try a more specific question.\n\n"
|
|
|
|
|
|
|
| 100 |
|
| 101 |
messages.append({"role": "assistant", "content": complete_response})
|
| 102 |
+
timestamp = str(datetime.now().timestamp())
|
| 103 |
+
file = user_id[0] + timestamp + ".json"
|
| 104 |
+
logs = {
|
| 105 |
+
"user_id": user_id[0],
|
| 106 |
+
"prompt": query,
|
| 107 |
+
"retrived": sources,
|
| 108 |
+
"report_type": report_type,
|
| 109 |
+
"prompt_eng": messages[0],
|
| 110 |
+
"answer": messages[-1]["content"],
|
| 111 |
+
"time": timestamp,
|
| 112 |
+
}
|
| 113 |
+
log_on_azure(file, logs, share_client)
|
| 114 |
+
|
| 115 |
for chunk in response:
|
| 116 |
if chunk_message := chunk["choices"][0]["delta"].get("content", None):
|
| 117 |
complete_response += chunk_message
|
|
|
|
| 120 |
yield gradio_format, messages, sources
|
| 121 |
|
| 122 |
|
| 123 |
+
def save_feedback(feed: str, user_id):
|
| 124 |
+
if len(feed) > 1:
|
| 125 |
+
timestamp = str(datetime.now().timestamp())
|
| 126 |
+
file = user_id[0] + timestamp + ".json"
|
| 127 |
+
logs = {
|
| 128 |
+
"user_id": user_id[0],
|
| 129 |
+
"feedback": feed,
|
| 130 |
+
"time": timestamp,
|
| 131 |
+
}
|
| 132 |
+
log_on_azure(file, logs, share_client)
|
| 133 |
+
return "Thanks for your feedbacks"
|
| 134 |
|
| 135 |
|
| 136 |
def reset_textbox():
|
| 137 |
return gr.update(value="")
|
| 138 |
|
| 139 |
|
| 140 |
+
def log_on_azure(file, logs, share_client):
|
| 141 |
+
file_client = share_client.get_file_client(file)
|
| 142 |
+
file_client.upload_file(str(logs))
|
| 143 |
+
|
| 144 |
+
|
| 145 |
# Gradio
|
| 146 |
css_code = ".gradio-container {background-image: url('file=background.png');background-position: top right}"
|
| 147 |
|
| 148 |
with gr.Blocks(title="π ClimateGPT Ekimetrics", css=css_code) as demo:
|
| 149 |
|
| 150 |
openai.api_key = os.environ["api_key"]
|
| 151 |
+
user_id = create_user_id(10)
|
| 152 |
+
user_id_state = gr.State([user_id])
|
| 153 |
|
| 154 |
with gr.Tab("App"):
|
| 155 |
gr.Markdown("# Welcome to Climate GPT π !")
|
|
|
|
| 171 |
placeholder="Enter text and press enter",
|
| 172 |
sample_inputs=["which country polutes the most ?"],
|
| 173 |
).style(container=False)
|
|
|
|
| 174 |
|
| 175 |
with gr.Column(scale=1, variant="panel"):
|
| 176 |
gr.Markdown("### Sources")
|
| 177 |
+
sources_textbox = gr.Textbox(
|
| 178 |
+
interactive=False, show_label=False, max_lines=50
|
| 179 |
+
)
|
| 180 |
ask.submit(
|
| 181 |
fn=chat,
|
| 182 |
inputs=[
|
| 183 |
+
user_id_state,
|
| 184 |
ask,
|
| 185 |
state,
|
| 186 |
gr.inputs.Dropdown(
|
|
|
|
| 197 |
gr.Markdown("Please complete some feedbacks π")
|
| 198 |
feedback = gr.Textbox()
|
| 199 |
feedback_save = gr.Button(value="submit feedback")
|
| 200 |
+
# thanks = gr.Textbox()
|
| 201 |
+
feedback_save.click(
|
| 202 |
+
save_feedback,
|
| 203 |
+
inputs=[feedback, user_id_state], # outputs=[thanks]
|
| 204 |
+
)
|
| 205 |
|
| 206 |
with gr.Accordion("Add your personal openai api key - Option", open=False):
|
| 207 |
openai_api_key_textbox = gr.Textbox(
|
|
|
|
| 210 |
lines=1,
|
| 211 |
type="password",
|
| 212 |
)
|
| 213 |
+
openai_api_key_textbox.change(
|
| 214 |
+
set_openai_api_key, inputs=[openai_api_key_textbox]
|
| 215 |
+
)
|
| 216 |
+
openai_api_key_textbox.submit(
|
| 217 |
+
set_openai_api_key, inputs=[openai_api_key_textbox]
|
| 218 |
+
)
|
| 219 |
|
| 220 |
with gr.Tab("Information"):
|
| 221 |
gr.Markdown(
|