Added logging
Browse files
app.py
CHANGED
|
@@ -4,12 +4,7 @@ import numpy as np
|
|
| 4 |
import os
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
-
from utils import
|
| 8 |
-
make_pairs,
|
| 9 |
-
set_openai_api_key,
|
| 10 |
-
create_user_id,
|
| 11 |
-
to_completion,
|
| 12 |
-
)
|
| 13 |
|
| 14 |
from azure.storage.fileshare import ShareServiceClient
|
| 15 |
|
|
@@ -30,7 +25,7 @@ from climateqa.prompts import audience_prompts
|
|
| 30 |
try:
|
| 31 |
from dotenv import load_dotenv
|
| 32 |
load_dotenv()
|
| 33 |
-
except:
|
| 34 |
pass
|
| 35 |
|
| 36 |
# Set up Gradio Theme
|
|
@@ -40,6 +35,8 @@ theme = gr.themes.Base(
|
|
| 40 |
font=[gr.themes.GoogleFont("Poppins"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 41 |
)
|
| 42 |
|
|
|
|
|
|
|
| 43 |
init_prompt = ""
|
| 44 |
|
| 45 |
system_template = {
|
|
@@ -47,19 +44,21 @@ system_template = {
|
|
| 47 |
"content": init_prompt,
|
| 48 |
}
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
# account_url = os.environ["account_url"]
|
| 57 |
-
# file_share_name = "climategpt"
|
| 58 |
-
# service = ShareServiceClient(account_url=account_url, credential=credential)
|
| 59 |
-
# share_client = service.get_share_client(file_share_name)
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 63 |
|
| 64 |
#---------------------------------------------------------------------------
|
| 65 |
# ClimateQ&A core functions
|
|
@@ -242,6 +241,24 @@ def answer_bot(query,history,docs,question,language,audience):
|
|
| 242 |
else:
|
| 243 |
pass
|
| 244 |
thread.join()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
else:
|
| 246 |
complete_response = "**⚠️ No relevant passages found in the climate science reports (IPCC and IPBES), you may want to ask a more specific question (specifying your question on climate and biodiversity issues).**"
|
| 247 |
history[-1][1] += complete_response
|
|
@@ -394,9 +411,9 @@ def make_html_source(source,i):
|
|
| 394 |
|
| 395 |
# messages.append({"role": "assistant", "content": complete_response})
|
| 396 |
# timestamp = str(datetime.now().timestamp())
|
| 397 |
-
# file = user_id
|
| 398 |
# logs = {
|
| 399 |
-
# "user_id": user_id
|
| 400 |
# "prompt": query,
|
| 401 |
# "retrived": sources,
|
| 402 |
# "report_type": report_type,
|
|
@@ -424,9 +441,9 @@ def make_html_source(source,i):
|
|
| 424 |
def save_feedback(feed: str, user_id):
|
| 425 |
if len(feed) > 1:
|
| 426 |
timestamp = str(datetime.now().timestamp())
|
| 427 |
-
file = user_id
|
| 428 |
logs = {
|
| 429 |
-
"user_id": user_id
|
| 430 |
"feedback": feed,
|
| 431 |
"time": timestamp,
|
| 432 |
}
|
|
@@ -437,10 +454,18 @@ def save_feedback(feed: str, user_id):
|
|
| 437 |
def reset_textbox():
|
| 438 |
return gr.update(value="")
|
| 439 |
|
|
|
|
| 440 |
|
| 441 |
def log_on_azure(file, logs, share_client):
|
|
|
|
|
|
|
| 442 |
file_client = share_client.get_file_client(file)
|
| 443 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
|
| 445 |
|
| 446 |
# def disable_component():
|
|
|
|
| 4 |
import os
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
+
from utils import create_user_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
from azure.storage.fileshare import ShareServiceClient
|
| 10 |
|
|
|
|
| 25 |
try:
|
| 26 |
from dotenv import load_dotenv
|
| 27 |
load_dotenv()
|
| 28 |
+
except Exception as e:
|
| 29 |
pass
|
| 30 |
|
| 31 |
# Set up Gradio Theme
|
|
|
|
| 35 |
font=[gr.themes.GoogleFont("Poppins"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 36 |
)
|
| 37 |
|
| 38 |
+
|
| 39 |
+
|
| 40 |
init_prompt = ""
|
| 41 |
|
| 42 |
system_template = {
|
|
|
|
| 44 |
"content": init_prompt,
|
| 45 |
}
|
| 46 |
|
| 47 |
+
account_key = os.environ["BLOB_ACCOUNT_KEY"]
|
| 48 |
+
if len(account_key) == 86:
|
| 49 |
+
account_key += "=="
|
| 50 |
|
| 51 |
+
credential = {
|
| 52 |
+
"account_key": account_key,
|
| 53 |
+
"account_name": os.environ["BLOB_ACCOUNT_NAME"],
|
| 54 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
account_url = os.environ["BLOB_ACCOUNT_URL"]
|
| 57 |
+
file_share_name = "climategpt"
|
| 58 |
+
service = ShareServiceClient(account_url=account_url, credential=credential)
|
| 59 |
+
share_client = service.get_share_client(file_share_name)
|
| 60 |
|
| 61 |
+
user_id = create_user_id()
|
| 62 |
|
| 63 |
#---------------------------------------------------------------------------
|
| 64 |
# ClimateQ&A core functions
|
|
|
|
| 241 |
else:
|
| 242 |
pass
|
| 243 |
thread.join()
|
| 244 |
+
|
| 245 |
+
# Log answer on Azure Blob Storage
|
| 246 |
+
timestamp = str(datetime.now().timestamp())
|
| 247 |
+
file = timestamp + ".json"
|
| 248 |
+
prompt = history[-1][0]
|
| 249 |
+
logs = {
|
| 250 |
+
"user_id": str(user_id),
|
| 251 |
+
"prompt": prompt,
|
| 252 |
+
"query": prompt,
|
| 253 |
+
"question":question,
|
| 254 |
+
"docs":docs,
|
| 255 |
+
"answer": history[-1][1],
|
| 256 |
+
"time": timestamp,
|
| 257 |
+
}
|
| 258 |
+
log_on_azure(file, logs, share_client)
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
|
| 262 |
else:
|
| 263 |
complete_response = "**⚠️ No relevant passages found in the climate science reports (IPCC and IPBES), you may want to ask a more specific question (specifying your question on climate and biodiversity issues).**"
|
| 264 |
history[-1][1] += complete_response
|
|
|
|
| 411 |
|
| 412 |
# messages.append({"role": "assistant", "content": complete_response})
|
| 413 |
# timestamp = str(datetime.now().timestamp())
|
| 414 |
+
# file = user_id + timestamp + ".json"
|
| 415 |
# logs = {
|
| 416 |
+
# "user_id": user_id,
|
| 417 |
# "prompt": query,
|
| 418 |
# "retrived": sources,
|
| 419 |
# "report_type": report_type,
|
|
|
|
| 441 |
def save_feedback(feed: str, user_id):
|
| 442 |
if len(feed) > 1:
|
| 443 |
timestamp = str(datetime.now().timestamp())
|
| 444 |
+
file = user_id + timestamp + ".json"
|
| 445 |
logs = {
|
| 446 |
+
"user_id": user_id,
|
| 447 |
"feedback": feed,
|
| 448 |
"time": timestamp,
|
| 449 |
}
|
|
|
|
| 454 |
def reset_textbox():
|
| 455 |
return gr.update(value="")
|
| 456 |
|
| 457 |
+
import json
|
| 458 |
|
| 459 |
def log_on_azure(file, logs, share_client):
|
| 460 |
+
logs = json.dumps(logs)
|
| 461 |
+
print(type(logs))
|
| 462 |
file_client = share_client.get_file_client(file)
|
| 463 |
+
print("Uploading logs to Azure Blob Storage")
|
| 464 |
+
print("----------------------------------")
|
| 465 |
+
print("")
|
| 466 |
+
print(logs)
|
| 467 |
+
file_client.upload_file(logs)
|
| 468 |
+
print("Logs uploaded to Azure Blob Storage")
|
| 469 |
|
| 470 |
|
| 471 |
# def disable_component():
|
utils.py
CHANGED
|
@@ -1,69 +1,12 @@
|
|
| 1 |
import numpy as np
|
| 2 |
-
import openai
|
| 3 |
-
import os
|
| 4 |
import random
|
| 5 |
import string
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
"""_summary_
|
| 10 |
-
|
| 11 |
-
Args:
|
| 12 |
-
sentence (str): your sentence to classify
|
| 13 |
-
classifier (_type_): zero shot hugging face pipeline classifier
|
| 14 |
-
|
| 15 |
-
Returns:
|
| 16 |
-
bool: is_climate_change_related or not
|
| 17 |
-
"""
|
| 18 |
-
results = classifier(
|
| 19 |
-
sequences=sentence,
|
| 20 |
-
candidate_labels=["climate change related", "non climate change related"],
|
| 21 |
-
)
|
| 22 |
-
print(f" ## Result from is climate change related {results}")
|
| 23 |
-
return results["labels"][np.argmax(results["scores"])] == "climate change related"
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def make_pairs(lst):
|
| 27 |
-
"""From a list of even lenght, make tupple pairs
|
| 28 |
-
Args:
|
| 29 |
-
lst (list): a list of even lenght
|
| 30 |
-
Returns:
|
| 31 |
-
list: the list as tupple pairs
|
| 32 |
-
"""
|
| 33 |
-
assert not (l := len(lst) % 2), f"your list is of lenght {l} which is not even"
|
| 34 |
-
return [(lst[i], lst[i + 1]) for i in range(0, len(lst), 2)]
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def set_openai_api_key(text):
|
| 38 |
-
"""Set the api key and return chain.If no api_key, then None is returned.
|
| 39 |
-
To do : add raise error & Warning message
|
| 40 |
-
Args:
|
| 41 |
-
text (str): openai api key
|
| 42 |
-
Returns:
|
| 43 |
-
str: Result of connection
|
| 44 |
-
"""
|
| 45 |
-
openai.api_key = os.environ["api_key"]
|
| 46 |
-
|
| 47 |
-
if text.startswith("sk-") and len(text) > 10:
|
| 48 |
-
openai.api_key = text
|
| 49 |
-
return f"You're all set: this is your api key: {openai.api_key}"
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
def create_user_id(length):
|
| 53 |
"""Create user_id
|
| 54 |
-
Args:
|
| 55 |
-
length (int): length of user id
|
| 56 |
-
Returns:
|
| 57 |
str: String to id user
|
| 58 |
"""
|
| 59 |
-
|
| 60 |
-
user_id
|
| 61 |
-
return user_id
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
def to_completion(messages):
|
| 65 |
-
s = []
|
| 66 |
-
for message in messages:
|
| 67 |
-
s.append(f"<|im_start|>{message['role']}\n{message['content']}<|im_end|>")
|
| 68 |
-
s.append("<|im_start|>assistant\n")
|
| 69 |
-
return "\n".join(s)
|
|
|
|
| 1 |
import numpy as np
|
|
|
|
|
|
|
| 2 |
import random
|
| 3 |
import string
|
| 4 |
+
import uuid
|
| 5 |
|
| 6 |
|
| 7 |
+
def create_user_id():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"""Create user_id
|
|
|
|
|
|
|
|
|
|
| 9 |
str: String to id user
|
| 10 |
"""
|
| 11 |
+
user_id = str(uuid.uuid4())
|
| 12 |
+
return user_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|