Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,15 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import faiss
|
| 5 |
-
|
| 6 |
import os
|
|
|
|
|
|
|
| 7 |
from openai import OpenAI
|
|
|
|
|
|
|
| 8 |
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
| 9 |
|
|
|
|
| 10 |
def row_to_text(row):
|
| 11 |
return (
|
| 12 |
f"Clinic: {row.get('Account_Name', 'N/A')}\n"
|
|
@@ -21,7 +25,7 @@ def row_to_text(row):
|
|
| 21 |
f"Notes: {row.get('Account_Notes', 'N/A')}"
|
| 22 |
)
|
| 23 |
|
| 24 |
-
|
| 25 |
def get_embedding(text):
|
| 26 |
response = client.embeddings.create(
|
| 27 |
input=[text],
|
|
@@ -29,6 +33,7 @@ def get_embedding(text):
|
|
| 29 |
)
|
| 30 |
return response.data[0].embedding
|
| 31 |
|
|
|
|
| 32 |
def contextual_pitch_assistant(csv_file, query):
|
| 33 |
df = pd.read_csv(csv_file.name)
|
| 34 |
text_chunks = df.apply(row_to_text, axis=1).tolist()
|
|
@@ -47,10 +52,8 @@ def contextual_pitch_assistant(csv_file, query):
|
|
| 47 |
Based on the following CRM insights and query, generate:
|
| 48 |
1) A short HTML email pitch (subject + body) ready to send.
|
| 49 |
2) A DALL·E 3 prompt for a matching header image.
|
| 50 |
-
|
| 51 |
Query:
|
| 52 |
{query}
|
| 53 |
-
|
| 54 |
CRM data:
|
| 55 |
{'---'.join(retrieved)}
|
| 56 |
"""
|
|
@@ -63,22 +66,11 @@ def contextual_pitch_assistant(csv_file, query):
|
|
| 63 |
|
| 64 |
output_text = response.choices[0].message.content
|
| 65 |
|
| 66 |
-
|
| 67 |
match = re.search(r"(Image prompt|DALL·E prompt)[:\-]\s*(.*)", output_text)
|
| 68 |
image_prompt = match.group(2).strip() if match else "modern dental clinic interior with dentist and patient"
|
| 69 |
-
|
| 70 |
-
#
|
| 71 |
-
# model="gpt-image-1",
|
| 72 |
-
# prompt=image_prompt,
|
| 73 |
-
# size="1024x1024"
|
| 74 |
-
#)
|
| 75 |
-
#image_url = image.data[0].url
|
| 76 |
-
|
| 77 |
-
# TEMP FIX — skip image generation until org verification is complete
|
| 78 |
-
import random
|
| 79 |
-
|
| 80 |
-
# Generate or pick a random header image
|
| 81 |
-
import random
|
| 82 |
image_choices = [
|
| 83 |
"https://huggingface.co/spaces/nmcamacho/RAGdemo/resolve/main/dental_header_1.png",
|
| 84 |
"https://huggingface.co/spaces/nmcamacho/RAGdemo/resolve/main/dental_header_2.png",
|
|
@@ -86,11 +78,11 @@ def contextual_pitch_assistant(csv_file, query):
|
|
| 86 |
]
|
| 87 |
image_url = random.choice(image_choices)
|
| 88 |
|
| 89 |
-
# Clean
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
|
| 95 |
html = f"""
|
| 96 |
<div style='font-family:Arial,sans-serif;max-width:600px;margin:auto;padding:16px;background:#ffffff;
|
|
@@ -101,6 +93,7 @@ def contextual_pitch_assistant(csv_file, query):
|
|
| 101 |
"""
|
| 102 |
return html
|
| 103 |
|
|
|
|
| 104 |
with gr.Blocks(title="Contextual Pitch Assistant for Dental Sales") as app:
|
| 105 |
gr.Markdown(
|
| 106 |
"""
|
|
@@ -113,11 +106,7 @@ with gr.Blocks(title="Contextual Pitch Assistant for Dental Sales") as app:
|
|
| 113 |
query = gr.Textbox(label="💬 Sales Query", placeholder="e.g. Which clinic is best for our imaging subscription?")
|
| 114 |
run_btn = gr.Button("🚀 Generate Pitch")
|
| 115 |
output = gr.HTML(label="✨ Email Pitch Preview", elem_id="output_html")
|
| 116 |
-
|
| 117 |
-
run_btn.click(fn=contextual_pitch_assistant, inputs=[csv_file, query], outputs=output)
|
| 118 |
-
|
| 119 |
|
| 120 |
run_btn.click(fn=contextual_pitch_assistant, inputs=[csv_file, query], outputs=output)
|
| 121 |
|
| 122 |
-
|
| 123 |
app.launch()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import faiss
|
|
|
|
| 5 |
import os
|
| 6 |
+
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"
|
|
|
|
| 25 |
f"Notes: {row.get('Account_Notes', 'N/A')}"
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Get embedding for a given text
|
| 29 |
def get_embedding(text):
|
| 30 |
response = client.embeddings.create(
|
| 31 |
input=[text],
|
|
|
|
| 33 |
)
|
| 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()
|
|
|
|
| 52 |
Based on the following CRM insights and query, generate:
|
| 53 |
1) A short HTML email pitch (subject + body) ready to send.
|
| 54 |
2) A DALL·E 3 prompt for a matching header image.
|
|
|
|
| 55 |
Query:
|
| 56 |
{query}
|
|
|
|
| 57 |
CRM data:
|
| 58 |
{'---'.join(retrieved)}
|
| 59 |
"""
|
|
|
|
| 66 |
|
| 67 |
output_text = response.choices[0].message.content
|
| 68 |
|
| 69 |
+
# Extract image prompt (for later)
|
| 70 |
match = re.search(r"(Image prompt|DALL·E prompt)[:\-]\s*(.*)", output_text)
|
| 71 |
image_prompt = match.group(2).strip() if match else "modern dental clinic interior with dentist and patient"
|
| 72 |
+
|
| 73 |
+
# TEMP FIX: use random pre-uploaded header image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
image_choices = [
|
| 75 |
"https://huggingface.co/spaces/nmcamacho/RAGdemo/resolve/main/dental_header_1.png",
|
| 76 |
"https://huggingface.co/spaces/nmcamacho/RAGdemo/resolve/main/dental_header_2.png",
|
|
|
|
| 78 |
]
|
| 79 |
image_url = random.choice(image_choices)
|
| 80 |
|
| 81 |
+
# Clean output text
|
| 82 |
+
output_text = re.sub(r"(?is)dall[-·]e.*?```", "", output_text)
|
| 83 |
+
output_text = re.sub(r"###.*?HTML Email Pitch", "", output_text)
|
| 84 |
+
output_text = re.sub(r"```html|```", "", output_text)
|
| 85 |
+
output_text = output_text.strip()
|
| 86 |
|
| 87 |
html = f"""
|
| 88 |
<div style='font-family:Arial,sans-serif;max-width:600px;margin:auto;padding:16px;background:#ffffff;
|
|
|
|
| 93 |
"""
|
| 94 |
return html
|
| 95 |
|
| 96 |
+
# Build the Gradio app UI
|
| 97 |
with gr.Blocks(title="Contextual Pitch Assistant for Dental Sales") as app:
|
| 98 |
gr.Markdown(
|
| 99 |
"""
|
|
|
|
| 106 |
query = gr.Textbox(label="💬 Sales Query", placeholder="e.g. Which clinic is best for our imaging subscription?")
|
| 107 |
run_btn = gr.Button("🚀 Generate Pitch")
|
| 108 |
output = gr.HTML(label="✨ Email Pitch Preview", elem_id="output_html")
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
run_btn.click(fn=contextual_pitch_assistant, inputs=[csv_file, query], outputs=output)
|
| 111 |
|
|
|
|
| 112 |
app.launch()
|