Wajahat698 commited on
Commit
09d71e4
·
verified ·
1 Parent(s): a34ddb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -12
app.py CHANGED
@@ -1773,6 +1773,8 @@ def get_top_scoring_statements(dataset):
1773
  return top_statements
1774
 
1775
  last_top_scoring_statements = None
 
 
1776
  def chatbot_response(message, history):
1777
  """
1778
  Generate chatbot response dynamically using selected dataset, user input, and maintaining history.
@@ -1828,17 +1830,20 @@ def chatbot_response(message, history):
1828
  else:
1829
  return f"Error: Invalid dataset selection '{selected_dataset_ai}'."
1830
 
1831
- # Prepare top-scoring statements
1832
- top_scoring_statements = "### Top Scoring Statements ###\n\n"
1833
- for bucket, statements in trust_data.items():
1834
- top_scoring_statements += f"**{bucket}**:\n"
1835
- for statement in statements:
1836
- top_scoring_statements += f"- {statement}\n"
1837
- top_scoring_statements += "\n"
1838
- last_top_scoring_statements = top_scoring_statements
1839
-
1840
- # Combine predefined prompt, top-scoring statements, and user input
1841
- combined_prompt = "\n\n### Top-Scoring Statements for Integration ###\n" + top_scoring_statements
 
 
 
1842
  combined_prompt += "\n\nUser Input:\n" + message
1843
  trust_tip, suggestion = get_trust_tip_and_suggestion()
1844
  trust_tip_and_suggestion = f"\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
@@ -1866,7 +1871,9 @@ def chatbot_response(message, history):
1866
 
1867
  # Prepare the final response
1868
  response = f"**Selected Dataset: {selected_dataset_ai}**\n\n"
1869
- response += f"{top_scoring_statements}\n"
 
 
1870
  response += f"\n{agent_output['output']}"
1871
  response += trust_tip_and_suggestion
1872
 
@@ -1886,6 +1893,119 @@ def chatbot_response(message, history):
1886
  logger.error(f"Unexpected error: {e}")
1887
  return "Error occurred during response generation."
1888
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1889
  def read_ai_dataset_selection():
1890
  global selected_dataset_ai
1891
  return selected_dataset_ai
 
1773
  return top_statements
1774
 
1775
  last_top_scoring_statements = None
1776
+
1777
+
1778
  def chatbot_response(message, history):
1779
  """
1780
  Generate chatbot response dynamically using selected dataset, user input, and maintaining history.
 
1830
  else:
1831
  return f"Error: Invalid dataset selection '{selected_dataset_ai}'."
1832
 
1833
+ # If top-scoring statements have not been generated yet, generate them
1834
+ if not last_top_scoring_statements:
1835
+ top_scoring_statements = "### Top Scoring Statements ###\n\n"
1836
+ for bucket, statements in trust_data.items():
1837
+ top_scoring_statements += f"**{bucket}**:\n"
1838
+ for statement in statements:
1839
+ top_scoring_statements += f"- {statement}\n"
1840
+ top_scoring_statements += "\n"
1841
+ last_top_scoring_statements = top_scoring_statements # Save this for future use
1842
+ else:
1843
+ top_scoring_statements = "" # Don't include the top-scoring statements in subsequent messages
1844
+
1845
+ # Combine predefined prompt, top-scoring statements (only the first time), and user input
1846
+ combined_prompt = top_scoring_statements # Include top-scoring statements only once
1847
  combined_prompt += "\n\nUser Input:\n" + message
1848
  trust_tip, suggestion = get_trust_tip_and_suggestion()
1849
  trust_tip_and_suggestion = f"\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
 
1871
 
1872
  # Prepare the final response
1873
  response = f"**Selected Dataset: {selected_dataset_ai}**\n\n"
1874
+ # Only include top-scoring statements once
1875
+ if top_scoring_statements:
1876
+ response += f"{top_scoring_statements}\n"
1877
  response += f"\n{agent_output['output']}"
1878
  response += trust_tip_and_suggestion
1879
 
 
1893
  logger.error(f"Unexpected error: {e}")
1894
  return "Error occurred during response generation."
1895
 
1896
+ # def chatbot_response(message, history):
1897
+ # """
1898
+ # Generate chatbot response dynamically using selected dataset, user input, and maintaining history.
1899
+ # """
1900
+ # global selected_dataset_ai, last_top_scoring_statements, chat_history
1901
+
1902
+ # try:
1903
+ # # Ensure a dataset is selected
1904
+ # if not selected_dataset_ai:
1905
+ # return "Error: No dataset selected. Please select a dataset and try again."
1906
+
1907
+ # # Define datasets and corresponding trust buckets
1908
+ # datasets = {
1909
+ # "VW Owners.xlsx": {
1910
+ # "Development": [
1911
+ # "We bring together the world's best talent in many disciplines to create your cars. (25%)",
1912
+ # "Building great and affordable cars is our foundation. (22%)",
1913
+ # "Our beginnings are a unique combination of investors and unions. (18%)",
1914
+ # ],
1915
+ # "Benefit": [
1916
+ # "We bring together the world's best talent in many disciplines to create your cars. (23%)",
1917
+ # "We strongly focus on keeping and nurturing our team and have a 99.5% retention rate. (18%)",
1918
+ # "Employees are provided with extensive continuous training. (16%)",
1919
+ # ],
1920
+ # "Vision": [
1921
+ # "Our brands are ranked No. 2 and 5 in the reliability rankings. (27%)",
1922
+ # "Our technology and manufacturing capabilities are second to none. (22%)",
1923
+ # "We produce almost 9 million cars per year. (15%)",
1924
+ # ],
1925
+ # },
1926
+ # "Volkswagen Non Customers.xlsx": {
1927
+ # "Stability": [
1928
+ # "We work with our unions in our restructuring and future plans. (21%)",
1929
+ # "We have learned from our mistakes in the Diesel Affair and we have made fundamental changes. (19%)",
1930
+ # "Building great and affordable cars is our foundation. (18%)",
1931
+ # ],
1932
+ # "Relationship": [
1933
+ # "We put a lot of emphasis on the interior experience and two of our cars have been ranked in the top 10. (24%)",
1934
+ # "We are at the forefront of technology to deliver better cars and driving experiences. (17%)",
1935
+ # "Our beginnings are a unique combination of investors and unions and today 9 of our 20 board members are staff representatives. (17%)",
1936
+ # ],
1937
+ # "Competence": [
1938
+ # "At the heart of our decision-making is the long-term quality of life for all of us. (20%)",
1939
+ # "We put a lot of emphasis on the interior experience and two of our cars have been ranked in the top 10. (19%)",
1940
+ # "We are one of the longest-established car companies. (18%)",
1941
+ # ],
1942
+ # },
1943
+ # }
1944
+
1945
+ # # Retrieve relevant data for the selected dataset
1946
+ # if selected_dataset_ai in datasets:
1947
+ # trust_data = datasets[selected_dataset_ai]
1948
+ # else:
1949
+ # return f"Error: Invalid dataset selection '{selected_dataset_ai}'."
1950
+
1951
+ # # Prepare top-scoring statements
1952
+ # top_scoring_statements = "### Top Scoring Statements ###\n\n"
1953
+ # for bucket, statements in trust_data.items():
1954
+ # top_scoring_statements += f"**{bucket}**:\n"
1955
+ # for statement in statements:
1956
+ # top_scoring_statements += f"- {statement}\n"
1957
+ # top_scoring_statements += "\n"
1958
+ # last_top_scoring_statements = top_scoring_statements
1959
+
1960
+ # # Combine predefined prompt, top-scoring statements, and user input
1961
+ # combined_prompt = "\n\n### Top-Scoring Statements for Integration ###\n" + top_scoring_statements
1962
+ # combined_prompt += "\n\nUser Input:\n" + message
1963
+ # trust_tip, suggestion = get_trust_tip_and_suggestion()
1964
+ # trust_tip_and_suggestion = f"\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
1965
+
1966
+ # # Validate chat history
1967
+ # validated_chat_history = []
1968
+ # for entry in history:
1969
+ # if isinstance(entry, dict) and "role" in entry and "content" in entry:
1970
+ # validated_chat_history.append(entry)
1971
+ # else:
1972
+ # logger.warning(f"Invalid chat history entry skipped: {entry}")
1973
+
1974
+ # # Include validated history in the prompt
1975
+ # for entry in validated_chat_history:
1976
+ # combined_prompt += f"\n{entry['role']}: {entry['content']}"
1977
+
1978
+ # # Structured input for agent execution
1979
+ # structured_input = {
1980
+ # "input": combined_prompt,
1981
+ # "chat_history": validated_chat_history,
1982
+ # }
1983
+
1984
+ # # Generate AI output using the agent pipeline (replace with actual logic)
1985
+ # agent_output = agent_executor.invoke(structured_input)
1986
+
1987
+ # # Prepare the final response
1988
+ # response = f"**Selected Dataset: {selected_dataset_ai}**\n\n"
1989
+ # response += f"{top_scoring_statements}\n"
1990
+ # response += f"\n{agent_output['output']}"
1991
+ # response += trust_tip_and_suggestion
1992
+
1993
+ # # Append interaction to history
1994
+ # validated_chat_history.append({"role": "user", "content": message})
1995
+ # validated_chat_history.append({"role": "assistant", "content": agent_output["output"]})
1996
+
1997
+ # return response
1998
+
1999
+ # except KeyError as ke:
2000
+ # logger.error(f"KeyError encountered: {ke}")
2001
+ # return "An unexpected error occurred. Please try again."
2002
+ # except ValueError as ve:
2003
+ # logger.error(f"ValueError encountered: {ve}")
2004
+ # return "An unexpected value was encountered. Please refine your input."
2005
+ # except Exception as e:
2006
+ # logger.error(f"Unexpected error: {e}")
2007
+ # return "Error occurred during response generation."
2008
+
2009
  def read_ai_dataset_selection():
2010
  global selected_dataset_ai
2011
  return selected_dataset_ai