Wajahat698 commited on
Commit
a5d474d
·
verified ·
1 Parent(s): 48748c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -100
app.py CHANGED
@@ -1753,116 +1753,38 @@ def get_top_scoring_statements(dataset):
1753
  return top_statements
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%)",
1777
- "We strongly focus on keeping and nurturing our team and have a 99.5% retention rate. (18%)",
1778
- "Employees are provided with extensive continuous training. (16%)",
1779
- ],
1780
- "Vision": [
1781
- "Our brands are ranked No. 2 and 5 in the reliability rankings. (27%)",
1782
- "Our technology and manufacturing capabilities are second to none. (22%)",
1783
- "We produce almost 9 million cars per year. (15%)",
1784
- ],
1785
- },
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%)",
1799
- "We put a lot of emphasis on the interior experience and two of our cars have been ranked in the top 10. (19%)",
1800
- "We are one of the longest-established car companies. (18%)",
1801
- ],
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:
1809
- return f"Error: Invalid dataset selection '{selected_dataset_ai}'."
1810
-
1811
- # Prepare top-scoring statements
1812
- top_scoring_statements = "### Top Scoring Statements ###\n\n"
1813
- for bucket, statements in trust_data.items():
1814
- top_scoring_statements += f"**{bucket}**:\n"
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
- combined_prompt += "\n\n### Top-Scoring Statements for Integration ###\n" + top_scoring_statements
1823
- combined_prompt += "\n\nUser Input:\n" + message
1824
- trust_tip, suggestion = get_trust_tip_and_suggestion()
1825
- trust_tip_and_suggestion = f"\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
1826
-
1827
-
1828
- # Validate chat history
1829
- validated_chat_history = []
1830
- for entry in history:
1831
- if isinstance(entry, dict) and "role" in entry and "content" in entry:
1832
- validated_chat_history.append(entry)
1833
- else:
1834
- logger.warning(f"Invalid chat history entry skipped: {entry}")
1835
 
1836
- structured_input = {
1837
- "input": combined_prompt,
1838
- "chat_history": validated_chat_history,
1839
- }
1840
 
1841
- # Generate AI output using the agent pipeline
1842
- agent_output = agent_executor.invoke(structured_input)
1843
 
1844
- # Prepare the final response
1845
- response = f"**Selected Dataset: {selected_dataset_ai}**\n\n"
1846
- response += f"{top_scoring_statements}\n"
1847
- response += f"\n{agent_output['output']}"
1848
-
1849
- response+=trust_tip_and_suggestion
1850
 
1851
- # Append interaction to history
1852
- validated_chat_history.append({"role": "user", "content": message})
1853
- validated_chat_history.append({"role": "assistant", "content": agent_output["output"]})
 
 
 
 
1854
 
1855
  return response
1856
-
1857
- except KeyError as ke:
1858
- logger.error(f"KeyError encountered: {ke}")
1859
- return "An unexpected error occurred. Please try again."
1860
- except ValueError as ve:
1861
- logger.error(f"ValueError encountered: {ve}")
1862
- return "An unexpected value was encountered. Please refine your input."
1863
  except Exception as e:
1864
- logger.error(f"Unexpected error: {e}")
1865
- return "Error occurred during response generation."
 
1866
 
1867
  def read_ai_dataset_selection():
1868
  global selected_dataset_ai
 
1753
  return top_statements
1754
 
1755
  last_top_scoring_statements = None
1756
+
1757
  def chatbot_response(message, history):
 
 
 
 
1758
 
1759
+ global selected_dataset_ai
1760
+ global df_builder_pivot_str
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1761
 
1762
+ try:
1763
+ # Get the current value of selected_dataset_ai
1764
+ selected_dataset_ai = read_ai_dataset_selection()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1765
 
1766
+ # Recompute DATAFRAME_PROOF_POINT based on the selected dataset
1767
+ df_builder_pivot_str = compute_dataframe_proof_point()
 
 
1768
 
1769
+ # Generate response using the agent executor
1770
+ output = agent_executor.invoke({"input": message, "chat_history": chat_history})
1771
 
1772
+ # Prepend the selected dataset to the response
1773
+ response = f"**Selected Dataset: {selected_dataset_ai}**\n\n{output['output']}"
 
 
 
 
1774
 
1775
+ # Save the interaction context
1776
+ chat_history.extend(
1777
+ [
1778
+ HumanMessage(content=message),
1779
+ AIMessage(content=response),
1780
+ ]
1781
+ )
1782
 
1783
  return response
 
 
 
 
 
 
 
1784
  except Exception as e:
1785
+ logger.error(f"Error generating chatbot response: {e}")
1786
+ return "Error occurred during response generation"
1787
+
1788
 
1789
  def read_ai_dataset_selection():
1790
  global selected_dataset_ai