Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
import faiss
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# -------------------------------
|
| 9 |
+
# Load dataset
|
| 10 |
+
# -------------------------------
|
| 11 |
+
file_path = "marketing-campaigns.csv"
|
| 12 |
+
df = pd.read_csv(file_path)
|
| 13 |
+
|
| 14 |
+
# Combine text for embeddings
|
| 15 |
+
df = df.dropna(subset=["campaign_name", "description"])
|
| 16 |
+
df["text"] = df["campaign_name"] + ": " + df["description"]
|
| 17 |
+
|
| 18 |
+
# -------------------------------
|
| 19 |
+
# Embeddings + FAISS index
|
| 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)
|
| 30 |
+
|
| 31 |
+
# -------------------------------
|
| 32 |
+
# Load LLM (Phi-4-mini)
|
| 33 |
+
# -------------------------------
|
| 34 |
+
model_name = "microsoft/phi-4-mini"
|
| 35 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 36 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32, device_map="auto")
|
| 37 |
+
|
| 38 |
+
# -------------------------------
|
| 39 |
+
# RAG functions
|
| 40 |
+
# -------------------------------
|
| 41 |
+
def retrieve_context(query, k=3):
|
| 42 |
+
query_vec = embed_model.encode([query], convert_to_tensor=True).cpu().numpy()
|
| 43 |
+
D, I = index.search(query_vec, k)
|
| 44 |
+
results = [df.iloc[i]["text"] for i in I[0]]
|
| 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)
|
| 63 |
+
|
| 64 |
+
# -------------------------------
|
| 65 |
+
# Gradio UI
|
| 66 |
+
# -------------------------------
|
| 67 |
+
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 analysis query")
|
| 72 |
+
output = gr.Textbox(label="Generated Campaign")
|
| 73 |
+
btn = gr.Button("Generate with RAG")
|
| 74 |
+
|
| 75 |
+
btn.click(generate_with_rag, inputs=query, outputs=output)
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
demo.launch()
|