Wajahat698 commited on
Commit
de2bf69
·
verified ·
1 Parent(s): afce2a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -6
app.py CHANGED
@@ -22,6 +22,8 @@ from datetime import datetime
22
  import pandas as pd
23
  import re
24
  import random
 
 
25
  # Initialize logging and load environment variables
26
  logging.basicConfig(level=logging.INFO)
27
  logger = logging.getLogger(__name__)
@@ -242,7 +244,18 @@ def search_knowledge_base(query):
242
  except Exception as e:
243
  logger.error(f"Error searching knowledge base: {e}")
244
  return ["Error occurred during knowledge base search"]
245
-
 
 
 
 
 
 
 
 
 
 
 
246
  def google_search(query):
247
  try:
248
  search_client = serpapi.Client(api_key=serper_api_key)
@@ -291,7 +304,11 @@ prompt_message = f"""
291
  Strictly Do not mention"Trust" , "Stability" , "Development" , "Relationship" , "Competence" , "benefit" , "vision" words in your response .
292
  Do not generate imaginary/Creative content . It should not look like creative story .
293
  When asking for trust builders it has tibe returned short/concised facts as dot points.
294
- text with dollar figures needs to be concised and readable with spacing .
 
 
 
 
295
 
296
 
297
 
@@ -518,16 +535,18 @@ if prompt :
518
  try:
519
  # Generate response using the agent executor
520
  output = agent_executor.invoke({
521
- "input": f" {prompt} . Always INCLUDE & Be specific with numbers, dates, people, and [$ dollar amounts] (include them).* Search and provide sources for all the results. Respond directly to the query without any introductory phrases or meta-commentary. Your response should be natural and read as if it's addressing the query immediately, without any preamble. Use creative techniques like metaphors, analogies, and juxtaposition to enhance the content. Interconnect ideas smoothly to create meaning for the reader.Always write in nicely flowing text and create an active language headline. Avoid using colons in the sub-headings and incorporate a creative technique in every headline and sub-header. Interweave the examples contextually to create meaning for donors and beneficiaries. When asked about to write annual report ONLY THEN write report in 2-3 pargagraphs(with no sub-headings) and end then : do mention these 3 in sub headings with example related to the report: -example proof points , -Heuristics used , -creative techniques used. DO NOT USE COLONS in the sub-headers Otherwise ignore it . DONOT WRITE CONCLUSION in the end related to given prompt. remove all tags present with your response so response format is great",
522
  "chat_history": st.session_state.chat_history
523
  })
524
  full_response = output["output"]
525
 
 
526
 
527
  # Display the response
528
- trust_tip = random.choice(trust_tips)
529
- suggestion = random.choice(suggestions)
530
- full_response += f"\n\n**Trust Tip**: {trust_tip} \n \n **Suggestion**: {suggestion}"
 
531
 
532
  st.write(full_response, unsafe_allow_html=True)
533
 
 
22
  import pandas as pd
23
  import re
24
  import random
25
+ from bs4 import BeautifulSoup
26
+
27
  # Initialize logging and load environment variables
28
  logging.basicConfig(level=logging.INFO)
29
  logger = logging.getLogger(__name__)
 
244
  except Exception as e:
245
  logger.error(f"Error searching knowledge base: {e}")
246
  return ["Error occurred during knowledge base search"]
247
+ def sanitize_and_normalize(text):
248
+ # Use BeautifulSoup to parse and clean the HTML
249
+ soup = BeautifulSoup(text, 'html.parser')
250
+
251
+ # Remove unwanted tags or attributes if necessary
252
+ for tag in soup.find_all(['i', 'em']): # Example: removing <i> and <em> tags
253
+ tag.unwrap()
254
+
255
+ # Normalize the text by ensuring proper spacing
256
+ normalized_text = ' '.join(soup.get_text().split())
257
+ return normalized_text
258
+
259
  def google_search(query):
260
  try:
261
  search_client = serpapi.Client(api_key=serper_api_key)
 
304
  Strictly Do not mention"Trust" , "Stability" , "Development" , "Relationship" , "Competence" , "benefit" , "vision" words in your response .
305
  Do not generate imaginary/Creative content . It should not look like creative story .
306
  When asking for trust builders it has tibe returned short/concised facts as dot points.
307
+ text with dollar figures needs to be concised and readable with spacing.
308
+
309
+
310
+
311
+
312
 
313
 
314
 
 
535
  try:
536
  # Generate response using the agent executor
537
  output = agent_executor.invoke({
538
+ "input": f" {prompt} . Always INCLUDE & Be specific with numbers, dates, people, and [$ dollar amounts] (include them).* Search and provide sources for all the results. Respond directly to the query without any introductory phrases or meta-commentary. Your response should be natural and read as if it's addressing the query immediately, without any preamble. Use creative techniques like metaphors, analogies, and juxtaposition to enhance the content. Interconnect ideas smoothly to create meaning for the reader.Always write in nicely flowing text and create an active language headline. Avoid using colons in the sub-headings and incorporate a creative technique in every headline and sub-header. Interweave the examples contextually to create meaning for donors and beneficiaries. When asked about to write annual report ONLY THEN write report in 2-3 pargagraphs(with no sub-headings) and end then : do mention these 3 in sub headings with example related to the report: -example proof points , -Heuristics used , -creative techniques used (DO-NOT MENTION THIS WHEN ASKED TO WRITE EMAIL or news letter). When asked about email news letter or news letter it should be in proper email format . DO NOT USE COLONS in the sub-headers Otherwise ignore it . DONOT WRITE CONCLUSION in the end related to given prompt. remove all tags present with your response so response format is great",
539
  "chat_history": st.session_state.chat_history
540
  })
541
  full_response = output["output"]
542
 
543
+ full_response = sanitize_and_normalize(full_response)
544
 
545
  # Display the response
546
+ if "**Trust Tip**" not in full_response and "**Suggestion**" not in full_response:
547
+ trust_tip = random.choice(trust_tips)
548
+ suggestion = random.choice(suggestions)
549
+ full_response += f"\n\n**Trust Tip**: {trust_tip} \n\n **Suggestion**: {suggestion}"
550
 
551
  st.write(full_response, unsafe_allow_html=True)
552