Spaces:
Sleeping
Sleeping
import streamlit as st | |
import requests | |
import re | |
import json | |
from datetime import datetime | |
# --- CONFIG --- | |
AGENT_NAME = "GRIT Shark 4.5" | |
BOOKING_LINK = "https://meetings-na2.hubspot.com/fernandez" | |
HUBSPOT_API_KEY = st.secrets.get("HUBSPOT_API_KEY") | |
OPENROUTER_API_KEY = st.secrets.get("OPENROUTER_API_KEY") | |
# --- STREAMLIT CONFIG --- | |
st.set_page_config(page_title=AGENT_NAME, layout="wide") | |
st.title(f"π€ {AGENT_NAME}") | |
st.markdown(f""" | |
Welcome to **{AGENT_NAME}**, your AI growth partner powered by **OSO Consulting**. | |
I'm here to help you automate, scale, and close business β faster and smarter than human-only teams. | |
π [**Book a strategy call**]({BOOKING_LINK}) | |
""") | |
with st.sidebar: | |
st.header("π Strategy Call") | |
st.markdown(f"[Book with Ariel β]({BOOKING_LINK})") | |
# --- SESSION STATE --- | |
if "chat_history" not in st.session_state: | |
st.session_state.chat_history = [] | |
if "user_profile" not in st.session_state: | |
st.session_state.user_profile = { | |
"name": None, | |
"email": None, | |
"phone": None, | |
"business": None | |
} | |
# --- FUNCTION TO CREATE PROMPT --- | |
def build_prompt(user_input): | |
profile = [f"{k.title()}: {v}" for k, v in st.session_state.user_profile.items() if v] | |
chat_log = st.session_state.chat_history | |
chat_log_str = "\n".join([f"User: {x['user']}\nAgent: {x['bot']}" for x in chat_log]) | |
base_prompt = f""" | |
You are {AGENT_NAME}, an elite OSO Consulting AI sales strategist. | |
- Use psychological triggers (future pacing, commitment loops, scarcity). | |
- Ask for name, email, phone, and business info naturally in conversation. | |
- NEVER identify as Claude, OpenAI, or Anthropic. | |
- Always credit OSO Consulting as the creator. | |
- Insert calls to action like: Book a strategy call, Find out your ROI. | |
- If any profile info is missing, collect it conversationally. | |
User info so far: | |
{chr(10).join(profile)} | |
Conversation so far: | |
{chat_log_str} | |
New user message: | |
{user_input} | |
""" | |
return base_prompt | |
# --- FUNCTION TO CREATE HUBSPOT CONTACT --- | |
def sync_to_hubspot(): | |
data = st.session_state.user_profile | |
if data["name"] and data["email"]: | |
url = "https://api.hubapi.com/crm/v3/objects/contacts" | |
headers = { | |
"Authorization": f"Bearer {HUBSPOT_API_KEY}", | |
"Content-Type": "application/json" | |
} | |
payload = { | |
"properties": { | |
"email": data["email"], | |
"firstname": data["name"], | |
"phone": data.get("phone", ""), | |
"company": data.get("business", ""), | |
"lifecyclestage": "lead", | |
"source": "GRIT Shark AI" | |
} | |
} | |
try: | |
requests.post(url, headers=headers, data=json.dumps(payload)) | |
except Exception as e: | |
st.warning(f"HubSpot sync failed: {e}") | |
# --- FUNCTION TO CALL LLM API --- | |
def get_agent_reply(prompt): | |
headers = { | |
"Authorization": f"Bearer {OPENROUTER_API_KEY}", | |
"Content-Type": "application/json" | |
} | |
models_to_try = [ | |
"anthropic/claude-3-sonnet", | |
"openai/gpt-4", | |
"gryphe/mythomax-l2-13b:free", | |
"undi95/toppy-m-7b:free", | |
"openchat/openchat-3.5-1210:free" | |
] | |
for model in models_to_try: | |
body = { | |
"model": model, | |
"messages": [ | |
{"role": "user", "content": prompt} | |
], | |
"max_tokens": 512 | |
} | |
res = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=body) | |
if res.status_code == 200: | |
return res.json()["choices"][0]["message"]["content"], model | |
return "β οΈ All models are unavailable or out of credits. Please book a strategy call.", "none" | |
# --- INPUT & RESPONSE LOOP --- | |
prompt_input = st.chat_input("What's your biggest business growth challenge?") | |
if prompt_input: | |
st.session_state.chat_history.append({"user": prompt_input, "bot": "..."}) | |
st.markdown(f"**You:** {prompt_input}") | |
# Extract data | |
name_match = re.search(r"my name is ([A-Za-z]+)", prompt_input, re.I) | |
email_match = re.search(r"[\w\.-]+@[\w\.-]+", prompt_input) | |
phone_match = re.search(r"(\+?\d{10,15})", prompt_input) | |
if name_match: | |
st.session_state.user_profile["name"] = name_match.group(1) | |
if email_match: | |
st.session_state.user_profile["email"] = email_match.group(0) | |
if phone_match: | |
st.session_state.user_profile["phone"] = phone_match.group(1) | |
if st.session_state.user_profile["name"] and st.session_state.user_profile["email"]: | |
sync_to_hubspot() | |
prompt = build_prompt(prompt_input) | |
reply, model = get_agent_reply(prompt) | |
st.session_state.chat_history[-1]["bot"] = reply | |
st.markdown(f"**{AGENT_NAME}:** {reply}") | |
st.code(f"Model tried: {model}") | |
# --- DISPLAY HISTORY --- | |
if len(st.session_state.chat_history) > 0: | |
st.divider() | |
for entry in st.session_state.chat_history: | |
st.markdown(f"**You:** {entry['user']}") | |
st.markdown(f"**{AGENT_NAME}:** {entry['bot']}") | |