Update app.py
Browse files
app.py
CHANGED
|
@@ -34,51 +34,53 @@ def get_embedding(text):
|
|
| 34 |
return response.data[0].embedding
|
| 35 |
|
| 36 |
# Core function: generate contextual pitch
|
| 37 |
-
def contextual_pitch_assistant(csv_file, query):
|
| 38 |
df = pd.read_csv(csv_file.name)
|
| 39 |
text_chunks = df.apply(row_to_text, axis=1).tolist()
|
| 40 |
embeddings = [get_embedding(t) for t in text_chunks]
|
| 41 |
|
| 42 |
dim = len(embeddings[0])
|
| 43 |
index = faiss.IndexFlatL2(dim)
|
| 44 |
-
index.add(np.array(embeddings).astype(
|
| 45 |
|
| 46 |
-
q_emb = np.array([get_embedding(query)]).astype(
|
| 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 |
-
|
| 56 |
try:
|
| 57 |
account_name = retrieved[0].split("Clinic: ")[1].split("\n")[0].strip()
|
| 58 |
except Exception:
|
| 59 |
account_name = "your clinic"
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
prompt = f"""
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
Query:
|
| 76 |
-
{query}
|
| 77 |
-
|
| 78 |
-
CRM context (for your understanding, do not copy verbatim):
|
| 79 |
-
{'---'.join(retrieved)}
|
| 80 |
-
"""
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
response = client.chat.completions.create(
|
| 84 |
model="gpt-4o-mini",
|
|
@@ -88,10 +90,6 @@ def contextual_pitch_assistant(csv_file, query):
|
|
| 88 |
|
| 89 |
output_text = response.choices[0].message.content
|
| 90 |
|
| 91 |
-
# Extract image prompt (for later)
|
| 92 |
-
match = re.search(r"(Image prompt|DALLΒ·E prompt)[:\-]\s*(.*)", output_text)
|
| 93 |
-
image_prompt = match.group(2).strip() if match else "modern dental clinic interior with dentist and patient"
|
| 94 |
-
|
| 95 |
# TEMP FIX: use random pre-uploaded header image
|
| 96 |
image_choices = [
|
| 97 |
"https://huggingface.co/spaces/nmcamacho/RAGdemo/resolve/main/dental_header_1.png",
|
|
@@ -107,9 +105,9 @@ def contextual_pitch_assistant(csv_file, query):
|
|
| 107 |
output_text = output_text.strip()
|
| 108 |
|
| 109 |
html = f"""
|
| 110 |
-
<div style='font-family:Arial,sans-serif;max-width:
|
| 111 |
-
border-radius:12px;box-shadow:0
|
| 112 |
-
<img src="{image_url}" style="width:100%;border-radius:8px;margin-bottom:
|
| 113 |
{output_text}
|
| 114 |
</div>
|
| 115 |
"""
|
|
@@ -118,28 +116,31 @@ def contextual_pitch_assistant(csv_file, query):
|
|
| 118 |
# Build the Gradio app UI
|
| 119 |
with gr.Blocks(
|
| 120 |
title="Contextual Pitch Assistant for Dental Sales",
|
| 121 |
-
css="
|
|
|
|
|
|
|
|
|
|
| 122 |
) as app:
|
| 123 |
gr.Markdown(
|
| 124 |
"""
|
| 125 |
-
# π¦· Contextual Pitch Assistant for Dental Sales
|
| 126 |
-
Upload a CRM file and ask a sales question β get a contextualized email pitch with a
|
| 127 |
"""
|
| 128 |
)
|
| 129 |
|
| 130 |
csv_file = gr.File(label="π Upload CRM CSV (5β100 rows)", file_types=[".csv"])
|
| 131 |
query = gr.Textbox(label="π¬ Sales Query", placeholder="e.g. Which clinic is best for our imaging subscription?")
|
| 132 |
-
|
|
|
|
| 133 |
output = gr.HTML(label="β¨ Email Pitch Preview", elem_id="output_html")
|
| 134 |
|
| 135 |
-
run_btn.click(fn=contextual_pitch_assistant, inputs=[csv_file, query], outputs=output)
|
| 136 |
|
| 137 |
run_btn.click(
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
|
| 145 |
app.launch()
|
|
|
|
| 34 |
return response.data[0].embedding
|
| 35 |
|
| 36 |
# Core function: generate contextual pitch
|
| 37 |
+
def contextual_pitch_assistant(csv_file, query, sender_name):
|
| 38 |
df = pd.read_csv(csv_file.name)
|
| 39 |
text_chunks = df.apply(row_to_text, axis=1).tolist()
|
| 40 |
embeddings = [get_embedding(t) for t in text_chunks]
|
| 41 |
|
| 42 |
dim = len(embeddings[0])
|
| 43 |
index = faiss.IndexFlatL2(dim)
|
| 44 |
+
index.add(np.array(embeddings).astype("float32"))
|
| 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 |
+
|
| 66 |
+
Write a short, natural-sounding HTML sales email for a dental clinic.
|
| 67 |
+
|
| 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}".
|
| 74 |
+
- Keep it under 150 words.
|
| 75 |
+
- Include a polite call to action at the end (e.g. suggesting a demo or short call).
|
| 76 |
+
- Return only HTML β no markdown, no code fences.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
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(
|
| 86 |
model="gpt-4o-mini",
|
|
|
|
| 90 |
|
| 91 |
output_text = response.choices[0].message.content
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# TEMP FIX: use random pre-uploaded header image
|
| 94 |
image_choices = [
|
| 95 |
"https://huggingface.co/spaces/nmcamacho/RAGdemo/resolve/main/dental_header_1.png",
|
|
|
|
| 105 |
output_text = output_text.strip()
|
| 106 |
|
| 107 |
html = f"""
|
| 108 |
+
<div style='font-family:Arial,sans-serif;max-width:700px;margin:auto;padding:24px;background:#ffffff;
|
| 109 |
+
border-radius:12px;box-shadow:0 3px 10px rgba(0,0,0,0.1);'>
|
| 110 |
+
<img src="{image_url}" style="width:100%;border-radius:8px;margin-bottom:20px;">
|
| 111 |
{output_text}
|
| 112 |
</div>
|
| 113 |
"""
|
|
|
|
| 116 |
# Build the Gradio app UI
|
| 117 |
with gr.Blocks(
|
| 118 |
title="Contextual Pitch Assistant for Dental Sales",
|
| 119 |
+
css="""
|
| 120 |
+
#output_html {min-height: 450px;}
|
| 121 |
+
.gradio-container {max-width: 90% !important; margin:auto;}
|
| 122 |
+
"""
|
| 123 |
) as app:
|
| 124 |
gr.Markdown(
|
| 125 |
"""
|
| 126 |
+
# π¦· Contextual Pitch Assistant for Dental Sales
|
| 127 |
+
Upload a CRM file and ask a sales question β get a contextualized email pitch with a matching image.
|
| 128 |
"""
|
| 129 |
)
|
| 130 |
|
| 131 |
csv_file = gr.File(label="π Upload CRM CSV (5β100 rows)", file_types=[".csv"])
|
| 132 |
query = gr.Textbox(label="π¬ Sales Query", placeholder="e.g. Which clinic is best for our imaging subscription?")
|
| 133 |
+
sender_name = gr.Textbox(label="βοΈ Who signs the email?", placeholder="e.g. Nuno Camacho, Sales Director", value="Nuno Camacho")
|
| 134 |
+
run_btn = gr.Button("π Generate Pitch", variant="primary")
|
| 135 |
output = gr.HTML(label="β¨ Email Pitch Preview", elem_id="output_html")
|
| 136 |
|
| 137 |
+
run_btn.click(fn=contextual_pitch_assistant, inputs=[csv_file, query, sender_name], outputs=output)
|
| 138 |
|
| 139 |
run_btn.click(
|
| 140 |
+
lambda: "<p style='color:gray;'>β³ Processing... please wait 30β60 seconds.</p>",
|
| 141 |
+
inputs=None,
|
| 142 |
+
outputs=output,
|
| 143 |
+
queue=False
|
| 144 |
+
)
|
|
|
|
| 145 |
|
| 146 |
app.launch()
|