Update app.py
Browse files
app.py
CHANGED
@@ -42,6 +42,8 @@ LOG_DIR = pathlib.Path("logs") # each session: logs/<uuid>.js
|
|
42 |
IMG_DIR = LOG_DIR / "imgs" # cached PNGs (optional)
|
43 |
LOG_DIR.mkdir(exist_ok=True)
|
44 |
IMG_DIR.mkdir(exist_ok=True)
|
|
|
|
|
45 |
|
46 |
# ====== Bedrock client ====== #
|
47 |
bedrock = boto3.client(
|
@@ -99,7 +101,35 @@ def cache_image(session_id: str, pil_img: Image.Image) -> str:
|
|
99 |
pil_img.save(fpath, format="PNG")
|
100 |
return str(fpath)
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
def append_log(session_id: str, user_text: str, assistant_text: str, img_path: Optional[str] = None):
|
|
|
103 |
record = {
|
104 |
"ts": datetime.datetime.utcnow().isoformat(timespec="seconds") + "Z",
|
105 |
"user": user_text,
|
@@ -110,6 +140,9 @@ def append_log(session_id: str, user_text: str, assistant_text: str, img_path: O
|
|
110 |
path = LOG_DIR / f"{session_id}.jsonl"
|
111 |
with path.open("a", encoding="utf-8") as f:
|
112 |
f.write(json.dumps(record, ensure_ascii=False) + "\n")
|
|
|
|
|
|
|
113 |
|
114 |
# ====== Gradio UI ====== #
|
115 |
with gr.Blocks(title="Multimodal Chat") as demo:
|
|
|
42 |
IMG_DIR = LOG_DIR / "imgs" # cached PNGs (optional)
|
43 |
LOG_DIR.mkdir(exist_ok=True)
|
44 |
IMG_DIR.mkdir(exist_ok=True)
|
45 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Set this in your Space's secrets
|
46 |
+
DATASET_NAME = "collinear-ai/amazon-external-premier-chat-logs" # Create this dataset on HF
|
47 |
|
48 |
# ====== Bedrock client ====== #
|
49 |
bedrock = boto3.client(
|
|
|
101 |
pil_img.save(fpath, format="PNG")
|
102 |
return str(fpath)
|
103 |
|
104 |
+
|
105 |
+
|
106 |
+
def upload_to_hf_dataset(session_id: str, user_text: str, assistant_text: str, img_path: Optional[str] = None):
|
107 |
+
"""Upload conversation log to HF Dataset"""
|
108 |
+
try:
|
109 |
+
record = {
|
110 |
+
"session_id": session_id,
|
111 |
+
"timestamp": datetime.datetime.utcnow().isoformat() + "Z",
|
112 |
+
"user_message": user_text,
|
113 |
+
"assistant_message": assistant_text,
|
114 |
+
"has_image": img_path is not None,
|
115 |
+
"image_path": img_path if img_path else ""
|
116 |
+
}
|
117 |
+
|
118 |
+
# Create dataset from single record
|
119 |
+
dataset = Dataset.from_list([record])
|
120 |
+
|
121 |
+
# Push to hub (append mode)
|
122 |
+
dataset.push_to_hub(
|
123 |
+
DATASET_NAME,
|
124 |
+
token=HF_TOKEN,
|
125 |
+
private=True # Keep logs private
|
126 |
+
)
|
127 |
+
except Exception as e:
|
128 |
+
print(f"Failed to upload log: {e}")
|
129 |
+
|
130 |
+
# Replace your append_log function with:
|
131 |
def append_log(session_id: str, user_text: str, assistant_text: str, img_path: Optional[str] = None):
|
132 |
+
# Keep local logging for immediate access
|
133 |
record = {
|
134 |
"ts": datetime.datetime.utcnow().isoformat(timespec="seconds") + "Z",
|
135 |
"user": user_text,
|
|
|
140 |
path = LOG_DIR / f"{session_id}.jsonl"
|
141 |
with path.open("a", encoding="utf-8") as f:
|
142 |
f.write(json.dumps(record, ensure_ascii=False) + "\n")
|
143 |
+
|
144 |
+
# Also upload to HF Dataset
|
145 |
+
upload_to_hf_dataset(session_id, user_text, assistant_text, img_path)
|
146 |
|
147 |
# ====== Gradio UI ====== #
|
148 |
with gr.Blocks(title="Multimodal Chat") as demo:
|