Spaces:
Build error
Build error
Update common/ResponseValidation.py
Browse files- common/ResponseValidation.py +59 -25
common/ResponseValidation.py
CHANGED
|
@@ -34,44 +34,84 @@ First Person: No
|
|
| 34 |
return False
|
| 35 |
|
| 36 |
|
| 37 |
-
def matches_user_speaking_style(answer, transcript_text, processor_llm):
|
| 38 |
"""
|
| 39 |
Uses the LLM to determine if the answer matches the tone and style of the user's prior speaking style in the transcript.
|
| 40 |
Returns True if similar, False otherwise.
|
|
|
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
Style Match: Yes
|
| 56 |
-
or
|
| 57 |
-
Style Match: No
|
| 58 |
-
"""
|
| 59 |
-
try:
|
| 60 |
-
logging.info("[Style Match Check] Invoking LLM to compare tone and style with prior transcript")
|
| 61 |
response = processor_llm.invoke(prompt)
|
| 62 |
result = response.content.strip().lower()
|
|
|
|
| 63 |
if "style match: yes" in result:
|
|
|
|
| 64 |
return True
|
| 65 |
elif "style match: no" in result:
|
|
|
|
| 66 |
return False
|
| 67 |
else:
|
| 68 |
-
logging.warning(f"
|
| 69 |
return False
|
|
|
|
| 70 |
except Exception as e:
|
| 71 |
-
logging.error(f"LLM failed during
|
| 72 |
return False
|
| 73 |
|
| 74 |
-
|
| 75 |
def validate_response(question, answer, user_profile_str, fast_facts_str, interview_transcript_text, respondent_type, ai_evaluator_agent, processor_llm):
|
| 76 |
llm_mode_prompt = f"""
|
| 77 |
You are an expert in market research interview analysis. Given the following question, determine if it is:
|
|
@@ -131,9 +171,6 @@ def validate_response(question, answer, user_profile_str, fast_facts_str, interv
|
|
| 131 |
if not is_first_person(answer, processor_llm):
|
| 132 |
logging.warning("Did not pass style due to 3rd person use")
|
| 133 |
return False
|
| 134 |
-
#if not matches_user_speaking_style(answer, interview_transcript_text, processor_llm):
|
| 135 |
-
#logging.warning("Did not match user's speaking style from transcript")
|
| 136 |
-
#return False
|
| 137 |
return True
|
| 138 |
return False
|
| 139 |
else:
|
|
@@ -166,8 +203,5 @@ def validate_response(question, answer, user_profile_str, fast_facts_str, interv
|
|
| 166 |
if not is_first_person(answer, processor_llm):
|
| 167 |
logging.warning("Did not pass style due to 3rd person use")
|
| 168 |
return False
|
| 169 |
-
#if not matches_user_speaking_style(answer, interview_transcript_text, processor_llm):
|
| 170 |
-
#logging.warning("Did not match user's speaking style from transcript")
|
| 171 |
-
#return False
|
| 172 |
return True
|
| 173 |
return False
|
|
|
|
| 34 |
return False
|
| 35 |
|
| 36 |
|
| 37 |
+
def matches_user_speaking_style(answer, transcript_text, processor_llm, user_profile, agent_question):
|
| 38 |
"""
|
| 39 |
Uses the LLM to determine if the answer matches the tone and style of the user's prior speaking style in the transcript.
|
| 40 |
Returns True if similar, False otherwise.
|
| 41 |
+
Incorporates logic to skip style matching for factual questions and uses profile-based criteria.
|
| 42 |
"""
|
| 43 |
+
logging.info("[Style Match Check] Entry")
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
# Get communication profile
|
| 47 |
+
style = user_profile.get_field("Communication", "Style")
|
| 48 |
+
tone = user_profile.get_field("Communication", "Tone")
|
| 49 |
+
length = user_profile.get_field("Communication", "Length")
|
| 50 |
+
topics = user_profile.get_field("Communication", "Topics")
|
| 51 |
|
| 52 |
+
# Identify factual-type question
|
| 53 |
+
factual_keywords = [
|
| 54 |
+
"name", "age", "where are you from", "where do you live", "occupation",
|
| 55 |
+
"birthplace", "what do you do", "how old", "which city", "which country"
|
| 56 |
+
]
|
| 57 |
+
lower_q = agent_question.strip().lower()
|
| 58 |
+
is_factual = any(kw in lower_q for kw in factual_keywords)
|
| 59 |
|
| 60 |
+
if is_factual:
|
| 61 |
+
logging.info("[Style Match Check] Question is factual — skipping style comparison")
|
| 62 |
+
return True
|
| 63 |
|
| 64 |
+
prompt = f"""
|
| 65 |
+
You are a writing style and tone analyst.
|
| 66 |
+
|
| 67 |
+
Your job is to assess whether a new response sounds like it was written by the same person who spoke in the interview transcript — considering phrasing, vocabulary, tone, and sentence structure.
|
| 68 |
+
|
| 69 |
+
---
|
| 70 |
+
### Prior Interview Transcript (how the user usually talks):
|
| 71 |
+
\"\"\"{transcript_text}\"\"\"
|
| 72 |
+
|
| 73 |
+
---
|
| 74 |
+
### New Response:
|
| 75 |
+
\"\"\"{answer}\"\"\"
|
| 76 |
+
|
| 77 |
+
---
|
| 78 |
+
### Style Profile Reference:
|
| 79 |
+
- Style: {style}
|
| 80 |
+
- Tone: {tone}
|
| 81 |
+
- Preferred Length: {length}
|
| 82 |
+
- Topics: {topics}
|
| 83 |
+
|
| 84 |
+
---
|
| 85 |
+
### Instructions:
|
| 86 |
+
- Check if the *tone*, *style*, and *language* of the new response align with the transcript.
|
| 87 |
+
- Use the style profile for reference.
|
| 88 |
+
- Focus on phrasing, formality, sentence structure, expressiveness, and personal flair.
|
| 89 |
+
- Ignore topic similarity — you’re assessing delivery style.
|
| 90 |
+
- Reply only with one of the following:
|
| 91 |
+
|
| 92 |
+
Style Match: Yes
|
| 93 |
+
or
|
| 94 |
+
Style Match: No
|
| 95 |
+
"""
|
| 96 |
|
| 97 |
+
logging.info("[Style Match Check] Invoking LLM with style comparison prompt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
response = processor_llm.invoke(prompt)
|
| 99 |
result = response.content.strip().lower()
|
| 100 |
+
|
| 101 |
if "style match: yes" in result:
|
| 102 |
+
logging.info("[Style Match Check] Match confirmed")
|
| 103 |
return True
|
| 104 |
elif "style match: no" in result:
|
| 105 |
+
logging.info("[Style Match Check] Style mismatch detected")
|
| 106 |
return False
|
| 107 |
else:
|
| 108 |
+
logging.warning(f"[Style Match Check] Unexpected response format: {result}")
|
| 109 |
return False
|
| 110 |
+
|
| 111 |
except Exception as e:
|
| 112 |
+
logging.error(f"[Style Match Check] LLM failed during comparison: {e}")
|
| 113 |
return False
|
| 114 |
|
|
|
|
| 115 |
def validate_response(question, answer, user_profile_str, fast_facts_str, interview_transcript_text, respondent_type, ai_evaluator_agent, processor_llm):
|
| 116 |
llm_mode_prompt = f"""
|
| 117 |
You are an expert in market research interview analysis. Given the following question, determine if it is:
|
|
|
|
| 171 |
if not is_first_person(answer, processor_llm):
|
| 172 |
logging.warning("Did not pass style due to 3rd person use")
|
| 173 |
return False
|
|
|
|
|
|
|
|
|
|
| 174 |
return True
|
| 175 |
return False
|
| 176 |
else:
|
|
|
|
| 203 |
if not is_first_person(answer, processor_llm):
|
| 204 |
logging.warning("Did not pass style due to 3rd person use")
|
| 205 |
return False
|
|
|
|
|
|
|
|
|
|
| 206 |
return True
|
| 207 |
return False
|