Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,8 @@ from agents import QuestionGeneratorAgent, SynthesisAgent
|
|
| 5 |
from utils import GroqClient
|
| 6 |
from storage import (
|
| 7 |
add_session, add_response, get_session,
|
| 8 |
-
|
| 9 |
-
|
| 10 |
)
|
| 11 |
from validator import is_weak_answer, generate_follow_up
|
| 12 |
|
|
@@ -72,8 +72,12 @@ def create_app():
|
|
| 72 |
setup_btn.click(initialize_system, inputs=[api_key_input], outputs=[setup_status, api_key_state])
|
| 73 |
|
| 74 |
with gr.Tab("Create Interview"):
|
| 75 |
-
|
| 76 |
-
context_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
generate_btn = gr.Button("Generate Questions", variant="primary")
|
| 78 |
|
| 79 |
questions_display = gr.Dataframe(
|
|
@@ -86,22 +90,21 @@ def create_app():
|
|
| 86 |
status = gr.Textbox(label="Status", interactive=False)
|
| 87 |
link_box = gr.Textbox(label="Session ID (share with respondents)", visible=False, interactive=True)
|
| 88 |
|
| 89 |
-
query_state = gr.State("")
|
| 90 |
questions_state = gr.State([])
|
| 91 |
|
| 92 |
-
def generate_q(api_key,
|
| 93 |
if not api_key:
|
| 94 |
-
return None, "Initialize system first",
|
| 95 |
try:
|
| 96 |
agent = QuestionGeneratorAgent(api_key)
|
| 97 |
-
result = agent.generate(
|
| 98 |
questions = result.get('questions', [])
|
| 99 |
df_data = [[q['id'], q['section'], q['question']] for q in questions]
|
| 100 |
-
return df_data, f"Generated {len(questions)} questions",
|
| 101 |
except Exception as e:
|
| 102 |
-
return None, f"Error: {str(e)}",
|
| 103 |
|
| 104 |
-
def save_q(df_data,
|
| 105 |
try:
|
| 106 |
if df_data is None or len(df_data) == 0:
|
| 107 |
return [], "No questions to save", "", False
|
|
@@ -129,14 +132,14 @@ def create_app():
|
|
| 129 |
return [], "No valid questions found", "", False
|
| 130 |
|
| 131 |
session_id = str(uuid.uuid4())[:8]
|
| 132 |
-
add_session(session_id, questions,
|
| 133 |
|
| 134 |
return questions, f"Saved! Session ID: {session_id}", session_id, True
|
| 135 |
except Exception as e:
|
| 136 |
return [], f"Error: {str(e)}", "", False
|
| 137 |
|
| 138 |
-
generate_btn.click(generate_q, [api_key_state,
|
| 139 |
-
save_btn.click(save_q, [questions_display,
|
| 140 |
|
| 141 |
with gr.Tab("Interview Form"):
|
| 142 |
gr.Markdown("### Respondents: Enter Session ID to start")
|
|
@@ -209,7 +212,6 @@ def create_app():
|
|
| 209 |
qs = session['questions']
|
| 210 |
total = len(qs)
|
| 211 |
|
| 212 |
-
# Check if answer is weak
|
| 213 |
is_weak, reason = is_weak_answer(ans)
|
| 214 |
|
| 215 |
if is_weak and attempt_count < 2:
|
|
@@ -298,20 +300,19 @@ def create_app():
|
|
| 298 |
|
| 299 |
try:
|
| 300 |
session = get_session(sid)
|
| 301 |
-
query = session['query']
|
| 302 |
api_key = session['api_key']
|
| 303 |
|
| 304 |
-
# Get all responses
|
| 305 |
-
all_responses, total_sessions =
|
| 306 |
|
| 307 |
# Generate global analysis
|
| 308 |
syn = SynthesisAgent(api_key)
|
| 309 |
-
analysis = syn.aggregate_interviews(all_responses
|
| 310 |
|
| 311 |
# Update global analysis
|
| 312 |
-
update_global_analysis(
|
| 313 |
|
| 314 |
-
return f"Interview submitted successfully! Thank you.\n\nTotal interviews
|
| 315 |
except Exception as e:
|
| 316 |
print(f"Error in analysis: {e}")
|
| 317 |
return f"Interview submitted! Thank you.\n\nAnalysis update failed but your responses are saved."
|
|
@@ -349,10 +350,10 @@ def create_app():
|
|
| 349 |
)
|
| 350 |
|
| 351 |
with gr.Tab("Analysis Dashboard"):
|
| 352 |
-
gr.Markdown("###
|
|
|
|
| 353 |
|
| 354 |
-
|
| 355 |
-
load_analysis_btn = gr.Button("Load Analysis", variant="primary")
|
| 356 |
|
| 357 |
with gr.Row():
|
| 358 |
with gr.Column(scale=1):
|
|
@@ -373,43 +374,39 @@ def create_app():
|
|
| 373 |
|
| 374 |
if insight.get('quotes'):
|
| 375 |
html += '<div style="margin-top: 8px;">'
|
| 376 |
-
for quote in insight['quotes'][:2]:
|
| 377 |
html += f'<div class="quote">"{quote}"</div>'
|
| 378 |
html += '</div>'
|
| 379 |
|
| 380 |
html += '</div>'
|
| 381 |
return html
|
| 382 |
|
| 383 |
-
def load_analysis(
|
| 384 |
-
if not query or not query.strip():
|
| 385 |
-
return "Please enter a query", "", ""
|
| 386 |
-
|
| 387 |
try:
|
| 388 |
-
|
| 389 |
|
| 390 |
-
if not
|
| 391 |
return (
|
| 392 |
"<div class='stat-card'>No analysis available yet. Submit interviews first.</div>",
|
| 393 |
"",
|
| 394 |
""
|
| 395 |
)
|
| 396 |
|
| 397 |
-
analysis =
|
| 398 |
-
total_sessions = len(project['sessions'])
|
| 399 |
total_interviews = analysis.get('total_interviews', 0)
|
|
|
|
| 400 |
|
| 401 |
# Stats HTML
|
| 402 |
stats_html = f"""
|
| 403 |
<div class='stat-card'>
|
| 404 |
<h3>Study Statistics</h3>
|
| 405 |
-
<p><strong>Query:</strong> {query}</p>
|
| 406 |
<p><strong>Total Interviews:</strong> {total_interviews}</p>
|
| 407 |
<p><strong>Sessions Created:</strong> {total_sessions}</p>
|
| 408 |
</div>
|
| 409 |
"""
|
| 410 |
|
| 411 |
# Update info
|
| 412 |
-
updated_at =
|
| 413 |
update_html = f"""
|
| 414 |
<div class='stat-card'>
|
| 415 |
<h3>Last Updated</h3>
|
|
@@ -483,7 +480,6 @@ def create_app():
|
|
| 483 |
|
| 484 |
load_analysis_btn.click(
|
| 485 |
load_analysis,
|
| 486 |
-
inputs=[query_input_results],
|
| 487 |
outputs=[stats_display, update_info, analysis_display]
|
| 488 |
)
|
| 489 |
|
|
|
|
| 5 |
from utils import GroqClient
|
| 6 |
from storage import (
|
| 7 |
add_session, add_response, get_session,
|
| 8 |
+
get_all_responses, update_global_analysis,
|
| 9 |
+
get_global_data
|
| 10 |
)
|
| 11 |
from validator import is_weak_answer, generate_follow_up
|
| 12 |
|
|
|
|
| 72 |
setup_btn.click(initialize_system, inputs=[api_key_input], outputs=[setup_status, api_key_state])
|
| 73 |
|
| 74 |
with gr.Tab("Create Interview"):
|
| 75 |
+
gr.Markdown("### Generate interview questions")
|
| 76 |
+
context_input = gr.Textbox(
|
| 77 |
+
label="Context (optional)",
|
| 78 |
+
lines=3,
|
| 79 |
+
placeholder="e.g., Premium sports shoe buyers, Coffee shop regulars, etc."
|
| 80 |
+
)
|
| 81 |
generate_btn = gr.Button("Generate Questions", variant="primary")
|
| 82 |
|
| 83 |
questions_display = gr.Dataframe(
|
|
|
|
| 90 |
status = gr.Textbox(label="Status", interactive=False)
|
| 91 |
link_box = gr.Textbox(label="Session ID (share with respondents)", visible=False, interactive=True)
|
| 92 |
|
|
|
|
| 93 |
questions_state = gr.State([])
|
| 94 |
|
| 95 |
+
def generate_q(api_key, context):
|
| 96 |
if not api_key:
|
| 97 |
+
return None, "Initialize system first", [], "", False
|
| 98 |
try:
|
| 99 |
agent = QuestionGeneratorAgent(api_key)
|
| 100 |
+
result = agent.generate(context)
|
| 101 |
questions = result.get('questions', [])
|
| 102 |
df_data = [[q['id'], q['section'], q['question']] for q in questions]
|
| 103 |
+
return df_data, f"Generated {len(questions)} questions", questions, "", False
|
| 104 |
except Exception as e:
|
| 105 |
+
return None, f"Error: {str(e)}", [], "", False
|
| 106 |
|
| 107 |
+
def save_q(df_data, api_key):
|
| 108 |
try:
|
| 109 |
if df_data is None or len(df_data) == 0:
|
| 110 |
return [], "No questions to save", "", False
|
|
|
|
| 132 |
return [], "No valid questions found", "", False
|
| 133 |
|
| 134 |
session_id = str(uuid.uuid4())[:8]
|
| 135 |
+
add_session(session_id, questions, api_key)
|
| 136 |
|
| 137 |
return questions, f"Saved! Session ID: {session_id}", session_id, True
|
| 138 |
except Exception as e:
|
| 139 |
return [], f"Error: {str(e)}", "", False
|
| 140 |
|
| 141 |
+
generate_btn.click(generate_q, [api_key_state, context_input], [questions_display, status, questions_state, link_box, link_box])
|
| 142 |
+
save_btn.click(save_q, [questions_display, api_key_state], [questions_state, status, link_box, link_box])
|
| 143 |
|
| 144 |
with gr.Tab("Interview Form"):
|
| 145 |
gr.Markdown("### Respondents: Enter Session ID to start")
|
|
|
|
| 212 |
qs = session['questions']
|
| 213 |
total = len(qs)
|
| 214 |
|
|
|
|
| 215 |
is_weak, reason = is_weak_answer(ans)
|
| 216 |
|
| 217 |
if is_weak and attempt_count < 2:
|
|
|
|
| 300 |
|
| 301 |
try:
|
| 302 |
session = get_session(sid)
|
|
|
|
| 303 |
api_key = session['api_key']
|
| 304 |
|
| 305 |
+
# Get all responses
|
| 306 |
+
all_responses, total_sessions = get_all_responses()
|
| 307 |
|
| 308 |
# Generate global analysis
|
| 309 |
syn = SynthesisAgent(api_key)
|
| 310 |
+
analysis = syn.aggregate_interviews(all_responses)
|
| 311 |
|
| 312 |
# Update global analysis
|
| 313 |
+
update_global_analysis(analysis)
|
| 314 |
|
| 315 |
+
return f"Interview submitted successfully! Thank you.\n\nTotal interviews in system: {len(all_responses)}\n\nGlobal analysis has been updated!"
|
| 316 |
except Exception as e:
|
| 317 |
print(f"Error in analysis: {e}")
|
| 318 |
return f"Interview submitted! Thank you.\n\nAnalysis update failed but your responses are saved."
|
|
|
|
| 350 |
)
|
| 351 |
|
| 352 |
with gr.Tab("Analysis Dashboard"):
|
| 353 |
+
gr.Markdown("### Empathy Map Analysis")
|
| 354 |
+
gr.Markdown("View aggregated insights from all interviews")
|
| 355 |
|
| 356 |
+
load_analysis_btn = gr.Button("Load Analysis", variant="primary", size="lg")
|
|
|
|
| 357 |
|
| 358 |
with gr.Row():
|
| 359 |
with gr.Column(scale=1):
|
|
|
|
| 374 |
|
| 375 |
if insight.get('quotes'):
|
| 376 |
html += '<div style="margin-top: 8px;">'
|
| 377 |
+
for quote in insight['quotes'][:2]:
|
| 378 |
html += f'<div class="quote">"{quote}"</div>'
|
| 379 |
html += '</div>'
|
| 380 |
|
| 381 |
html += '</div>'
|
| 382 |
return html
|
| 383 |
|
| 384 |
+
def load_analysis():
|
|
|
|
|
|
|
|
|
|
| 385 |
try:
|
| 386 |
+
global_data = get_global_data()
|
| 387 |
|
| 388 |
+
if not global_data['global_analysis']:
|
| 389 |
return (
|
| 390 |
"<div class='stat-card'>No analysis available yet. Submit interviews first.</div>",
|
| 391 |
"",
|
| 392 |
""
|
| 393 |
)
|
| 394 |
|
| 395 |
+
analysis = global_data['global_analysis']
|
|
|
|
| 396 |
total_interviews = analysis.get('total_interviews', 0)
|
| 397 |
+
total_sessions = global_data['total_sessions']
|
| 398 |
|
| 399 |
# Stats HTML
|
| 400 |
stats_html = f"""
|
| 401 |
<div class='stat-card'>
|
| 402 |
<h3>Study Statistics</h3>
|
|
|
|
| 403 |
<p><strong>Total Interviews:</strong> {total_interviews}</p>
|
| 404 |
<p><strong>Sessions Created:</strong> {total_sessions}</p>
|
| 405 |
</div>
|
| 406 |
"""
|
| 407 |
|
| 408 |
# Update info
|
| 409 |
+
updated_at = global_data.get('analysis_updated_at', 'Unknown')
|
| 410 |
update_html = f"""
|
| 411 |
<div class='stat-card'>
|
| 412 |
<h3>Last Updated</h3>
|
|
|
|
| 480 |
|
| 481 |
load_analysis_btn.click(
|
| 482 |
load_analysis,
|
|
|
|
| 483 |
outputs=[stats_display, update_info, analysis_display]
|
| 484 |
)
|
| 485 |
|