Spaces:
Running
Running
refactor
Browse files- app.py +13 -20
- auditqa/reader.py +5 -6
- auditqa/retriever.py +1 -2
- auditqa/utils.py +0 -1
- logs/app.log +1 -0
- model_params.cfg +3 -1
app.py
CHANGED
@@ -15,9 +15,6 @@ from auditqa.reader import nvidia_client, dedicated_endpoint
|
|
15 |
from auditqa.utils import make_html_source, parse_output_llm_with_sources, save_logs, get_message_template, get_client_location, get_client_ip
|
16 |
from dotenv import load_dotenv
|
17 |
from threading import Lock
|
18 |
-
# import json
|
19 |
-
# from functools import partial
|
20 |
-
# import time
|
21 |
from gradio.routes import Request
|
22 |
from qdrant_client import QdrantClient
|
23 |
|
@@ -26,7 +23,7 @@ from auditqa.logging_config import setup_logging
|
|
26 |
setup_logging()
|
27 |
import logging
|
28 |
logger = logging.getLogger(__name__)
|
29 |
-
logger.setLevel(logging.DEBUG)
|
30 |
|
31 |
load_dotenv()
|
32 |
|
@@ -44,12 +41,12 @@ JSON_DATASET_PATH = JSON_DATASET_DIR / f"logs-{uuid4()}.json"
|
|
44 |
# the logs are written to dataset repo periodically from local logs
|
45 |
# https://huggingface.co/spaces/Wauplin/space_to_dataset_saver
|
46 |
scheduler = CommitScheduler(
|
47 |
-
repo_id=
|
48 |
repo_type="dataset",
|
49 |
folder_path=JSON_DATASET_DIR,
|
50 |
-
path_in_repo="
|
51 |
token=SPACES_LOG,
|
52 |
-
every=2)
|
53 |
|
54 |
#####--------------- VECTOR STORE -------------------------------------------------
|
55 |
# Configure cloud Qdrant client
|
@@ -511,7 +508,7 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
511 |
|
512 |
|
513 |
|
514 |
-
#--------------------
|
515 |
with gr.Row(visible=False) as feedback_row:
|
516 |
gr.Markdown("Was this response helpful?")
|
517 |
with gr.Row():
|
@@ -520,7 +517,6 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
520 |
|
521 |
feedback_thanks = gr.Markdown("Thanks for the feedback!", visible=False)
|
522 |
feedback_state = gr.State() # Add state to store logs data
|
523 |
-
debug_ip = gr.Markdown(visible=False) # Add debug display for IP
|
524 |
|
525 |
def show_feedback(logs):
|
526 |
"""Show feedback buttons and store logs in state"""
|
@@ -528,29 +524,25 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
528 |
|
529 |
def submit_feedback_okay(logs_data):
|
530 |
"""Handle 'okay' feedback submission"""
|
531 |
-
|
532 |
-
location = logs_data.get('client_location', 'No location found')
|
533 |
-
return submit_feedback("okay", logs_data) + (gr.update(visible=True, value=f"TESTING - Client IP: {ip}, Location: {location}"),)
|
534 |
|
535 |
def submit_feedback_not_okay(logs_data):
|
536 |
"""Handle 'not okay' feedback submission"""
|
537 |
-
|
538 |
-
location = logs_data.get('client_location', 'No location found')
|
539 |
-
return submit_feedback("not_okay", logs_data) + (gr.update(visible=True, value=f"TESTING - Client IP: {ip}, Location: {location}"),)
|
540 |
|
541 |
okay_btn.click(
|
542 |
submit_feedback_okay,
|
543 |
[feedback_state],
|
544 |
-
[feedback_row, feedback_thanks
|
545 |
)
|
546 |
|
547 |
not_okay_btn.click(
|
548 |
submit_feedback_not_okay,
|
549 |
[feedback_state],
|
550 |
-
[feedback_row, feedback_thanks
|
551 |
)
|
552 |
|
553 |
-
|
554 |
|
555 |
# Add these state components at the top level of the Blocks
|
556 |
session_id = gr.State(None)
|
@@ -561,6 +553,8 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
561 |
"""Handler for getting client IP in Gradio context"""
|
562 |
return get_client_ip(request)
|
563 |
|
|
|
|
|
564 |
# Update the event handlers
|
565 |
(textbox
|
566 |
.submit(get_client_ip_handler, [textbox], [client_ip], api_name="get_ip_textbox")
|
@@ -584,5 +578,4 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
584 |
|
585 |
demo.queue()
|
586 |
|
587 |
-
demo.launch()
|
588 |
-
logger.info("App launched")
|
|
|
15 |
from auditqa.utils import make_html_source, parse_output_llm_with_sources, save_logs, get_message_template, get_client_location, get_client_ip
|
16 |
from dotenv import load_dotenv
|
17 |
from threading import Lock
|
|
|
|
|
|
|
18 |
from gradio.routes import Request
|
19 |
from qdrant_client import QdrantClient
|
20 |
|
|
|
23 |
setup_logging()
|
24 |
import logging
|
25 |
logger = logging.getLogger(__name__)
|
26 |
+
logger.setLevel(logging.DEBUG)
|
27 |
|
28 |
load_dotenv()
|
29 |
|
|
|
41 |
# the logs are written to dataset repo periodically from local logs
|
42 |
# https://huggingface.co/spaces/Wauplin/space_to_dataset_saver
|
43 |
scheduler = CommitScheduler(
|
44 |
+
repo_id=model_config.get('app','repo_id'),
|
45 |
repo_type="dataset",
|
46 |
folder_path=JSON_DATASET_DIR,
|
47 |
+
path_in_repo="audit_chatbot",
|
48 |
token=SPACES_LOG,
|
49 |
+
every=2) # TESTING: every 2 seconds
|
50 |
|
51 |
#####--------------- VECTOR STORE -------------------------------------------------
|
52 |
# Configure cloud Qdrant client
|
|
|
508 |
|
509 |
|
510 |
|
511 |
+
#-------------------- New UI elements for Feedback -------------------------
|
512 |
with gr.Row(visible=False) as feedback_row:
|
513 |
gr.Markdown("Was this response helpful?")
|
514 |
with gr.Row():
|
|
|
517 |
|
518 |
feedback_thanks = gr.Markdown("Thanks for the feedback!", visible=False)
|
519 |
feedback_state = gr.State() # Add state to store logs data
|
|
|
520 |
|
521 |
def show_feedback(logs):
|
522 |
"""Show feedback buttons and store logs in state"""
|
|
|
524 |
|
525 |
def submit_feedback_okay(logs_data):
|
526 |
"""Handle 'okay' feedback submission"""
|
527 |
+
return submit_feedback("okay", logs_data)
|
|
|
|
|
528 |
|
529 |
def submit_feedback_not_okay(logs_data):
|
530 |
"""Handle 'not okay' feedback submission"""
|
531 |
+
return submit_feedback("not_okay", logs_data)
|
|
|
|
|
532 |
|
533 |
okay_btn.click(
|
534 |
submit_feedback_okay,
|
535 |
[feedback_state],
|
536 |
+
[feedback_row, feedback_thanks]
|
537 |
)
|
538 |
|
539 |
not_okay_btn.click(
|
540 |
submit_feedback_not_okay,
|
541 |
[feedback_state],
|
542 |
+
[feedback_row, feedback_thanks]
|
543 |
)
|
544 |
|
545 |
+
#-------------------- Session Management + Geolocation -------------------------
|
546 |
|
547 |
# Add these state components at the top level of the Blocks
|
548 |
session_id = gr.State(None)
|
|
|
553 |
"""Handler for getting client IP in Gradio context"""
|
554 |
return get_client_ip(request)
|
555 |
|
556 |
+
#-------------------- Gradio voodoo -------------------------
|
557 |
+
|
558 |
# Update the event handlers
|
559 |
(textbox
|
560 |
.submit(get_client_ip_handler, [textbox], [client_ip], api_name="get_ip_textbox")
|
|
|
578 |
|
579 |
demo.queue()
|
580 |
|
581 |
+
demo.launch()
|
|
auditqa/reader.py
CHANGED
@@ -14,9 +14,8 @@ import logging
|
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
16 |
model_config = getconfig("model_params.cfg")
|
17 |
-
|
18 |
-
|
19 |
-
# HF_token = os.getenv('LLAMA_3_1') # TESTING
|
20 |
|
21 |
def nvidia_client():
|
22 |
logger.info("NVIDIA client activated")
|
@@ -35,8 +34,8 @@ def nvidia_client():
|
|
35 |
def dedicated_endpoint():
|
36 |
logger.info("Serverless endpoint activated")
|
37 |
try:
|
38 |
-
|
39 |
-
if not
|
40 |
raise ValueError("LLAMA_3_1 environment variable is empty")
|
41 |
|
42 |
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
@@ -44,7 +43,7 @@ def dedicated_endpoint():
|
|
44 |
|
45 |
client = InferenceClient(
|
46 |
model=model_id,
|
47 |
-
api_key=
|
48 |
)
|
49 |
|
50 |
logger.info("Serverless InferenceClient initialization successful")
|
|
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
16 |
model_config = getconfig("model_params.cfg")
|
17 |
+
NVIDIA_SERVER = os.environ["NVIDIA_SERVERLESS"]
|
18 |
+
HF_token = os.environ["LLAMA_3_1"]
|
|
|
19 |
|
20 |
def nvidia_client():
|
21 |
logger.info("NVIDIA client activated")
|
|
|
34 |
def dedicated_endpoint():
|
35 |
logger.info("Serverless endpoint activated")
|
36 |
try:
|
37 |
+
HF_token = os.environ["LLAMA_3_1"]
|
38 |
+
if not HF_token:
|
39 |
raise ValueError("LLAMA_3_1 environment variable is empty")
|
40 |
|
41 |
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
|
|
43 |
|
44 |
client = InferenceClient(
|
45 |
model=model_id,
|
46 |
+
api_key=HF_token,
|
47 |
)
|
48 |
|
49 |
logger.info("Serverless InferenceClient initialization successful")
|
auditqa/retriever.py
CHANGED
@@ -41,12 +41,11 @@ def create_filter(reports:list = [],sources:str =None,
|
|
41 |
return filter
|
42 |
|
43 |
|
44 |
-
|
45 |
def get_context(vectorstore,query,reports,sources,subtype,year):
|
46 |
logger.info("Retriever activated")
|
47 |
# create metadata filter
|
48 |
# filter = create_filter(reports=reports,sources=sources,subtype=subtype,year=year)
|
49 |
-
filter = None
|
50 |
|
51 |
# getting context
|
52 |
retriever = vectorstore.as_retriever(search_type="similarity_score_threshold",
|
|
|
41 |
return filter
|
42 |
|
43 |
|
|
|
44 |
def get_context(vectorstore,query,reports,sources,subtype,year):
|
45 |
logger.info("Retriever activated")
|
46 |
# create metadata filter
|
47 |
# filter = create_filter(reports=reports,sources=sources,subtype=subtype,year=year)
|
48 |
+
filter = None #TESTING
|
49 |
|
50 |
# getting context
|
51 |
retriever = vectorstore.as_retriever(search_type="similarity_score_threshold",
|
auditqa/utils.py
CHANGED
@@ -36,7 +36,6 @@ def save_logs(scheduler, JSON_DATASET_PATH, logs, feedback=None) -> None:
|
|
36 |
raise
|
37 |
|
38 |
|
39 |
-
|
40 |
def get_message_template(type, SYSTEM_PROMPT, USER_PROMPT):
|
41 |
if type == 'NVIDIA':
|
42 |
messages = [{"role": "system", "content": SYSTEM_PROMPT},
|
|
|
36 |
raise
|
37 |
|
38 |
|
|
|
39 |
def get_message_template(type, SYSTEM_PROMPT, USER_PROMPT):
|
40 |
if type == 'NVIDIA':
|
41 |
messages = [{"role": "system", "content": SYSTEM_PROMPT},
|
logs/app.log
CHANGED
@@ -1367,3 +1367,4 @@ Make sure your token has the correct permissions.
|
|
1367 |
2025-02-13 13:39:44,129 - auditqa.reader - INFO - Serverless InferenceClient initialization successful
|
1368 |
2025-02-13 13:40:00,385 - auditqa.utils - INFO - logging done
|
1369 |
2025-02-13 13:40:05,605 - auditqa.utils - INFO - logging done
|
|
|
|
1367 |
2025-02-13 13:39:44,129 - auditqa.reader - INFO - Serverless InferenceClient initialization successful
|
1368 |
2025-02-13 13:40:00,385 - auditqa.utils - INFO - logging done
|
1369 |
2025-02-13 13:40:05,605 - auditqa.utils - INFO - logging done
|
1370 |
+
2025-02-13 13:45:56,822 - __main__ - INFO - App launched
|
model_params.cfg
CHANGED
@@ -11,4 +11,6 @@ DEDICATED_MODEL = meta-llama/Llama-3.1-8B-Instruct
|
|
11 |
DEDICATED_ENDPOINT = https://qu2d8m6dmsollhly.us-east-1.aws.endpoints.huggingface.cloud
|
12 |
NVIDIA_MODEL = meta-llama/Llama-3.1-8B-Instruct
|
13 |
NVIDIA_ENDPOINT = https://huggingface.co/api/integrations/dgx/v1
|
14 |
-
MAX_TOKENS = 512
|
|
|
|
|
|
11 |
DEDICATED_ENDPOINT = https://qu2d8m6dmsollhly.us-east-1.aws.endpoints.huggingface.cloud
|
12 |
NVIDIA_MODEL = meta-llama/Llama-3.1-8B-Instruct
|
13 |
NVIDIA_ENDPOINT = https://huggingface.co/api/integrations/dgx/v1
|
14 |
+
MAX_TOKENS = 512
|
15 |
+
[app]
|
16 |
+
repo_id = mtyrrell/audit_chatbot
|