Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
import faiss
|
| 6 |
import torch
|
|
@@ -11,19 +11,23 @@ import torch
|
|
| 11 |
file_path = "marketing-campaigns.csv"
|
| 12 |
df = pd.read_csv(file_path)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# -------------------------------
|
| 19 |
-
# Embeddings + FAISS
|
| 20 |
# -------------------------------
|
| 21 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 22 |
-
|
| 23 |
embeddings = embed_model.encode(df["text"].tolist(), convert_to_tensor=True, show_progress_bar=True)
|
| 24 |
embeddings_np = embeddings.detach().cpu().numpy()
|
| 25 |
|
| 26 |
-
# Build FAISS index
|
| 27 |
d = embeddings_np.shape[1]
|
| 28 |
index = faiss.IndexFlatL2(d)
|
| 29 |
index.add(embeddings_np)
|
|
@@ -45,18 +49,15 @@ def retrieve_context(query, k=3):
|
|
| 45 |
return results
|
| 46 |
|
| 47 |
def generate_with_rag(prompt):
|
| 48 |
-
# Step 1: Retrieve top campaigns
|
| 49 |
context = retrieve_context(prompt, k=3)
|
| 50 |
context_str = "\n".join(context)
|
| 51 |
|
| 52 |
-
# Step 2: Build final prompt
|
| 53 |
rag_prompt = f"""
|
| 54 |
-
You are an AI marketing assistant.
|
| 55 |
Here are some past campaigns for reference:\n{context_str}\n
|
| 56 |
-
Based on these, generate a new campaign idea for: {prompt}
|
| 57 |
"""
|
| 58 |
|
| 59 |
-
# Step 3: Generate with Phi-4
|
| 60 |
inputs = tokenizer(rag_prompt, return_tensors="pt").to(model.device)
|
| 61 |
outputs = model.generate(**inputs, max_length=200, temperature=0.7, top_p=0.9)
|
| 62 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
@@ -68,7 +69,7 @@ with gr.Blocks() as demo:
|
|
| 68 |
gr.Markdown("## 🤖 RAG-powered AI Marketing Campaign Generator")
|
| 69 |
|
| 70 |
with gr.Row():
|
| 71 |
-
query = gr.Textbox(label="Enter campaign idea or
|
| 72 |
output = gr.Textbox(label="Generated Campaign")
|
| 73 |
btn = gr.Button("Generate with RAG")
|
| 74 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
import faiss
|
| 6 |
import torch
|
|
|
|
| 11 |
file_path = "marketing-campaigns.csv"
|
| 12 |
df = pd.read_csv(file_path)
|
| 13 |
|
| 14 |
+
# Flexible column handling
|
| 15 |
+
if "description" in df.columns:
|
| 16 |
+
df = df.dropna(subset=["campaign_name", "description"])
|
| 17 |
+
df["text"] = df["campaign_name"].astype(str) + ": " + df["description"].astype(str)
|
| 18 |
+
elif "campaign_name" in df.columns:
|
| 19 |
+
df = df.dropna(subset=["campaign_name"])
|
| 20 |
+
df["text"] = df["campaign_name"].astype(str)
|
| 21 |
+
else:
|
| 22 |
+
raise ValueError("CSV must contain at least a 'campaign_name' column")
|
| 23 |
|
| 24 |
# -------------------------------
|
| 25 |
+
# Embeddings + FAISS
|
| 26 |
# -------------------------------
|
| 27 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
|
|
| 28 |
embeddings = embed_model.encode(df["text"].tolist(), convert_to_tensor=True, show_progress_bar=True)
|
| 29 |
embeddings_np = embeddings.detach().cpu().numpy()
|
| 30 |
|
|
|
|
| 31 |
d = embeddings_np.shape[1]
|
| 32 |
index = faiss.IndexFlatL2(d)
|
| 33 |
index.add(embeddings_np)
|
|
|
|
| 49 |
return results
|
| 50 |
|
| 51 |
def generate_with_rag(prompt):
|
|
|
|
| 52 |
context = retrieve_context(prompt, k=3)
|
| 53 |
context_str = "\n".join(context)
|
| 54 |
|
|
|
|
| 55 |
rag_prompt = f"""
|
| 56 |
+
You are an AI marketing assistant.
|
| 57 |
Here are some past campaigns for reference:\n{context_str}\n
|
| 58 |
+
Based on these, generate a new creative campaign idea for: {prompt}
|
| 59 |
"""
|
| 60 |
|
|
|
|
| 61 |
inputs = tokenizer(rag_prompt, return_tensors="pt").to(model.device)
|
| 62 |
outputs = model.generate(**inputs, max_length=200, temperature=0.7, top_p=0.9)
|
| 63 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 69 |
gr.Markdown("## 🤖 RAG-powered AI Marketing Campaign Generator")
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
+
query = gr.Textbox(label="Enter campaign idea or keyword")
|
| 73 |
output = gr.Textbox(label="Generated Campaign")
|
| 74 |
btn = gr.Button("Generate with RAG")
|
| 75 |
|