File size: 6,695 Bytes
54f05b5
 
 
 
964e64f
 
54f05b5
 
 
 
964e64f
54f05b5
964e64f
 
54f05b5
 
637d90b
54f05b5
 
 
 
 
 
 
 
 
 
964e64f
54f05b5
964e64f
44cdca6
 
964e64f
 
 
 
 
 
 
 
 
54f05b5
 
964e64f
54f05b5
 
 
964e64f
54f05b5
 
 
964e64f
 
 
 
 
 
54f05b5
964e64f
 
709c9f6
 
964e64f
709c9f6
54f05b5
 
964e64f
54f05b5
709c9f6
 
964e64f
 
 
709c9f6
54f05b5
 
964e64f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
709c9f6
54f05b5
 
 
964e64f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54f05b5
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import gradio as gr
import pandas as pd
import faiss
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from sentence_transformers import SentenceTransformer

# -------------------------------
# Load dataset
# -------------------------------
file_path = "marketing-campaigns.csv"  # file uploaded in Hugging Face space
df = pd.read_csv(file_path)
df = df.dropna()  # drop completely empty rows
df["text"] = df.astype(str).agg(" | ".join, axis=1)  # merge all cols into text

# -------------------------------
# Embeddings + FAISS
# -------------------------------
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = embed_model.encode(df["text"].tolist(), convert_to_tensor=True, show_progress_bar=True)
embeddings_np = embeddings.detach().cpu().numpy()

d = embeddings_np.shape[1]
index = faiss.IndexFlatL2(d)
index.add(embeddings_np)

# -------------------------------
# Load LLM (default Phi-4-mini)
# -------------------------------
model_choices = {
    "openai/gpt-oss-120b": "microsoft/phi-2",
    "openai/gpt-oss-20b": "microsoft/phi-2"  # placeholder, can map to another HF model
}

current_model_id = "openai/gpt-oss-120b"
hf_model = model_choices[current_model_id]

tokenizer = AutoTokenizer.from_pretrained(hf_model)
model = AutoModelForCausalLM.from_pretrained(
    hf_model, torch_dtype=torch.float32, device_map="auto"
)

# -------------------------------
# RAG Functions
# -------------------------------
def retrieve_context(query, k=3):
    query_vec = embed_model.encode([query], convert_to_tensor=True).cpu().numpy()
    D, I = index.search(query_vec, k)
    results = [df.iloc[i]["text"] for i in I[0]]
    return results

def generate_with_rag(prompt, temperature=0.8, max_tokens=250):
    # Step 1: Retrieve context
    context = retrieve_context(prompt, k=3)
    context_str = "\n".join(context)

    # Step 2: Construct grounded prompt
    rag_prompt = f"""
    You are a creative AI campaign assistant.
    Use the following supporting data to ground your answer:
    {context_str}

    Task: Generate a unique and creative marketing campaign idea for:
    {prompt}
    """

    # Step 3: Generate
    inputs = tokenizer(rag_prompt, return_tensors="pt").to(model.device)
    outputs = model.generate(
        **inputs,
        max_length=max_tokens,
        temperature=temperature,
        top_p=0.9
    )
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

def switch_model(model_choice):
    """Switch between available models."""
    global model, tokenizer, current_model_id
    hf_model = model_choices.get(model_choice, "microsoft/phi-4-mini")
    tokenizer = AutoTokenizer.from_pretrained(hf_model)
    model = AutoModelForCausalLM.from_pretrained(
        hf_model, torch_dtype=torch.float32, device_map="auto"
    )
    current_model_id = model_choice
    return gr.update(visible=(model_choice=="openai/gpt-oss-120b")), gr.update(visible=(model_choice=="openai/gpt-oss-20b")), model_choice

# -------------------------------
# Custom CSS
# -------------------------------
custom_css = """
.gradio-container {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 25%, #f093fb 50%, #4facfe 75%, #00f2fe 100%);
    background-size: 400% 400%;
    animation: gradient-animation 15s ease infinite;
    min-height: 100vh;
}
@keyframes gradient-animation {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}
.main-container {
    background-color: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    padding: 20px;
    box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
    border: 1px solid rgba(255, 255, 255, 0.18);
    margin: 10px;
}
"""

# -------------------------------
# Gradio UI
# -------------------------------
with gr.Blocks(fill_height=True, theme=gr.themes.Soft, css=custom_css) as demo:
    # Top badges
    with gr.Row(elem_classes="badge-container"):
        gr.HTML("""
            <div style="display: flex; justify-content: center; align-items: center; gap: 15px;">
                <a href="https://huggingface.co/spaces/VIDraft/gpt-oss-RAG" target="_blank">
                    <img src="https://img.shields.io/static/v1?label=gpt-oss-20b&message=RAG&color=%23000080&labelColor=%23ffa500&logo=huggingface&logoColor=white&style=for-the-badge" alt="badge">
                </a>
                <a href="https://discord.gg/openfreeai" target="_blank">
                    <img src="https://img.shields.io/static/v1?label=Discord&message=Openfree%20AI&color=%23000080&labelColor=%23ffa500&logo=discord&logoColor=white&style=for-the-badge" alt="badge">
                </a>
            </div>
        """)

    with gr.Row():
        # Sidebar
        with gr.Column(scale=1):
            with gr.Group(elem_classes="main-container"):
                gr.Markdown("# πŸš€ Inference Provider")
                model_dropdown = gr.Dropdown(
                    choices=list(model_choices.keys()),
                    value=current_model_id,
                    label="πŸ“Š Select Model"
                )
                reload_btn = gr.Button("πŸ”„ Apply Model Change", variant="primary")

                with gr.Accordion("βš™οΈ Advanced Options", open=False):
                    temperature = gr.Slider(
                        minimum=0, maximum=2, value=0.8, step=0.1, label="Temperature"
                    )
                    max_tokens = gr.Slider(
                        minimum=50, maximum=1024, value=250, step=10, label="Max Tokens"
                    )

        # Main chat area
        with gr.Column(scale=3):
            with gr.Group(elem_classes="main-container"):
                gr.Markdown("## πŸ’¬ RAG-powered Creative Campaign Assistant")

                query = gr.Textbox(label="Enter campaign idea / theme", lines=2)
                output = gr.Textbox(label="Generated Campaign Script", lines=10)

                btn = gr.Button("✨ Generate with RAG")

                btn.click(
                    generate_with_rag,
                    inputs=[query, temperature, max_tokens],
                    outputs=output
                )

                with gr.Column(visible=True) as model_120b_container:
                    gr.Markdown("### Active Model: openai/gpt-oss-120b")
                with gr.Column(visible=False) as model_20b_container:
                    gr.Markdown("### Active Model: openai/gpt-oss-20b")

    reload_btn.click(
        fn=switch_model,
        inputs=[model_dropdown],
        outputs=[model_120b_container, model_20b_container, gr.State(current_model_id)]
    )

if __name__ == "__main__":
    demo.launch()