Spaces:
Running
Running
Omar Solano
commited on
Commit
Β·
9d31d4d
1
Parent(s):
c254c97
refactor logging: switch to logfire, update date handling, and clean up commented code in app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,13 @@
|
|
1 |
import logging
|
2 |
import os
|
3 |
-
from typing import Optional
|
4 |
import time
|
5 |
from datetime import datetime
|
|
|
6 |
|
7 |
import gradio as gr
|
|
|
8 |
import pandas as pd
|
9 |
-
|
10 |
from buster.completers import Completion
|
11 |
-
|
12 |
from gradio.themes.utils import (
|
13 |
colors,
|
14 |
fonts,
|
@@ -30,7 +29,7 @@ AVAILABLE_SOURCES_UI = [
|
|
30 |
"Activeloop Docs",
|
31 |
"HF Transformers Docs",
|
32 |
"Wikipedia",
|
33 |
-
"OpenAI Docs",
|
34 |
"LangChain Docs",
|
35 |
]
|
36 |
|
@@ -42,7 +41,7 @@ AVAILABLE_SOURCES = [
|
|
42 |
"activeloop",
|
43 |
"hf_transformers",
|
44 |
"wikipedia",
|
45 |
-
"openai",
|
46 |
"langchain_docs",
|
47 |
]
|
48 |
|
@@ -64,15 +63,15 @@ def save_completion(completion: Completion, history):
|
|
64 |
)
|
65 |
|
66 |
# Add the current date and time to the JSON
|
67 |
-
completion_json["timestamp"] = datetime.
|
68 |
completion_json["history"] = history
|
69 |
completion_json["history_len"] = len(history)
|
70 |
|
71 |
try:
|
72 |
cfg.mongo_db[collection].insert_one(completion_json)
|
73 |
-
|
74 |
except Exception as e:
|
75 |
-
|
76 |
|
77 |
|
78 |
def log_likes(completion: Completion, like_data: gr.LikeData):
|
@@ -82,36 +81,41 @@ def log_likes(completion: Completion, like_data: gr.LikeData):
|
|
82 |
columns_to_ignore=["embedding", "similarity", "similarity_to_answer"]
|
83 |
)
|
84 |
completion_json["liked"] = like_data.liked
|
85 |
-
|
86 |
|
87 |
try:
|
88 |
cfg.mongo_db[collection].insert_one(completion_json)
|
89 |
-
|
90 |
except:
|
91 |
-
|
92 |
|
93 |
|
94 |
def log_emails(email: gr.Textbox):
|
95 |
collection = "email_data-test"
|
96 |
|
97 |
-
|
98 |
email_document = {"email": email}
|
99 |
|
100 |
try:
|
101 |
cfg.mongo_db[collection].insert_one(email_document)
|
102 |
-
|
103 |
except:
|
104 |
-
|
105 |
|
106 |
return ""
|
107 |
|
108 |
|
109 |
def format_sources(matched_documents: pd.DataFrame) -> str:
|
110 |
if len(matched_documents) == 0:
|
|
|
111 |
return ""
|
112 |
|
113 |
-
documents_answer_template: str =
|
114 |
-
|
|
|
|
|
|
|
|
|
115 |
|
116 |
matched_documents.similarity_to_answer = (
|
117 |
matched_documents.similarity_to_answer * 100
|
@@ -138,9 +142,8 @@ def format_sources(matched_documents: pd.DataFrame) -> str:
|
|
138 |
|
139 |
|
140 |
def add_sources(history, completion):
|
141 |
-
|
142 |
-
|
143 |
-
history.append([None, formatted_sources])
|
144 |
|
145 |
return history
|
146 |
|
@@ -184,8 +187,7 @@ with gr.Blocks(
|
|
184 |
theme=gr.themes.Soft(
|
185 |
primary_hue="blue",
|
186 |
secondary_hue="blue",
|
187 |
-
font=fonts.GoogleFont("Source Sans Pro"),
|
188 |
-
font_mono=fonts.GoogleFont("IBM Plex Mono"),
|
189 |
)
|
190 |
) as demo:
|
191 |
with gr.Row():
|
@@ -250,5 +252,5 @@ with gr.Blocks(
|
|
250 |
submit_email.click(log_emails, email, email)
|
251 |
email.submit(log_emails, email, email)
|
252 |
|
253 |
-
demo.queue(
|
254 |
demo.launch(debug=True, share=False)
|
|
|
1 |
import logging
|
2 |
import os
|
|
|
3 |
import time
|
4 |
from datetime import datetime
|
5 |
+
from typing import Optional
|
6 |
|
7 |
import gradio as gr
|
8 |
+
import logfire
|
9 |
import pandas as pd
|
|
|
10 |
from buster.completers import Completion
|
|
|
11 |
from gradio.themes.utils import (
|
12 |
colors,
|
13 |
fonts,
|
|
|
29 |
"Activeloop Docs",
|
30 |
"HF Transformers Docs",
|
31 |
"Wikipedia",
|
32 |
+
# "OpenAI Docs",
|
33 |
"LangChain Docs",
|
34 |
]
|
35 |
|
|
|
41 |
"activeloop",
|
42 |
"hf_transformers",
|
43 |
"wikipedia",
|
44 |
+
# "openai",
|
45 |
"langchain_docs",
|
46 |
]
|
47 |
|
|
|
63 |
)
|
64 |
|
65 |
# Add the current date and time to the JSON
|
66 |
+
completion_json["timestamp"] = datetime.now().isoformat()
|
67 |
completion_json["history"] = history
|
68 |
completion_json["history_len"] = len(history)
|
69 |
|
70 |
try:
|
71 |
cfg.mongo_db[collection].insert_one(completion_json)
|
72 |
+
logfire.info("Completion saved to db")
|
73 |
except Exception as e:
|
74 |
+
logfire.info(f"Something went wrong logging completion to db: {e}")
|
75 |
|
76 |
|
77 |
def log_likes(completion: Completion, like_data: gr.LikeData):
|
|
|
81 |
columns_to_ignore=["embedding", "similarity", "similarity_to_answer"]
|
82 |
)
|
83 |
completion_json["liked"] = like_data.liked
|
84 |
+
logfire.info(f"User reported {like_data.liked=}")
|
85 |
|
86 |
try:
|
87 |
cfg.mongo_db[collection].insert_one(completion_json)
|
88 |
+
logfire.info("")
|
89 |
except:
|
90 |
+
logfire.info("Something went wrong logging")
|
91 |
|
92 |
|
93 |
def log_emails(email: gr.Textbox):
|
94 |
collection = "email_data-test"
|
95 |
|
96 |
+
logfire.info(f"User reported {email=}")
|
97 |
email_document = {"email": email}
|
98 |
|
99 |
try:
|
100 |
cfg.mongo_db[collection].insert_one(email_document)
|
101 |
+
logfire.info("")
|
102 |
except:
|
103 |
+
logfire.info("Something went wrong logging")
|
104 |
|
105 |
return ""
|
106 |
|
107 |
|
108 |
def format_sources(matched_documents: pd.DataFrame) -> str:
|
109 |
if len(matched_documents) == 0:
|
110 |
+
logfire.info("No sources found")
|
111 |
return ""
|
112 |
|
113 |
+
documents_answer_template: str = (
|
114 |
+
"π Here are the sources I used to answer your question:\n\n{documents}\n\n{footnote}"
|
115 |
+
)
|
116 |
+
document_template: str = (
|
117 |
+
"[π {document.source}: {document.title}]({document.url}), relevance: {document.similarity_to_answer:2.1f} %" # | # total chunks matched: {document.repetition:d}"
|
118 |
+
)
|
119 |
|
120 |
matched_documents.similarity_to_answer = (
|
121 |
matched_documents.similarity_to_answer * 100
|
|
|
142 |
|
143 |
|
144 |
def add_sources(history, completion):
|
145 |
+
formatted_sources = format_sources(completion.matched_documents)
|
146 |
+
history.append([None, formatted_sources])
|
|
|
147 |
|
148 |
return history
|
149 |
|
|
|
187 |
theme=gr.themes.Soft(
|
188 |
primary_hue="blue",
|
189 |
secondary_hue="blue",
|
190 |
+
font=[fonts.GoogleFont("Source Sans Pro"), fonts.GoogleFont("IBM Plex Mono")],
|
|
|
191 |
)
|
192 |
) as demo:
|
193 |
with gr.Row():
|
|
|
252 |
submit_email.click(log_emails, email, email)
|
253 |
email.submit(log_emails, email, email)
|
254 |
|
255 |
+
demo.queue(default_concurrency_limit=CONCURRENCY_COUNT)
|
256 |
demo.launch(debug=True, share=False)
|