Basitha commited on
Commit
b732bbd
·
verified ·
1 Parent(s): 7368a7d

darkmode trial

Browse files
Files changed (1) hide show
  1. PanelInterface.py +72 -25
PanelInterface.py CHANGED
@@ -10,50 +10,42 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
10
  def build_interface(respondent_agents_dict, processor_llm):
11
 
12
  def chatbot_interface(message, history=None):
13
- """
14
- Handles chatbot interaction for Gradio.
15
- Maintains and uses last_respondent_agent from history.
16
- """
17
  if history is None or not isinstance(history, dict):
18
  history = {"chat": [], "last_respondent_agent": None}
19
-
20
  last_respondent_agent = history.get("last_respondent_agent")
21
  logging.info(f"User message received: {message}")
22
  logging.info(f"Last respondent agent: {last_respondent_agent}")
23
-
24
  try:
25
- # Pass last_respondent_agent into the agent selection logic
26
  responses = ask_interview_question(respondent_agents_dict, last_respondent_agent, message, processor_llm)
27
  logging.info(f"Interview responses: {responses}")
28
  except Exception as e:
29
  logging.error(f"Error during interview processing: {e}")
30
  responses = [("System", "Sorry, something went wrong.")]
31
-
32
- # Normalize responses into a list of (agent, response) tuples
33
  if isinstance(responses, str):
34
  responses = [("System", responses)]
35
  elif isinstance(responses, list):
36
  responses = [(r.get("agent", "Unknown"), r.get("response", str(r))) if isinstance(r, dict) else ("Unknown", str(r)) for r in responses]
37
  else:
38
  responses = [("Unknown", str(responses))]
39
-
40
- # Append responses and update last_respondent_agent
41
  for agent, response in responses:
42
  history["chat"].append({
43
  "user": message,
44
  "agent": agent,
45
  "response": response
46
  })
47
- history["last_respondent_agent"] = agent # update with most recent
48
-
49
- # Format for Chatbot display: list of [user, response] pairs
50
  chat_ui = []
51
  for entry in history["chat"]:
52
  chat_ui.append({"role": "user", "content": entry["user"]})
53
  chat_ui.append({"role": "assistant", "content": entry["response"]})
54
-
55
- return chat_ui, "", history
56
-
57
  logging.info("Building Gradio interface...")
58
 
59
  panelists = [
@@ -92,26 +84,81 @@ def build_interface(respondent_agents_dict, processor_llm):
92
  - Our agents have been trained exclusively on topics relevant to youth attitudes, behaviours, and lifestyle preferences.
93
  - To ensure meaningful and respectful conversations, please do not ask inappropriate, offensive, or out-of-scope questions.
94
  """)
95
-
96
  gr.HTML(f"""
97
  <div style='text-align: left;'>
98
  <h2>Our Panellists</h2>
99
  <i>Click on our panellist's name so they can introduce themselves</i><br>
100
  {panel_html}
101
  </div>
102
- <div id='bioModal' style='display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: white; border: 1px solid #ccc; box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2); width: 400px; max-height: 300px; padding: 20px; text-align: left; z-index: 1000; overflow-y: auto;'>
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  <p id='bioContent'></p>
104
- <button onclick='closeModal()' style='margin-top: 10px; padding: 5px 10px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;'>Close</button>
105
  </div>
106
- <div id='modalBackdrop' style='display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 999;' onclick='closeModal()'></div>
 
 
 
 
 
 
 
 
107
  <script>
108
  const bios = {json.dumps(bios_js_object)};
 
109
  function showModal(event, name) {{
110
  event.preventDefault();
111
- document.getElementById('bioContent').innerText = bios[name] || 'Bio not found.';
112
- document.getElementById('bioModal').style.display = 'block';
113
- document.getElementById('modalBackdrop').style.display = 'block';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  }}
 
115
  function closeModal() {{
116
  document.getElementById('bioModal').style.display = 'none';
117
  document.getElementById('modalBackdrop').style.display = 'none';
@@ -137,4 +184,4 @@ def build_interface(respondent_agents_dict, processor_llm):
137
  """)
138
 
139
  logging.info("Interface build complete.")
140
- return demo
 
10
  def build_interface(respondent_agents_dict, processor_llm):
11
 
12
  def chatbot_interface(message, history=None):
 
 
 
 
13
  if history is None or not isinstance(history, dict):
14
  history = {"chat": [], "last_respondent_agent": None}
15
+
16
  last_respondent_agent = history.get("last_respondent_agent")
17
  logging.info(f"User message received: {message}")
18
  logging.info(f"Last respondent agent: {last_respondent_agent}")
19
+
20
  try:
 
21
  responses = ask_interview_question(respondent_agents_dict, last_respondent_agent, message, processor_llm)
22
  logging.info(f"Interview responses: {responses}")
23
  except Exception as e:
24
  logging.error(f"Error during interview processing: {e}")
25
  responses = [("System", "Sorry, something went wrong.")]
26
+
 
27
  if isinstance(responses, str):
28
  responses = [("System", responses)]
29
  elif isinstance(responses, list):
30
  responses = [(r.get("agent", "Unknown"), r.get("response", str(r))) if isinstance(r, dict) else ("Unknown", str(r)) for r in responses]
31
  else:
32
  responses = [("Unknown", str(responses))]
33
+
 
34
  for agent, response in responses:
35
  history["chat"].append({
36
  "user": message,
37
  "agent": agent,
38
  "response": response
39
  })
40
+ history["last_respondent_agent"] = agent
41
+
 
42
  chat_ui = []
43
  for entry in history["chat"]:
44
  chat_ui.append({"role": "user", "content": entry["user"]})
45
  chat_ui.append({"role": "assistant", "content": entry["response"]})
46
+
47
+ return chat_ui, "", history
48
+
49
  logging.info("Building Gradio interface...")
50
 
51
  panelists = [
 
84
  - Our agents have been trained exclusively on topics relevant to youth attitudes, behaviours, and lifestyle preferences.
85
  - To ensure meaningful and respectful conversations, please do not ask inappropriate, offensive, or out-of-scope questions.
86
  """)
87
+
88
  gr.HTML(f"""
89
  <div style='text-align: left;'>
90
  <h2>Our Panellists</h2>
91
  <i>Click on our panellist's name so they can introduce themselves</i><br>
92
  {panel_html}
93
  </div>
94
+ <div id='bioModal' style='
95
+ display: none;
96
+ position: fixed;
97
+ top: 50%;
98
+ left: 50%;
99
+ transform: translate(-50%, -50%);
100
+ width: 400px;
101
+ max-height: 300px;
102
+ padding: 20px;
103
+ text-align: left;
104
+ z-index: 1000;
105
+ overflow-y: auto;
106
+ border-radius: 10px;
107
+ '>
108
  <p id='bioContent'></p>
109
+ <button onclick='closeModal()' id='closeButton'>Close</button>
110
  </div>
111
+ <div id='modalBackdrop' style='
112
+ display: none;
113
+ position: fixed;
114
+ top: 0;
115
+ left: 0;
116
+ width: 100%;
117
+ height: 100%;
118
+ z-index: 999;
119
+ ' onclick='closeModal()'></div>
120
  <script>
121
  const bios = {json.dumps(bios_js_object)};
122
+
123
  function showModal(event, name) {{
124
  event.preventDefault();
125
+
126
+ const modal = document.getElementById('bioModal');
127
+ const backdrop = document.getElementById('modalBackdrop');
128
+ const closeButton = document.getElementById('closeButton');
129
+ const bioContent = document.getElementById('bioContent');
130
+
131
+ bioContent.innerText = bios[name] || 'Bio not found.';
132
+ modal.style.display = 'block';
133
+ backdrop.style.display = 'block';
134
+
135
+ const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
136
+
137
+ if (prefersDark) {{
138
+ modal.style.backgroundColor = '#1e1e1e';
139
+ modal.style.color = '#ffffff';
140
+ modal.style.border = '1px solid #444';
141
+ modal.style.boxShadow = '0px 4px 6px rgba(0, 0, 0, 0.8)';
142
+ closeButton.style.backgroundColor = '#007bff';
143
+ closeButton.style.color = 'white';
144
+ backdrop.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
145
+ }} else {{
146
+ modal.style.backgroundColor = '#ffffff';
147
+ modal.style.color = '#000000';
148
+ modal.style.border = '1px solid #ccc';
149
+ modal.style.boxShadow = '0px 4px 6px rgba(0, 0, 0, 0.2)';
150
+ closeButton.style.backgroundColor = '#007bff';
151
+ closeButton.style.color = 'white';
152
+ backdrop.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
153
+ }}
154
+
155
+ closeButton.style.marginTop = '10px';
156
+ closeButton.style.padding = '5px 10px';
157
+ closeButton.style.border = 'none';
158
+ closeButton.style.borderRadius = '5px';
159
+ closeButton.style.cursor = 'pointer';
160
  }}
161
+
162
  function closeModal() {{
163
  document.getElementById('bioModal').style.display = 'none';
164
  document.getElementById('modalBackdrop').style.display = 'none';
 
184
  """)
185
 
186
  logging.info("Interface build complete.")
187
+ return demo