gl-kp commited on
Commit
8fa405d
·
verified ·
1 Parent(s): 06fc4db

adding logging in a different way

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -4,7 +4,9 @@ from swarm import Swarm, Agent
4
  from datasets import Dataset
5
  from datetime import datetime
6
  import pandas as pd
 
7
  import os
 
8
  hf_token = os.environ.get("HF_TOKEN")
9
  # Initialize OpenAI client outside of any function to use the same instance throughout the app
10
  openai_client = OpenAI(
@@ -46,7 +48,7 @@ st.title("Market Strategist")
46
  if "messages" not in st.session_state:
47
  st.session_state.messages = []
48
  if "dataset" not in st.session_state:
49
- st.session_state.dataset = Dataset.from_dict({"role": [], "content": []})
50
 
51
  # Display all previous messages
52
  for message in st.session_state.messages:
@@ -57,12 +59,18 @@ for message in st.session_state.messages:
57
  if prompt := st.chat_input("Type your message (or 'exit' to clear history):"):
58
  # If user types 'exit', clear the history
59
  if prompt.strip().lower() == "exit":
60
- if len(st.session_state.dataset) > 0:
61
- dataset_name = "gl-kp/market-strategist-logging" # Replace with your dataset name
62
- st.session_state.dataset.push_to_hub(
63
- dataset_name,
64
- token=hf_token,
65
- private=True
 
 
 
 
 
 
66
  )
67
  st.session_state.messages = []
68
  st.session_state.clear()
@@ -70,10 +78,6 @@ if prompt := st.chat_input("Type your message (or 'exit' to clear history):"):
70
  else:
71
  # Append user message to session state
72
  st.session_state.messages.append({"role": "user", "content": prompt})
73
- st.session_state.dataset = st.session_state.dataset.add_item({
74
- "role": "user",
75
- "content": prompt
76
- })
77
 
78
  with st.chat_message("user"):
79
  st.markdown(prompt)
@@ -96,9 +100,6 @@ if prompt := st.chat_input("Type your message (or 'exit' to clear history):"):
96
  # Display assistant response and append to history
97
  st.markdown(business_report)
98
  st.session_state.messages.append({"role": "assistant", "content": business_report})
99
- st.session_state.dataset = st.session_state.dataset.add_item({
100
- "role": "assistant",
101
- "content": business_report
102
- })
103
 
104
 
 
4
  from datasets import Dataset
5
  from datetime import datetime
6
  import pandas as pd
7
+ from datetime import datetime
8
  import os
9
+
10
  hf_token = os.environ.get("HF_TOKEN")
11
  # Initialize OpenAI client outside of any function to use the same instance throughout the app
12
  openai_client = OpenAI(
 
48
  if "messages" not in st.session_state:
49
  st.session_state.messages = []
50
  if "dataset" not in st.session_state:
51
+ st.session_state.dataset = Dataset.from_dict({"conversation": [], "timestamp": []})
52
 
53
  # Display all previous messages
54
  for message in st.session_state.messages:
 
59
  if prompt := st.chat_input("Type your message (or 'exit' to clear history):"):
60
  # If user types 'exit', clear the history
61
  if prompt.strip().lower() == "exit":
62
+ conversation_str = "\n".join(
63
+ [f"{msg['role']}: {msg['content']}" for msg in st.session_state.messages]
64
+ )
65
+ st.session_state.dataset = st.session_state.dataset.add_item({
66
+ "conversation": conversation_str,
67
+ "timestamp": datetime.now().isoformat()
68
+ })
69
+ dataset_name = "gl-kp/market-strategist-logging" # Replace with your dataset name
70
+ st.session_state.dataset.push_to_hub(
71
+ dataset_name,
72
+ token=hf_token,
73
+ private=True
74
  )
75
  st.session_state.messages = []
76
  st.session_state.clear()
 
78
  else:
79
  # Append user message to session state
80
  st.session_state.messages.append({"role": "user", "content": prompt})
 
 
 
 
81
 
82
  with st.chat_message("user"):
83
  st.markdown(prompt)
 
100
  # Display assistant response and append to history
101
  st.markdown(business_report)
102
  st.session_state.messages.append({"role": "assistant", "content": business_report})
103
+
 
 
 
104
 
105