Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1726,30 +1726,60 @@ def get_trust_tip_and_suggestion():
|
|
| 1726 |
return trust_tip, suggestion
|
| 1727 |
|
| 1728 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1729 |
def chatbot_response(message, history):
|
| 1730 |
global selected_dataset_ai
|
| 1731 |
global df_builder_pivot_str
|
| 1732 |
|
| 1733 |
try:
|
| 1734 |
-
# Get the current value of selected_dataset_ai
|
| 1735 |
-
selected_dataset_ai = read_ai_dataset_selection()
|
| 1736 |
-
|
| 1737 |
-
# Recompute DATAFRAME_PROOF_POINT based on the selected dataset
|
| 1738 |
-
df_builder_pivot_str = compute_dataframe_proof_point()
|
| 1739 |
-
|
| 1740 |
# Generate response using the agent executor
|
| 1741 |
-
output = agent_executor.invoke({"input": message, "chat_history":
|
| 1742 |
trust_tip, suggestion = get_trust_tip_and_suggestion()
|
| 1743 |
|
| 1744 |
# Prepend the selected dataset to the response
|
| 1745 |
-
response = f"**Selected Dataset: {selected_dataset_ai}**\n\n
|
| 1746 |
|
| 1747 |
-
if
|
| 1748 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1749 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1750 |
|
| 1751 |
# Save the interaction context
|
| 1752 |
-
|
| 1753 |
[
|
| 1754 |
HumanMessage(content=message),
|
| 1755 |
AIMessage(content=response),
|
|
@@ -1766,8 +1796,6 @@ def chatbot_response(message, history):
|
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
-
|
| 1770 |
-
|
| 1771 |
def read_ai_dataset_selection():
|
| 1772 |
global selected_dataset_ai
|
| 1773 |
return selected_dataset_ai
|
|
|
|
| 1726 |
return trust_tip, suggestion
|
| 1727 |
|
| 1728 |
|
| 1729 |
+
|
| 1730 |
+
def extract_top_scoring_statements(df_builder_pivot_str, buckets_to_include):
|
| 1731 |
+
"""
|
| 1732 |
+
Extracts top-scoring statements for the given trust buckets.
|
| 1733 |
+
Args:
|
| 1734 |
+
df_builder_pivot_str: The computed dataframe proof point string.
|
| 1735 |
+
buckets_to_include: List of trust buckets to include in the response.
|
| 1736 |
+
Returns:
|
| 1737 |
+
A formatted string of top-scoring statements.
|
| 1738 |
+
"""
|
| 1739 |
+
try:
|
| 1740 |
+
# Filter the dataframe for the required buckets
|
| 1741 |
+
filtered_df = df_builder_pivot_str[df_builder_pivot_str["Bucket"].isin(buckets_to_include)]
|
| 1742 |
+
|
| 1743 |
+
# Sort and extract top-scoring statements
|
| 1744 |
+
top_statements = filtered_df.sort_values(by="Score", ascending=False).groupby("Bucket").head(1)
|
| 1745 |
+
return top_statements.to_string(index=False)
|
| 1746 |
+
except Exception as e:
|
| 1747 |
+
logger.error(f"Error extracting top-scoring statements: {e}")
|
| 1748 |
+
return "No top-scoring statements available."
|
| 1749 |
+
|
| 1750 |
+
|
| 1751 |
+
|
| 1752 |
def chatbot_response(message, history):
|
| 1753 |
global selected_dataset_ai
|
| 1754 |
global df_builder_pivot_str
|
| 1755 |
|
| 1756 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1757 |
# Generate response using the agent executor
|
| 1758 |
+
output = agent_executor.invoke({"input": message, "chat_history": history})
|
| 1759 |
trust_tip, suggestion = get_trust_tip_and_suggestion()
|
| 1760 |
|
| 1761 |
# Prepend the selected dataset to the response
|
| 1762 |
+
response = f"**Selected Dataset: {selected_dataset_ai}**\n\n"
|
| 1763 |
|
| 1764 |
+
if selected_dataset_ai == "VW Customers.xlsx":
|
| 1765 |
+
# Include only Development, Benefit, Vision for VW Customers
|
| 1766 |
+
buckets_to_include = ["Development", "Benefit", "Vision"]
|
| 1767 |
+
else:
|
| 1768 |
+
# Include only Stability, Relationship, Benefit for VW Prospects
|
| 1769 |
+
buckets_to_include = ["Stability", "Relationship", "Benefit"]
|
| 1770 |
|
| 1771 |
+
# Extract and format top-scoring statements for the included buckets
|
| 1772 |
+
top_scoring_statements = extract_top_scoring_statements(df_builder_pivot_str, buckets_to_include)
|
| 1773 |
+
response += f"**Top Scoring Statements:**\n\n{top_scoring_statements}\n\n"
|
| 1774 |
+
|
| 1775 |
+
response += output["output"]
|
| 1776 |
+
|
| 1777 |
+
# Add Trust Tip and Suggestion if not already in history
|
| 1778 |
+
if not any(f"**Trust Tip**: {trust_tip}" in entry["bot"] and f"**Suggestion**: {suggestion}" in entry["bot"] for entry in history):
|
| 1779 |
+
response += f"\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
|
| 1780 |
|
| 1781 |
# Save the interaction context
|
| 1782 |
+
history.extend(
|
| 1783 |
[
|
| 1784 |
HumanMessage(content=message),
|
| 1785 |
AIMessage(content=response),
|
|
|
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
|
|
|
|
|
|
| 1799 |
def read_ai_dataset_selection():
|
| 1800 |
global selected_dataset_ai
|
| 1801 |
return selected_dataset_ai
|