Spaces:
Build error
Build error
working version :)
Browse files- app.py +21 -9
- requirements.txt +2 -1
- utils.py +13 -3
app.py
CHANGED
|
@@ -11,7 +11,12 @@ Notes:
|
|
| 11 |
- Need to call Request from start state
|
| 12 |
- Example URL: localhost:7860/?user=123&session=456&questionid=0&response=0
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
| 15 |
"""
|
| 16 |
|
| 17 |
from __future__ import annotations
|
|
@@ -25,21 +30,24 @@ from typing import Generator, Any
|
|
| 25 |
|
| 26 |
from pathlib import Path
|
| 27 |
|
| 28 |
-
logger = logging.getLogger(__name__)
|
| 29 |
|
| 30 |
from utils import (
|
| 31 |
PromptTemplate,
|
| 32 |
convert_gradio_to_openai,
|
| 33 |
initialize_client,
|
| 34 |
-
|
| 35 |
upload_azure,
|
| 36 |
record_chat,
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
|
| 40 |
# %% Initialize common assets
|
|
|
|
|
|
|
| 41 |
if os.environ.get("AZURE_ENDPOINT") is None: # Set Azure credentials from local files
|
| 42 |
-
|
| 43 |
client = initialize_client() # Shared across sessions
|
| 44 |
question_mapping: dict[str, str] = json.loads(Path("assets/question_mapping.json").read_text())
|
| 45 |
|
|
@@ -63,7 +71,7 @@ def initialize_interview(request: gr.Request) -> tuple:
|
|
| 63 |
request_params = request.query_params
|
| 64 |
user_id: str = request_params.get("user", "testUser")
|
| 65 |
session_id: str = request_params.get("session", "testSession")
|
| 66 |
-
|
| 67 |
|
| 68 |
# Parse question
|
| 69 |
question_id: str = request_params.get("questionid", "0")
|
|
@@ -72,13 +80,13 @@ def initialize_interview(request: gr.Request) -> tuple:
|
|
| 72 |
question_wording: str = question_data["question"]
|
| 73 |
question_choices: str = question_data["choices"]
|
| 74 |
response_text: str = question_choices[int(response_id)]
|
| 75 |
-
|
| 76 |
|
| 77 |
# Load initial and system messages
|
| 78 |
initial_message: str = PromptTemplate.from_file("assets/initial_message.txt").format(surveyQuestion=question_wording)
|
| 79 |
system_message: str = PromptTemplate.from_file("assets/system_message.txt").format(surveyQuestion=question_wording, responseVal=response_text)
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
# Return all
|
| 84 |
return (
|
|
@@ -171,8 +179,8 @@ def log_interaction(
|
|
| 171 |
session_id: str,
|
| 172 |
) -> None:
|
| 173 |
"Record last pair of interactions"
|
| 174 |
-
record_chat(session_id, "user", chat_history[-1][0])
|
| 175 |
-
record_chat(session_id, "bot", chat_history[-1][1])
|
| 176 |
|
| 177 |
|
| 178 |
def interview_end_check(
|
|
@@ -275,6 +283,7 @@ with gr.Blocks(theme="sudeepshouche/minimalist") as demo:
|
|
| 275 |
icon="./arrow_icon.svg",
|
| 276 |
)
|
| 277 |
exitButton = gr.Button("Save and Exit", visible=False, variant="stop")
|
|
|
|
| 278 |
# Footer
|
| 279 |
disclaimer = gr.HTML(
|
| 280 |
"""
|
|
@@ -355,6 +364,9 @@ with gr.Blocks(theme="sudeepshouche/minimalist") as demo:
|
|
| 355 |
exitButton.click(
|
| 356 |
end_interview, inputs=[sessionId, chatDisplay], outputs=[chatDisplay]
|
| 357 |
)
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
|
| 360 |
if __name__ == "__main__":
|
|
|
|
| 11 |
- Need to call Request from start state
|
| 12 |
- Example URL: localhost:7860/?user=123&session=456&questionid=0&response=0
|
| 13 |
|
| 14 |
+
TODO:
|
| 15 |
+
- Test interview ending behavior: does it get triggered reliably?
|
| 16 |
+
- Add password protection
|
| 17 |
|
| 18 |
+
Pre-flight:
|
| 19 |
+
- Check dotenv values match Gradio secrets
|
| 20 |
"""
|
| 21 |
|
| 22 |
from __future__ import annotations
|
|
|
|
| 30 |
|
| 31 |
from pathlib import Path
|
| 32 |
|
|
|
|
| 33 |
|
| 34 |
from utils import (
|
| 35 |
PromptTemplate,
|
| 36 |
convert_gradio_to_openai,
|
| 37 |
initialize_client,
|
| 38 |
+
load_dotenv,
|
| 39 |
upload_azure,
|
| 40 |
record_chat,
|
| 41 |
+
ChatLoggerHandler,
|
| 42 |
+
auth_no_user
|
| 43 |
)
|
| 44 |
|
| 45 |
|
| 46 |
# %% Initialize common assets
|
| 47 |
+
base_logger = logging.getLogger(__name__)
|
| 48 |
+
chat_logger = ChatLoggerHandler()
|
| 49 |
if os.environ.get("AZURE_ENDPOINT") is None: # Set Azure credentials from local files
|
| 50 |
+
load_dotenv()
|
| 51 |
client = initialize_client() # Shared across sessions
|
| 52 |
question_mapping: dict[str, str] = json.loads(Path("assets/question_mapping.json").read_text())
|
| 53 |
|
|
|
|
| 71 |
request_params = request.query_params
|
| 72 |
user_id: str = request_params.get("user", "testUser")
|
| 73 |
session_id: str = request_params.get("session", "testSession")
|
| 74 |
+
base_logger.info(f"User: {user_id} (Session: {session_id})")
|
| 75 |
|
| 76 |
# Parse question
|
| 77 |
question_id: str = request_params.get("questionid", "0")
|
|
|
|
| 80 |
question_wording: str = question_data["question"]
|
| 81 |
question_choices: str = question_data["choices"]
|
| 82 |
response_text: str = question_choices[int(response_id)]
|
| 83 |
+
base_logger.info(f"Question: {question_wording} ({response_text})")
|
| 84 |
|
| 85 |
# Load initial and system messages
|
| 86 |
initial_message: str = PromptTemplate.from_file("assets/initial_message.txt").format(surveyQuestion=question_wording)
|
| 87 |
system_message: str = PromptTemplate.from_file("assets/system_message.txt").format(surveyQuestion=question_wording, responseVal=response_text)
|
| 88 |
+
base_logger.info(f"Initial message: {initial_message}")
|
| 89 |
+
base_logger.info(f"System message: {system_message}")
|
| 90 |
|
| 91 |
# Return all
|
| 92 |
return (
|
|
|
|
| 179 |
session_id: str,
|
| 180 |
) -> None:
|
| 181 |
"Record last pair of interactions"
|
| 182 |
+
record_chat(chat_logger, session_id, "user", chat_history[-1][0])
|
| 183 |
+
record_chat(chat_logger, session_id, "bot", chat_history[-1][1])
|
| 184 |
|
| 185 |
|
| 186 |
def interview_end_check(
|
|
|
|
| 283 |
icon="./arrow_icon.svg",
|
| 284 |
)
|
| 285 |
exitButton = gr.Button("Save and Exit", visible=False, variant="stop")
|
| 286 |
+
# testExitButton = gr.Button("Save and Exit", visible=True, variant="stop")
|
| 287 |
# Footer
|
| 288 |
disclaimer = gr.HTML(
|
| 289 |
"""
|
|
|
|
| 364 |
exitButton.click(
|
| 365 |
end_interview, inputs=[sessionId, chatDisplay], outputs=[chatDisplay]
|
| 366 |
)
|
| 367 |
+
# testExitButton.click(
|
| 368 |
+
# end_interview, inputs=[sessionId, chatDisplay], outputs=[chatDisplay]
|
| 369 |
+
# )
|
| 370 |
|
| 371 |
|
| 372 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -3,4 +3,5 @@ openai
|
|
| 3 |
wandb
|
| 4 |
azure-storage-blob
|
| 5 |
azure-identity
|
| 6 |
-
debugpy
|
|
|
|
|
|
| 3 |
wandb
|
| 4 |
azure-storage-blob
|
| 5 |
azure-identity
|
| 6 |
+
debugpy
|
| 7 |
+
python-dotenv
|
utils.py
CHANGED
|
@@ -6,6 +6,7 @@ import os
|
|
| 6 |
from configparser import ConfigParser
|
| 7 |
from pathlib import Path
|
| 8 |
from string import Formatter
|
|
|
|
| 9 |
|
| 10 |
import openai
|
| 11 |
from azure.storage.blob import BlobClient
|
|
@@ -24,10 +25,13 @@ class ChatLoggerHandler:
|
|
| 24 |
|
| 25 |
def record(self, session: str, role: str, record: str):
|
| 26 |
log_entry = {
|
| 27 |
-
"
|
| 28 |
-
"
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
-
(self.logdir / f"{session}.jsonl"
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
def record_chat(
|
|
@@ -123,6 +127,12 @@ def convert_openai_to_gradio(
|
|
| 123 |
return chat_history
|
| 124 |
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
def seed_azure_key(cfg: str = "~/.cfg/openai.cfg") -> None:
|
| 127 |
config = ConfigParser()
|
| 128 |
try:
|
|
|
|
| 6 |
from configparser import ConfigParser
|
| 7 |
from pathlib import Path
|
| 8 |
from string import Formatter
|
| 9 |
+
from dotenv import dotenv_values
|
| 10 |
|
| 11 |
import openai
|
| 12 |
from azure.storage.blob import BlobClient
|
|
|
|
| 25 |
|
| 26 |
def record(self, session: str, role: str, record: str):
|
| 27 |
log_entry = {
|
| 28 |
+
"session": session,
|
| 29 |
+
"timestamp": get_current_timestamp(),
|
| 30 |
+
"role": role,
|
| 31 |
+
"message": record,
|
| 32 |
}
|
| 33 |
+
with open(self.logdir / f"{session}.jsonl", "a+") as f:
|
| 34 |
+
f.write(json.dumps(log_entry) + "\n")
|
| 35 |
|
| 36 |
|
| 37 |
def record_chat(
|
|
|
|
| 127 |
return chat_history
|
| 128 |
|
| 129 |
|
| 130 |
+
def load_dotenv():
|
| 131 |
+
config = dotenv_values(".env")
|
| 132 |
+
for key, value in config.items():
|
| 133 |
+
os.environ[key] = value
|
| 134 |
+
|
| 135 |
+
|
| 136 |
def seed_azure_key(cfg: str = "~/.cfg/openai.cfg") -> None:
|
| 137 |
config = ConfigParser()
|
| 138 |
try:
|