Wajahat698 commited on
Commit
d51313e
·
verified ·
1 Parent(s): 96d1c42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -45
app.py CHANGED
@@ -1754,18 +1754,23 @@ def get_top_scoring_statements(dataset):
1754
 
1755
  last_top_scoring_statements = None
1756
  def chatbot_response(message, history):
1757
- global selected_dataset_ai,last_top_scoring_statements
1758
- global df_builder_pivot_str
 
 
 
1759
  try:
1760
- # Recompute DATAFRAME_PROOF_POINT based on the selected dataset
1761
- #df_builder_pivot_str = compute_dataframe_proof_point()
1762
- selected_dataset_ai = read_ai_dataset_selection()
 
 
1763
  datasets = {
1764
  "VW Customers.xlsx": {
1765
  "Development": [
1766
  "We bring together the world's best talent in many disciplines to create your cars. (25%)",
1767
  "Building great and affordable cars is our foundation. (22%)",
1768
- "Our beginnings are a unique combination of investors and unions, and today 9 of our 20 board members are staff representatives. (18%)",
1769
  ],
1770
  "Benefit": [
1771
  "We bring together the world's best talent in many disciplines to create your cars. (23%)",
@@ -1781,13 +1786,13 @@ def chatbot_response(message, history):
1781
  "Volkswagen Non Customers.xlsx": {
1782
  "Stability": [
1783
  "We work with our unions in our restructuring and future plans. (21%)",
1784
- "We have learned from our mistakes in the Diesel Affair and made fundamental changes. (19%)",
1785
  "Building great and affordable cars is our foundation. (18%)",
1786
  ],
1787
  "Relationship": [
1788
  "We put a lot of emphasis on the interior experience and two of our cars have been ranked in the top 10. (24%)",
1789
  "We are at the forefront of technology to deliver better cars and driving experiences. (17%)",
1790
- "Our beginnings are a unique combination of investors and unions, and today 9 of our 20 board members are staff representatives. (17%)",
1791
  ],
1792
  "Competence": [
1793
  "At the heart of our decision-making is the long-term quality of life for all of us. (20%)",
@@ -1797,7 +1802,7 @@ def chatbot_response(message, history):
1797
  },
1798
  }
1799
 
1800
- # Retrieve relevant data
1801
  if selected_dataset_ai in datasets:
1802
  trust_data = datasets[selected_dataset_ai]
1803
  else:
@@ -1810,56 +1815,40 @@ def chatbot_response(message, history):
1810
  for statement in statements:
1811
  top_scoring_statements += f"- {statement}\n"
1812
  top_scoring_statements += "\n"
1813
-
1814
  last_top_scoring_statements = top_scoring_statements
1815
- response = f"{message}\n\n{top_scoring_statements}"
1816
- Combined_prompt = (
1817
- predefined_prompt
1818
- + "\n\n### Top-Scoring Statements for Integration ###\n"
1819
- + top_scoring_statements
1820
- + "\n\nUser Input:\n"
1821
- + message
1822
- )
1823
-
1824
  structured_input = {
1825
- "input": Combined_prompt,
1826
- "chat_history": history,
 
 
 
1827
  }
1828
 
1829
  # Generate AI output using the agent pipeline
1830
  agent_output = agent_executor.invoke(structured_input)
1831
-
1832
- # Generate AI output using agent_executor
1833
 
1834
- # Append the generated output to the response
1835
  response = f"**Selected Dataset: {selected_dataset_ai}**\n\n"
1836
  response += f"{top_scoring_statements}\n"
1837
- response += f"\n{agent_output['output']}"
1838
-
1839
-
1840
- # Generate Trust Tip and Suggestion
1841
- trust_tip, suggestion = get_trust_tip_and_suggestion()
1842
-
1843
- # Add Trust Tip and Suggestion if not already in history
1844
-
1845
-
1846
-
1847
- if not any(f"**Trust Tip**: {trust_tip}" in entry["bot"] and f"**Suggestion**: {suggestion}" in entry["bot"] for entry in history):
1848
- response += f"\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
1849
-
1850
- chat_history.append({"role": "user", "content": message})
1851
-
1852
- chat_history.append({"role": "assistant", "content": agent_output["output"]})
1853
-
1854
 
 
 
 
1855
 
1856
  return response
 
1857
  except Exception as e:
1858
  logger.error(f"Error generating chatbot response: {e}")
1859
- return "Error occurred during response generation"
1860
-
1861
-
1862
-
1863
 
1864
 
1865
  def read_ai_dataset_selection():
 
1754
 
1755
  last_top_scoring_statements = None
1756
  def chatbot_response(message, history):
1757
+ """
1758
+ Generate chatbot response dynamically using selected dataset, user input, and maintaining history.
1759
+ """
1760
+ global selected_dataset_ai, last_top_scoring_statements, chat_history
1761
+
1762
  try:
1763
+ # Ensure a dataset is selected
1764
+ if not selected_dataset_ai:
1765
+ return "Error: No dataset selected. Please select a dataset and try again."
1766
+
1767
+ # Define datasets and corresponding trust buckets
1768
  datasets = {
1769
  "VW Customers.xlsx": {
1770
  "Development": [
1771
  "We bring together the world's best talent in many disciplines to create your cars. (25%)",
1772
  "Building great and affordable cars is our foundation. (22%)",
1773
+ "Our beginnings are a unique combination of investors and unions. (18%)",
1774
  ],
1775
  "Benefit": [
1776
  "We bring together the world's best talent in many disciplines to create your cars. (23%)",
 
1786
  "Volkswagen Non Customers.xlsx": {
1787
  "Stability": [
1788
  "We work with our unions in our restructuring and future plans. (21%)",
1789
+ "We have learned from our mistakes in the Diesel Affair. (19%)",
1790
  "Building great and affordable cars is our foundation. (18%)",
1791
  ],
1792
  "Relationship": [
1793
  "We put a lot of emphasis on the interior experience and two of our cars have been ranked in the top 10. (24%)",
1794
  "We are at the forefront of technology to deliver better cars and driving experiences. (17%)",
1795
+ "Our beginnings are a unique combination of investors and unions. (17%)",
1796
  ],
1797
  "Competence": [
1798
  "At the heart of our decision-making is the long-term quality of life for all of us. (20%)",
 
1802
  },
1803
  }
1804
 
1805
+ # Retrieve relevant data for the selected dataset
1806
  if selected_dataset_ai in datasets:
1807
  trust_data = datasets[selected_dataset_ai]
1808
  else:
 
1815
  for statement in statements:
1816
  top_scoring_statements += f"- {statement}\n"
1817
  top_scoring_statements += "\n"
 
1818
  last_top_scoring_statements = top_scoring_statements
1819
+
1820
+ # Combine predefined prompt, top-scoring statements, and user input
1821
+ combined_prompt = predefined_prompt
1822
+ if "top scoring" in message.lower() or "trust builders" in message.lower():
1823
+ combined_prompt += "\n\n### Top-Scoring Statements for Integration ###\n" + top_scoring_statements
1824
+ combined_prompt += "\n\nUser Input:\n" + message
1825
+
1826
+ # Ensure input format for LangChain agent
 
1827
  structured_input = {
1828
+ "input": combined_prompt,
1829
+ "chat_history": [
1830
+ {"role": "user", "content": entry["content"]} if entry["role"] == "user" else
1831
+ {"role": "assistant", "content": entry["content"]} for entry in history
1832
+ ]
1833
  }
1834
 
1835
  # Generate AI output using the agent pipeline
1836
  agent_output = agent_executor.invoke(structured_input)
 
 
1837
 
1838
+ # Prepare the final response
1839
  response = f"**Selected Dataset: {selected_dataset_ai}**\n\n"
1840
  response += f"{top_scoring_statements}\n"
1841
+ response += f"### Generated Content ###\n\n{agent_output['output']}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1842
 
1843
+ # Append interaction to history
1844
+ history.append({"role": "user", "content": message})
1845
+ history.append({"role": "assistant", "content": agent_output["output"]})
1846
 
1847
  return response
1848
+
1849
  except Exception as e:
1850
  logger.error(f"Error generating chatbot response: {e}")
1851
+ return "Error occurred during response generation."
 
 
 
1852
 
1853
 
1854
  def read_ai_dataset_selection():