elaineaishophouse commited on
Commit
b92d360
·
verified ·
1 Parent(s): 11926bf

Update PanelInterface.py

Browse files
Files changed (1) hide show
  1. PanelInterface.py +65 -39
PanelInterface.py CHANGED
@@ -2,43 +2,35 @@
2
  import gradio as gr
3
  import logging
4
 
5
- def chatbot_interface(message, history=None):
6
- if history is None:
7
- history = []
8
- response = f"You asked: {message}"
9
- history.append({"role": "user", "content": message})
10
- history.append({"role": "assistant", "content": response})
11
- return history, "", history
12
-
13
  def build_interface():
14
  logging.info("Building Gradio interface...")
15
 
16
  panelists = [
17
- {"ID": "P1", "Name": "Sourav", "Description": "Student from Kolkata", "Bio": "Sourav is a 25-year-old sociology student from Kolkata."},
18
- {"ID": "P2", "Name": "Anjali", "Description": "College student from Chennai", "Bio": "Anjali is a 17-year-old girl from Chennai, aspiring to be a psychologist."},
19
- {"ID": "P3", "Name": "Ravi", "Description": "Teen from Hyderabad", "Bio": "Ravi is a high schooler passionate about cricket and coding."},
20
- {"ID": "P4", "Name": "Divya", "Description": "Engineering student from Trichy", "Bio": "Divya is 21, studying electronics in Trichy, with interests in AI and robotics."}
21
  ]
22
 
23
- panel_html = ""
24
- bios_dict = {}
25
-
26
- for p in panelists:
27
- panel_html += f'<p><a href="#" onclick="showModal(event, \"{p["ID"]}\")"><b>{p["Name"]}</b></a> – {p["Description"]}</p>'
28
- bios_dict[p["ID"]] = p["Bio"]
29
-
30
- bios_js_object = str(bios_dict).replace("'", "\"")
31
 
32
  with gr.Blocks(css="assets/custom.css") as demo:
 
33
  with gr.Row(elem_classes="logo-row"):
34
- gr.Image("predata-logo.png", height=200, show_label=False, elem_id="logo")
35
 
36
  with gr.Row(elem_classes="welcome-section"):
37
  gr.Markdown("""
38
  # Welcome to PreData.AI's Market Research Panel [Demo]
39
  ## Introducing our AI-powered panel:
40
- - Ask a question by typing into the box below.
41
- - You can address a question to a **specific panellist** or the entire group.
 
 
42
  """)
43
 
44
  with gr.Row():
@@ -46,25 +38,26 @@ def build_interface():
46
  gr.HTML(f"""
47
  <div style='text-align: left;'>
48
  <h2>Our Panellists</h2>
 
49
  {panel_html}
50
  </div>
51
- <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;'>
52
  <p id='bioContent'></p>
53
- <button onclick='closeModal()' style='margin-top:10px;padding:5px 10px;background-color:#007bff;color:white;border:none;border-radius:5px;cursor:pointer;'>Close</button>
54
  </div>
55
- <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>
56
  <script>
57
- const bios = {bios_js_object};
58
- function showModal(event, name) {{
59
- event.preventDefault();
60
- document.getElementById('bioContent').innerText = bios[name] || 'Bio not found.';
61
- document.getElementById('bioModal').style.display = 'block';
62
- document.getElementById('modalBackdrop').style.display = 'block';
63
- }}
64
- function closeModal() {{
65
- document.getElementById('bioModal').style.display = 'none';
66
- document.getElementById('modalBackdrop').style.display = 'none';
67
- }}
68
  </script>
69
  """)
70
 
@@ -73,12 +66,45 @@ def build_interface():
73
  msg = gr.Textbox(placeholder="Ask your question to the panel here...")
74
  history = gr.State([])
75
  msg.submit(chatbot_interface, [msg, history], [chatbot, msg, history])
 
76
 
77
  with gr.Row(elem_classes="footer-row"):
78
  gr.Markdown("""
79
- <div style="text-align:center;margin-top:20px;font-size:14px;">
80
- © 2025 PreData.AI - All rights reserved.
 
 
81
  </div>
82
  """)
83
 
 
84
  return demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
  import logging
4
 
 
 
 
 
 
 
 
 
5
  def build_interface():
6
  logging.info("Building Gradio interface...")
7
 
8
  panelists = [
9
+ {"ID": "Panellist1", "Name": "Sourav", "Description": "25 year old male from Kolkata", "Bio": "Sourav's bio."},
10
+ {"ID": "Panellist2", "Name": "Divya", "Description": "17 year old female from Chennai", "Bio": "Divya's bio."},
11
+ {"ID": "Panellist3", "Name": "Rahul", "Description": "16 year old male from Hyderabad", "Bio": "Rahul's bio."},
12
+ {"ID": "Panellist4", "Name": "Meena", "Description": "21 year old female from Trichy", "Bio": "Meena's bio."}
13
  ]
14
 
15
+ bios_js_object = {p["ID"]: p["Bio"] for p in panelists}
16
+ panel_html = "".join(
17
+ f'<p><a href="#" onclick="showModal(event, \'{p["ID"]}\')"><b>{p["Name"]}</b></a> – {p["Description"]}</p>'
18
+ for p in panelists
19
+ )
 
 
 
20
 
21
  with gr.Blocks(css="assets/custom.css") as demo:
22
+ logging.info("Adding logo and welcome message...")
23
  with gr.Row(elem_classes="logo-row"):
24
+ gr.Image("static/predata-logo.png", height=200, show_label=False, elem_id="logo")
25
 
26
  with gr.Row(elem_classes="welcome-section"):
27
  gr.Markdown("""
28
  # Welcome to PreData.AI's Market Research Panel [Demo]
29
  ## Introducing our AI-powered panel:
30
+ - This panel consists of some **AI-powered agents** who will answer your questions.
31
+ - Ask them a question by **typing into the input box below**.
32
+ - Feel free to address a question to a **specific panellist**.
33
+ - If no panellist is specified, all of the panellists will respond.
34
  """)
35
 
36
  with gr.Row():
 
38
  gr.HTML(f"""
39
  <div style='text-align: left;'>
40
  <h2>Our Panellists</h2>
41
+ <i>Click on our panellist's name so they can introduce themselves</i><br>
42
  {panel_html}
43
  </div>
44
+ <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;'>
45
  <p id='bioContent'></p>
46
+ <button onclick='closeModal()' style='margin-top: 10px; padding: 5px 10px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;'>Close</button>
47
  </div>
48
+ <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>
49
  <script>
50
+ const bios = {str(bios_js_object).replace("'", '"')};
51
+ function showModal(event, name) {{
52
+ event.preventDefault();
53
+ document.getElementById('bioContent').innerText = bios[name] || 'Bio not found.';
54
+ document.getElementById('bioModal').style.display = 'block';
55
+ document.getElementById('modalBackdrop').style.display = 'block';
56
+ }}
57
+ function closeModal() {{
58
+ document.getElementById('bioModal').style.display = 'none';
59
+ document.getElementById('modalBackdrop').style.display = 'none';
60
+ }}
61
  </script>
62
  """)
63
 
 
66
  msg = gr.Textbox(placeholder="Ask your question to the panel here...")
67
  history = gr.State([])
68
  msg.submit(chatbot_interface, [msg, history], [chatbot, msg, history])
69
+ logging.info("Connected message box to chatbot_interface.")
70
 
71
  with gr.Row(elem_classes="footer-row"):
72
  gr.Markdown("""
73
+ <div style="text-align: center; margin-top: 20px; font-size: 14px;">
74
+ © 2025 PreData.AI - All rights reserved. | Contact us at
75
+ <a href="mailto:hello@predata.ai" onclick="event.stopPropagation();"
76
+ style="text-decoration: none; color: #007bff;">hello@predata.ai</a>
77
  </div>
78
  """)
79
 
80
+ logging.info("Interface build complete.")
81
  return demo
82
+
83
+
84
+ def chatbot_interface(message, history=None):
85
+ """
86
+ Handles chatbot interaction. Can be used both in Gradio and from MAIN.
87
+ """
88
+ if history is None:
89
+ history = []
90
+
91
+ logging.info(f"User message received: {message}")
92
+ try:
93
+ responses = ask_interview_question(respondent_agents_dict, message, processor_llm)
94
+ logging.info(f"Interview response is {responses}")
95
+ except Exception as e:
96
+ logging.error(f"Error during interview processing: {e}")
97
+ responses = ["Sorry, something went wrong."]
98
+
99
+ if isinstance(responses, str):
100
+ responses = [responses]
101
+ elif not isinstance(responses, list):
102
+ logging.warning(f"Unexpected response type ({type(responses)}). Wrapping in list.")
103
+ responses = [str(responses)]
104
+
105
+ formatted_responses = [{"role": "assistant", "content": str(r)} for r in responses]
106
+ history.append({"role": "user", "content": message})
107
+ history.extend(formatted_responses)
108
+
109
+ logging.info(f"Returning updated history with {len(formatted_responses)} response(s).")
110
+ return history, "", history