File size: 2,883 Bytes
f4146f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2af4b15
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

import streamlit as st
import json
from salesforce import get_context, create_note
from model import PreCallSummaryModel
from schema import PreCallSummary

st.set_page_config(page_title="Pre‑Call Summary Generator", layout="wide")

st.title("Pre‑Call Summary Generator (Salesforce → HF Space)")

with st.expander("Instructions", expanded=False):
    st.markdown("""
    1. Enter your Salesforce **Instance URL** (e.g., `https://xxx.my.salesforce.com`).
    2. Provide a short‑lived **Access Token** (from a Connected App / OAuth flow).
    3. Provide an **Account Id** (`001...`).
    4. Click **Generate Summary**. Optionally save it as a **Note** in Salesforce.
    **Security:** tokens are used only in memory for this session and are not logged.
    """)

inst = st.text_input("Salesforce Instance URL", placeholder="https://yourDomain.my.salesforce.com")
token = st.text_input("Salesforce Access Token", type="password", placeholder="Paste a short‑lived token")
acct_id = st.text_input("Account Id", placeholder="001xxxxxxxxxxxx")

colA, colB, colC = st.columns([1,1,1])
with colA:
    opp_n = st.number_input("Max Opportunities", min_value=0, max_value=20, value=5, step=1)
with colB:
    case_n = st.number_input("Max Cases", min_value=0, max_value=20, value=5, step=1)
with colC:
    task_n = st.number_input("Max Tasks", min_value=0, max_value=50, value=10, step=1)

push_back = st.checkbox("Write to Salesforce as Note after generation", value=False)
generate = st.button("Generate Summary", type="primary")

if generate:
    if not inst or not token or not acct_id:
        st.error("Please provide instance URL, access token, and account id.")
        st.stop()

    with st.spinner("Fetching Salesforce context..."):
        try:
            context = get_context(inst, token, acct_id, opp_n, case_n, task_n)
        except Exception as e:
            st.error(f"Salesforce API error: {e}")
            st.stop()

    st.subheader("Fetched Context (trimmed)")
    st.json(context)

    with st.spinner("Calling model..."):
        model = PreCallSummaryModel()
        try:
            summary: PreCallSummary = model.generate(context)
        except Exception as e:
            st.error(f"Model error: {e}")
            st.stop()

    st.subheader("Pre‑Call Summary (JSON)")
    st.json(json.loads(summary.model_dump_json()))

    if push_back:
        with st.spinner("Writing Note to Salesforce..."):
            try:
                note_id = create_note(inst, token, acct_id, "Pre‑Call Summary", summary.model_dump_json(indent=2))
                st.success(f"Saved Note Id: {note_id}")
            except Exception as e:
                st.error(f"Failed to save Note: {e}")

    st.download_button("Download JSON", data=summary.model_dump_json(indent=2),
                       file_name=f"precall_summary_{acct_id}.json", mime="application/json")