nmcamacho commited on
Commit
8e37637
Β·
verified Β·
1 Parent(s): f2d20ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -17
app.py CHANGED
@@ -7,14 +7,15 @@ import re
7
  import random
8
  from openai import OpenAI
9
 
10
- # Initialize OpenAI client securely (use Hugging Face secret)
11
  client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
12
 
13
  # Convert a CRM row into readable text for embeddings
14
  def row_to_text(row):
15
  return (
16
  f"Clinic: {row.get('Account_Name', 'N/A')}\n"
17
- f"Industry: {row.get('Industry', 'Dental')}\n"
 
18
  f"Region: {row.get('Region', 'Unknown')}\n"
19
  f"Account Manager: {row.get('Account_Manager', 'Unknown')}\n"
20
  f"Revenue: €{row.get('Revenue', 'N/A')}\n"
@@ -45,21 +46,29 @@ def contextual_pitch_assistant(csv_file, query, sender_name):
45
 
46
  q_emb = np.array([get_embedding(query)]).astype("float32")
47
  D, I = index.search(q_emb, 3)
48
- retrieved = [text_chunks[i] for i in I[0]]
49
-
50
- # Extract name and clinic safely first
51
- try:
52
- account_manager = retrieved[0].split("Account Manager: ")[1].split("\n")[0].strip()
53
- except Exception:
54
- account_manager = "team"
55
- try:
56
- account_name = retrieved[0].split("Clinic: ")[1].split("\n")[0].strip()
57
- except Exception:
58
- account_name = "your clinic"
59
 
60
  if not sender_name.strip():
61
  sender_name = "The Sales Team"
62
 
 
 
 
 
 
 
 
 
 
 
 
63
  prompt = f"""
64
  You are an expert in B2B email sales and marketing for dental technology solutions.
65
 
@@ -68,6 +77,8 @@ Write a short, natural-sounding HTML sales email for a dental clinic.
68
  Rules:
69
  - Always begin with "Dear {account_manager}," as the greeting.
70
  - Refer naturally to the clinic "{account_name}" in the body.
 
 
71
  - Focus on value, benefits, and a helpful tone β€” avoid technical jargon or metrics like churn or satisfaction scores.
72
  - Never include placeholders such as "Your Company Name" or "[Your Name]".
73
  - End the email with "Best regards," followed by "{sender_name}".
@@ -79,7 +90,7 @@ Query:
79
  {query}
80
 
81
  CRM context (for your understanding, do not copy verbatim):
82
- {'---'.join(retrieved)}
83
  """
84
 
85
  response = client.chat.completions.create(
@@ -113,7 +124,7 @@ CRM context (for your understanding, do not copy verbatim):
113
  """
114
  return html
115
 
116
- # Build the Gradio app UI
117
  with gr.Blocks(
118
  title="Contextual Pitch Assistant for Dental Sales",
119
  css="""
@@ -152,7 +163,7 @@ with gr.Blocks(
152
  )
153
 
154
  with gr.Row():
155
- csv_file = gr.File(label="πŸ“‚ Upload CRM CSV (5–100 rows)", file_types=[".csv"])
156
  sender_name = gr.Textbox(
157
  label="✍️ Who signs the email?",
158
  placeholder="e.g. Nuno Camacho, Sales Director",
@@ -178,4 +189,3 @@ with gr.Blocks(
178
  )
179
 
180
  app.launch()
181
-
 
7
  import random
8
  from openai import OpenAI
9
 
10
+ # Initialize OpenAI client securely
11
  client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
12
 
13
  # Convert a CRM row into readable text for embeddings
14
  def row_to_text(row):
15
  return (
16
  f"Clinic: {row.get('Account_Name', 'N/A')}\n"
17
+ f"Interaction Focus: {row.get('Recent_Interaction_Focus', 'N/A')}\n"
18
+ f"Interaction Notes: {row.get('Recent_Interaction_Notes', 'N/A')}\n"
19
  f"Region: {row.get('Region', 'Unknown')}\n"
20
  f"Account Manager: {row.get('Account_Manager', 'Unknown')}\n"
21
  f"Revenue: €{row.get('Revenue', 'N/A')}\n"
 
46
 
47
  q_emb = np.array([get_embedding(query)]).astype("float32")
48
  D, I = index.search(q_emb, 3)
49
+ retrieved = [df.iloc[i] for i in I[0]]
50
+
51
+ # Extract contextual fields
52
+ row = retrieved[0]
53
+ account_manager = row.get("Account_Manager", "team")
54
+ account_name = row.get("Account_Name", "your clinic")
55
+ focus = str(row.get("Recent_Interaction_Focus", "")).lower()
56
+ notes = str(row.get("Recent_Interaction_Notes", ""))
 
 
 
57
 
58
  if not sender_name.strip():
59
  sender_name = "The Sales Team"
60
 
61
+ # Map focus β†’ style instruction
62
+ if "business" in focus:
63
+ style_instruction = "Emphasize ROI, efficiency, and competitive advantage."
64
+ elif "patient" in focus:
65
+ style_instruction = "Emphasize patient comfort, satisfaction, and outcomes."
66
+ elif "clinical" in focus:
67
+ style_instruction = "Emphasize precision, innovation, and clinical quality."
68
+ else:
69
+ style_instruction = "Balance clinical, patient, and business value."
70
+
71
+ # Build prompt
72
  prompt = f"""
73
  You are an expert in B2B email sales and marketing for dental technology solutions.
74
 
 
77
  Rules:
78
  - Always begin with "Dear {account_manager}," as the greeting.
79
  - Refer naturally to the clinic "{account_name}" in the body.
80
+ - Adapt the pitch style: {style_instruction}
81
+ - Use this recent interaction note for inspiration: "{notes}"
82
  - Focus on value, benefits, and a helpful tone β€” avoid technical jargon or metrics like churn or satisfaction scores.
83
  - Never include placeholders such as "Your Company Name" or "[Your Name]".
84
  - End the email with "Best regards," followed by "{sender_name}".
 
90
  {query}
91
 
92
  CRM context (for your understanding, do not copy verbatim):
93
+ {row_to_text(row)}
94
  """
95
 
96
  response = client.chat.completions.create(
 
124
  """
125
  return html
126
 
127
+ # Build Gradio app (unchanged, apart from column updates)
128
  with gr.Blocks(
129
  title="Contextual Pitch Assistant for Dental Sales",
130
  css="""
 
163
  )
164
 
165
  with gr.Row():
166
+ csv_file = gr.File(label="πŸ“‚ Upload CRM CSV (with Recent_Interaction_Focus/Notes)", file_types=[".csv"])
167
  sender_name = gr.Textbox(
168
  label="✍️ Who signs the email?",
169
  placeholder="e.g. Nuno Camacho, Sales Director",
 
189
  )
190
 
191
  app.launch()