Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,6 @@ import pandas as pd
|
|
| 12 |
import torch
|
| 13 |
import gradio as gr
|
| 14 |
from sentence_transformers import SentenceTransformer, util as st_util
|
| 15 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 16 |
import spaces
|
| 17 |
|
| 18 |
warnings.filterwarnings("ignore")
|
|
@@ -27,13 +26,8 @@ def dummy_gpu_function():
|
|
| 27 |
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 29 |
EMBED_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
|
| 30 |
-
GEN_MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 31 |
-
GEN_MODEL_API = "HuggingFaceH4/zephyr-7b-beta"
|
| 32 |
|
| 33 |
# Private events get a different (warmer, personal) prompt tone
|
| 34 |
-
PRIVATE_EVENT_TYPES = {
|
| 35 |
-
"Wedding", "Bar/Bat Mitzvah", "Private Birthday", "Family Reunion"
|
| 36 |
-
}
|
| 37 |
|
| 38 |
VENDOR_CATEGORIES = [
|
| 39 |
"Catering", "AV_Technology", "Venue", "Security",
|
|
@@ -153,68 +147,8 @@ if _df is not None:
|
|
| 153 |
else:
|
| 154 |
_vemb = None
|
| 155 |
|
| 156 |
-
# ================================================================
|
| 157 |
-
# GENERATION MODEL β with retry logic
|
| 158 |
-
# ================================================================
|
| 159 |
-
|
| 160 |
-
_gen_backend = "none"
|
| 161 |
-
_gen_model = None
|
| 162 |
-
_gen_tok = None
|
| 163 |
-
_gen_client = None
|
| 164 |
|
| 165 |
-
if HF_TOKEN:
|
| 166 |
-
try:
|
| 167 |
-
from huggingface_hub import InferenceClient
|
| 168 |
-
_gen_client = InferenceClient(
|
| 169 |
-
model=GEN_MODEL_API, token=HF_TOKEN, timeout=30,
|
| 170 |
-
)
|
| 171 |
-
_gen_backend = "api"
|
| 172 |
-
print(f"β
Generation: HF Inference API ({GEN_MODEL_API})")
|
| 173 |
-
except Exception as e:
|
| 174 |
-
print(f"β οΈ HF API unavailable: {e}")
|
| 175 |
-
|
| 176 |
-
if _gen_backend == "none":
|
| 177 |
-
# TinyLlama takes 90-120 s to load and 3+ min per generation on CPU.
|
| 178 |
-
# Skipping local load β vendor matching works without it.
|
| 179 |
-
# Add HF_TOKEN as a Space Secret to enable fast cloud generation.
|
| 180 |
-
print("βΉοΈ No HF_TOKEN set. Generation disabled. Vendor matching still works.")
|
| 181 |
-
|
| 182 |
-
# ββ Retry wrapper (handles transient API drops) βββββββββββββββ
|
| 183 |
-
def _call_llm(prompt: str, max_new_tokens: int = 450) -> str:
|
| 184 |
-
if _gen_backend == "api":
|
| 185 |
-
last_err = None
|
| 186 |
-
for attempt in range(3): # 3 attempts, 2s gap
|
| 187 |
-
try:
|
| 188 |
-
return _gen_client.text_generation(
|
| 189 |
-
prompt, max_new_tokens=max_new_tokens,
|
| 190 |
-
temperature=0.7, repetition_penalty=1.1, do_sample=True,
|
| 191 |
-
).strip()
|
| 192 |
-
except Exception as e:
|
| 193 |
-
last_err = e
|
| 194 |
-
import time; time.sleep(2 * (attempt + 1)) # 2s, 4s, 6s
|
| 195 |
-
return (f"β οΈ API failed after 3 attempts:\n{last_err}\n\n"
|
| 196 |
-
f"Check that your HF_TOKEN has 'Read' access.")
|
| 197 |
-
|
| 198 |
-
if _gen_backend == "local" and _gen_model is not None:
|
| 199 |
-
try:
|
| 200 |
-
out = _gen_model(
|
| 201 |
-
prompt, max_new_tokens=max_new_tokens, do_sample=True,
|
| 202 |
-
temperature=0.7, repetition_penalty=1.15,
|
| 203 |
-
pad_token_id=_gen_tok.eos_token_id,
|
| 204 |
-
return_full_text=False,
|
| 205 |
-
)
|
| 206 |
-
return out[0]["generated_text"].strip()
|
| 207 |
-
except Exception as e:
|
| 208 |
-
return f"β οΈ Local model error: {e}"
|
| 209 |
|
| 210 |
-
return "β οΈ Generation not available. Add HF_TOKEN as a Space Secret."
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
def _prompt(system: str, user: str) -> str:
|
| 214 |
-
"""Zephyr / TinyLlama chat template."""
|
| 215 |
-
return (f"<|system|>\n{system}</s>\n"
|
| 216 |
-
f"<|user|>\n{user}</s>\n"
|
| 217 |
-
f"<|assistant|>\n")
|
| 218 |
|
| 219 |
# ================================================================
|
| 220 |
# CORE ENGINE
|
|
@@ -261,63 +195,7 @@ def recommend_vendors(brief, city, season, budget, top_n=3):
|
|
| 261 |
return results
|
| 262 |
|
| 263 |
|
| 264 |
-
def gen_run_of_show(ev_type, date, venue, city, guests, vendors, notes):
|
| 265 |
-
"""Context-aware: uses celebratory tone for private events."""
|
| 266 |
-
is_private = ev_type in PRIVATE_EVENT_TYPES
|
| 267 |
-
tone = (
|
| 268 |
-
"This is a personal life event. Use a warm, celebratory tone. "
|
| 269 |
-
"Include milestones like ceremony, first dance, toasts."
|
| 270 |
-
if is_private else
|
| 271 |
-
"This is a professional corporate event. Use formal operational language."
|
| 272 |
-
)
|
| 273 |
-
lines = "\n".join(f" - {k.replace('_', ' ')}: {v}"
|
| 274 |
-
for k, v in vendors.items())
|
| 275 |
-
return _call_llm(_prompt(
|
| 276 |
-
f"You are a senior event production manager. Write a detailed Run of Show "
|
| 277 |
-
f"using format HH:MM β Task. {tone}",
|
| 278 |
-
f"Event: {ev_type}\nDate: {date}\nVenue: {venue}, {city}\n"
|
| 279 |
-
f"Guests: {guests:,}\nNotes: {notes or 'None'}\n\nVendors:\n{lines}"
|
| 280 |
-
), max_new_tokens=600)
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
def gen_rfp(name, cat, specs, ev_type, date, city, guests, alloc_usd, notes):
|
| 284 |
-
"""B2B tone for corporate events, warm personal tone for private events."""
|
| 285 |
-
is_private = ev_type in PRIVATE_EVENT_TYPES
|
| 286 |
-
specs_str = ", ".join(specs[:3]) if specs else "general services"
|
| 287 |
-
cat_label = cat.replace("_", " ")
|
| 288 |
-
|
| 289 |
-
if is_private:
|
| 290 |
-
system_msg = (
|
| 291 |
-
"You are a warm and professional event coordinator helping a private client "
|
| 292 |
-
"reach out to a vendor. Write a friendly but professional inquiry email. "
|
| 293 |
-
"Use a personal, human tone β this is a meaningful personal life event."
|
| 294 |
-
)
|
| 295 |
-
user_msg = (
|
| 296 |
-
f"Write a vendor inquiry email for a {ev_type} on {date} in {city}.\n\n"
|
| 297 |
-
f"Vendor: {name}\nCategory: {cat_label}\nSpecialisms: {specs_str}\n"
|
| 298 |
-
f"Guest count: {guests:,}\nBudget for this service: ${alloc_usd:,.0f}\n"
|
| 299 |
-
f"Special requests: {notes or 'None'}\n\n"
|
| 300 |
-
f"Keep the tone warm and celebratory. Request availability and a quote, "
|
| 301 |
-
f"and express genuine excitement about the event."
|
| 302 |
-
)
|
| 303 |
-
else:
|
| 304 |
-
system_msg = (
|
| 305 |
-
"You are a professional event producer writing a formal B2B RFP email. "
|
| 306 |
-
"Be specific and professional. End with a clear call to action requesting "
|
| 307 |
-
"availability and a detailed quote within 48 hours."
|
| 308 |
-
)
|
| 309 |
-
user_msg = (
|
| 310 |
-
f"Write a formal RFP email to:\n\n"
|
| 311 |
-
f"Vendor: {name}\nCategory: {cat_label}\nSpecialisms: {specs_str}\n\n"
|
| 312 |
-
f"Event: {ev_type}\nDate: {date}\nLocation: {city}\n"
|
| 313 |
-
f"Guests: {guests:,}\nBudget: ${alloc_usd:,.0f} for {cat_label}\n"
|
| 314 |
-
f"Requirements: {notes or 'Standard professional service'}"
|
| 315 |
-
)
|
| 316 |
-
return _call_llm(_prompt(system_msg, user_msg), max_new_tokens=400)
|
| 317 |
|
| 318 |
-
# ================================================================
|
| 319 |
-
# OUTPUT FORMATTERS
|
| 320 |
-
# ================================================================
|
| 321 |
|
| 322 |
def _stars(r):
|
| 323 |
n = min(5, max(0, int(round(float(r)))))
|
|
@@ -355,56 +233,20 @@ def _fmt_vendors(recs, budget):
|
|
| 355 |
# GRADIO HANDLER
|
| 356 |
# ================================================================
|
| 357 |
|
| 358 |
-
def handle_submit(brief, city, season, budget, ev_type,
|
| 359 |
-
guests, notes):
|
|
|
|
| 360 |
recs = recommend_vendors(brief, city, season, float(budget))
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
if "error" in recs or _gen_backend == "none":
|
| 364 |
-
msg = (recs.get("error", "") or
|
| 365 |
-
"Add HF_TOKEN as a Space Secret to enable document generation.")
|
| 366 |
-
return vendor_md, msg, msg
|
| 367 |
-
|
| 368 |
-
selected, targets = {}, []
|
| 369 |
-
for cat in VENDOR_CATEGORIES:
|
| 370 |
-
if cat not in recs or not recs[cat]: continue
|
| 371 |
-
top = recs[cat][0]
|
| 372 |
-
name = top.get("vendor_name", "Unknown")
|
| 373 |
-
sp = top.get("specializations", [])
|
| 374 |
-
if isinstance(sp, str):
|
| 375 |
-
try: sp = json.loads(sp)
|
| 376 |
-
except: sp = []
|
| 377 |
-
selected[cat] = name
|
| 378 |
-
targets.append({
|
| 379 |
-
"name": name, "cat": cat, "specs": sp,
|
| 380 |
-
"alloc": float(budget) * ALLOC_RATIOS[cat],
|
| 381 |
-
})
|
| 382 |
-
|
| 383 |
-
ros = gen_run_of_show(ev_type, date, venue, city,
|
| 384 |
-
int(guests), selected, notes)
|
| 385 |
-
|
| 386 |
-
blocks = []
|
| 387 |
-
for t in sorted(targets, key=lambda x: -x["alloc"])[:3]:
|
| 388 |
-
txt = gen_rfp(
|
| 389 |
-
t["name"], t["cat"], t["specs"],
|
| 390 |
-
ev_type, date, city, int(guests), t["alloc"], notes,
|
| 391 |
-
)
|
| 392 |
-
cat_label = t["cat"].replace("_", " ")
|
| 393 |
-
blocks.append(
|
| 394 |
-
f"{'β'*54}\n RFP β {cat_label.upper()}\n"
|
| 395 |
-
f" TO : {t['name']}\n BUDGET: ${t['alloc']:,.0f}\n"
|
| 396 |
-
f"{'β'*54}\n\n" + txt + "\n"
|
| 397 |
-
)
|
| 398 |
-
return vendor_md, ros, "\n\n".join(blocks)
|
| 399 |
|
| 400 |
|
| 401 |
def _qs(idx):
|
| 402 |
q = QUICK_STARTERS[idx]
|
| 403 |
b, c, s, bu = q["brief"], q["city"], q["season"], q["budget"]
|
| 404 |
-
et, dt
|
| 405 |
gs, nt = q["guests"], q["notes"]
|
| 406 |
-
vm
|
| 407 |
-
return b, c, s, bu, et, dt,
|
| 408 |
|
| 409 |
def _qs0(): return _qs(0)
|
| 410 |
def _qs1(): return _qs(1)
|
|
@@ -497,7 +339,7 @@ with gr.Blocks(css=CSS, theme=gr.themes.Base(), title="ProSync AI") as demo:
|
|
| 497 |
|
| 498 |
gr.HTML("""
|
| 499 |
<div class="ps-header">
|
| 500 |
-
<h1
|
| 501 |
<p>The Event Producer's Command Center β intelligent vendor matching & automated document generation</p>
|
| 502 |
</div>
|
| 503 |
""")
|
|
@@ -519,11 +361,11 @@ with gr.Blocks(css=CSS, theme=gr.themes.Base(), title="ProSync AI") as demo:
|
|
| 519 |
"kosher catering, VIP securityβ¦",
|
| 520 |
)
|
| 521 |
with gr.Row():
|
| 522 |
-
city = gr.Dropdown(label="City", choices=CITIES, value="Tel Aviv")
|
| 523 |
-
season = gr.Dropdown(label="Season", choices=SEASONS, value="Summer")
|
| 524 |
-
budget = gr.
|
| 525 |
label="Total Budget (USD)",
|
| 526 |
-
|
| 527 |
)
|
| 528 |
|
| 529 |
gr.Markdown("---")
|
|
@@ -531,11 +373,12 @@ with gr.Blocks(css=CSS, theme=gr.themes.Base(), title="ProSync AI") as demo:
|
|
| 531 |
with gr.Row():
|
| 532 |
ev_type = gr.Dropdown(
|
| 533 |
label="Event Type", choices=EVENT_TYPES, value="Tech Summit",
|
|
|
|
| 534 |
)
|
| 535 |
guests = gr.Number(label="Guest Count", value=300, minimum=10, maximum=5000)
|
| 536 |
with gr.Row():
|
| 537 |
-
|
| 538 |
-
|
| 539 |
notes = gr.Textbox(
|
| 540 |
label="Special Requirements",
|
| 541 |
placeholder="e.g. Kosher catering, black-tie dress code, vegan menuβ¦",
|
|
@@ -553,25 +396,13 @@ with gr.Blocks(css=CSS, theme=gr.themes.Base(), title="ProSync AI") as demo:
|
|
| 553 |
elem_classes=["prose"],
|
| 554 |
)
|
| 555 |
|
| 556 |
-
gr.Markdown("### π Run of Show")
|
| 557 |
-
ros_out = gr.Textbox(
|
| 558 |
-
label="", lines=18, interactive=False,
|
| 559 |
-
placeholder="Generated schedule will appear hereβ¦",
|
| 560 |
-
)
|
| 561 |
-
|
| 562 |
-
gr.Markdown("### βοΈ RFP Emails")
|
| 563 |
-
rfp_out = gr.Textbox(
|
| 564 |
-
label="", lines=18, interactive=False,
|
| 565 |
-
placeholder="Generated emails will appear hereβ¦",
|
| 566 |
-
)
|
| 567 |
-
|
| 568 |
gr.HTML('<div class="ps-footer">ProSync AI Β· Gradio + HuggingFace Β· '
|
| 569 |
-
'MiniLM-L6-v2
|
| 570 |
|
| 571 |
# ββ Wiring ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 572 |
-
_in = [brief, city, season, budget, ev_type,
|
| 573 |
-
_out = [vendor_out
|
| 574 |
-
_form = [brief, city, season, budget, ev_type,
|
| 575 |
|
| 576 |
submit.click(fn=handle_submit, inputs=_in, outputs=_out)
|
| 577 |
qs0.click(fn=_qs0, outputs=_form + _out)
|
|
|
|
| 12 |
import torch
|
| 13 |
import gradio as gr
|
| 14 |
from sentence_transformers import SentenceTransformer, util as st_util
|
|
|
|
| 15 |
import spaces
|
| 16 |
|
| 17 |
warnings.filterwarnings("ignore")
|
|
|
|
| 26 |
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 27 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 28 |
EMBED_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Private events get a different (warmer, personal) prompt tone
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
VENDOR_CATEGORIES = [
|
| 33 |
"Catering", "AV_Technology", "Venue", "Security",
|
|
|
|
| 147 |
else:
|
| 148 |
_vemb = None
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
# ================================================================
|
| 154 |
# CORE ENGINE
|
|
|
|
| 195 |
return results
|
| 196 |
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
def _stars(r):
|
| 201 |
n = min(5, max(0, int(round(float(r)))))
|
|
|
|
| 233 |
# GRADIO HANDLER
|
| 234 |
# ================================================================
|
| 235 |
|
| 236 |
+
def handle_submit(brief, city, season, budget, ev_type,
|
| 237 |
+
date_from, date_to, guests, notes):
|
| 238 |
+
date_range = date_from if date_from == date_to else f"{date_from} β {date_to}"
|
| 239 |
recs = recommend_vendors(brief, city, season, float(budget))
|
| 240 |
+
return _fmt_vendors(recs, float(budget))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
|
| 243 |
def _qs(idx):
|
| 244 |
q = QUICK_STARTERS[idx]
|
| 245 |
b, c, s, bu = q["brief"], q["city"], q["season"], q["budget"]
|
| 246 |
+
et, dt = q["type"], q["date"]
|
| 247 |
gs, nt = q["guests"], q["notes"]
|
| 248 |
+
vm = handle_submit(b, c, s, bu, et, dt, dt, gs, nt)
|
| 249 |
+
return b, c, s, bu, et, dt, dt, gs, nt, vm
|
| 250 |
|
| 251 |
def _qs0(): return _qs(0)
|
| 252 |
def _qs1(): return _qs(1)
|
|
|
|
| 339 |
|
| 340 |
gr.HTML("""
|
| 341 |
<div class="ps-header">
|
| 342 |
+
<h1 style="text-align:center;">ProSync AI</h1>
|
| 343 |
<p>The Event Producer's Command Center β intelligent vendor matching & automated document generation</p>
|
| 344 |
</div>
|
| 345 |
""")
|
|
|
|
| 361 |
"kosher catering, VIP securityβ¦",
|
| 362 |
)
|
| 363 |
with gr.Row():
|
| 364 |
+
city = gr.Dropdown(label="City", choices=CITIES, value="Tel Aviv", allow_custom_value=False)
|
| 365 |
+
season = gr.Dropdown(label="Season", choices=SEASONS, value="Summer", allow_custom_value=False)
|
| 366 |
+
budget = gr.Number(
|
| 367 |
label="Total Budget (USD)",
|
| 368 |
+
value=200_000, minimum=5_000, maximum=2_000_000,
|
| 369 |
)
|
| 370 |
|
| 371 |
gr.Markdown("---")
|
|
|
|
| 373 |
with gr.Row():
|
| 374 |
ev_type = gr.Dropdown(
|
| 375 |
label="Event Type", choices=EVENT_TYPES, value="Tech Summit",
|
| 376 |
+
allow_custom_value=False,
|
| 377 |
)
|
| 378 |
guests = gr.Number(label="Guest Count", value=300, minimum=10, maximum=5000)
|
| 379 |
with gr.Row():
|
| 380 |
+
date_from = gr.Textbox(label="Event Start Date", value="2026-10-15", placeholder="YYYY-MM-DD")
|
| 381 |
+
date_to = gr.Textbox(label="Event End Date", value="2026-10-15", placeholder="YYYY-MM-DD")
|
| 382 |
notes = gr.Textbox(
|
| 383 |
label="Special Requirements",
|
| 384 |
placeholder="e.g. Kosher catering, black-tie dress code, vegan menuβ¦",
|
|
|
|
| 396 |
elem_classes=["prose"],
|
| 397 |
)
|
| 398 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
gr.HTML('<div class="ps-footer">ProSync AI Β· Gradio + HuggingFace Β· '
|
| 400 |
+
'MiniLM-L6-v2</div>')
|
| 401 |
|
| 402 |
# ββ Wiring ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 403 |
+
_in = [brief, city, season, budget, ev_type, date_from, date_to, guests, notes]
|
| 404 |
+
_out = [vendor_out]
|
| 405 |
+
_form = [brief, city, season, budget, ev_type, date_from, date_to, guests, notes]
|
| 406 |
|
| 407 |
submit.click(fn=handle_submit, inputs=_in, outputs=_out)
|
| 408 |
qs0.click(fn=_qs0, outputs=_form + _out)
|