sloganAI / app.py
3v324v23's picture
Deploy app with recommendation + slogan generator
da49eac
raw
history blame
4.24 kB
import gradio as gr
import pandas as pd
import numpy as np
from sentence_transformers import SentenceTransformer
import faiss
# === Load embedding model ===
embed_model = SentenceTransformer("sentence-transformers/all-mpnet-base-v2")
# Dummy dataset (for demo) – replace with your full startup dataset
data = pd.DataFrame({
"name": ["HowDidIDo", "Museotainment", "Movitr"],
"tagline": ["Online evaluation platform", "PacMan & Louvre meet", "Crowdsourced video translation"],
"description": [
"Public speaking, Presentation skills and interview practice",
"Interactive AR museum tours",
"Video translation with voice and subtitles"
]
})
# Build FAISS index
data_vecs = embed_model.encode(data["description"].tolist())
faiss.normalize_L2(data_vecs)
index = faiss.IndexFlatIP(data_vecs.shape[1])
index.add(data_vecs)
def recommend(query, top_k=3):
query_vec = embed_model.encode([query])
faiss.normalize_L2(query_vec)
scores, idx = index.search(query_vec, top_k)
results = data.iloc[idx[0]].copy()
results["score"] = scores[0]
return results[["name", "tagline", "description", "score"]]
def generate_slogan(query_text, neighbors_df=None, n_samples=16):
ctx = _neighbor_context(neighbors_df)
prompt = (
"You are a creative brand copywriter. Write short, original, memorable startup slogans (max 8 words).
"
"Forbidden words: app, assistant, platform, solution, system, marketplace, AI, machine learning, augmented reality, virtual reality, decentralized, empower.
"
"Focus on clear benefits and vivid verbs. Do not copy the description. Return ONLY a list, one slogan per line.
"
"Good Examples:
"
"Description: AI assistant for doctors to prioritize patient cases
"
"Slogan: Less Guessing. More Healing.
"
"Description: Payments for small online stores
"
"Slogan: Built to Grow with Your Cart.
"
"Description: Neurotech headset to boost focus
"
"Slogan: Train Your Brain to Win.
"
)
if ctx:
prompt += f"Similar taglines (style only):
{ctx}
"
prompt += f"Description: {query_text}
Slogans:"
input_ids = GEN_TOK(prompt, return_tensors="pt").input_ids.to(DEVICE)
outputs = GEN_MODEL.generate(
input_ids,
max_new_tokens=24,
do_sample=True,
top_k=60,
top_p=0.92,
temperature=1.2,
num_return_sequences=n_samples,
repetition_penalty=1.08
)
raw_cands = [GEN_TOK.decode(o, skip_special_tokens=True) for o in outputs]
cand_set = set()
for txt in raw_cands:
for line in txt.split("
"):
s = _clean_slogan(line)
if not s:
continue
if len(s.split()) < 2 or len(s.split()) > 8:
continue
if _is_blocked_slogan(s):
continue
cand_set.add(_titlecase_soft(s))
if not cand_set:
return _clean_slogan(GEN_TOK.decode(outputs[0], skip_special_tokens=True))
scored = _score_candidates(query_text, sorted(cand_set), neighbors_df)
if not scored:
return _clean_slogan(GEN_TOK.decode(outputs[0], skip_special_tokens=True))
scored.sort(key=lambda x: x[1], reverse=True)
return scored[0][0]
def pipeline(user_input):
recs = recommend(user_input, top_k=3)
slogan = generate_slogan(user_input)
recs = recs.reset_index(drop=True)
recs.loc[len(recs)] = ["Generated Slogan", slogan, user_input, np.nan]
return recs
examples = [
"AI coach for improving public speaking skills",
"Augmented reality app for interactive museum tours",
"Voice-controlled task manager for remote teams",
"Machine learning system for predicting crop yields",
"Platform for AI-assisted interior design suggestions"
]
demo = gr.Interface(
fn=pipeline,
inputs=gr.Textbox(label="Enter a startup description"),
outputs=gr.Dataframe(headers=["Name", "Tagline", "Description", "Score"]),
examples=examples,
title="SloganAI – Startup Recommendation & Slogan Generator",
description="Enter a startup idea and get top-3 similar startups + 1 generated slogan."
)
if __name__ == "__main__":
demo.launch()