Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +142 -33
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,149 @@
|
|
| 1 |
-
import
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
|
| 13 |
-
|
| 14 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
import os
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
from openai import OpenAI
|
| 4 |
|
| 5 |
+
# ------------------------------
|
| 6 |
+
# CONFIG
|
| 7 |
+
# ------------------------------
|
| 8 |
+
# Hugging Face: Settings -> Variables -> add OPENAI_API_KEY
|
| 9 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") or st.secrets.get("OPENAI_API_KEY", None)
|
| 10 |
+
|
| 11 |
+
st.set_page_config(
|
| 12 |
+
page_title="Procelevate AI Agent",
|
| 13 |
+
page_icon="🤖",
|
| 14 |
+
layout="wide"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
st.title("🤖 Procelevate Agent Suite")
|
| 18 |
+
st.write("PO Automation + Email Responder (Streamlit, HF Docker)")
|
| 19 |
+
|
| 20 |
+
if not OPENAI_API_KEY:
|
| 21 |
+
st.warning("⚠️ OPENAI_API_KEY not found. Set it in the Space secrets.")
|
| 22 |
+
client = OpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def call_llm(system_prompt: str, user_prompt: str) -> str:
|
| 26 |
+
"""Small helper to call OpenAI."""
|
| 27 |
+
if client is None:
|
| 28 |
+
return "❗OPENAI_API_KEY not set."
|
| 29 |
+
resp = client.chat.completions.create(
|
| 30 |
+
model="gpt-4o-mini",
|
| 31 |
+
messages=[
|
| 32 |
+
{"role": "system", "content": system_prompt},
|
| 33 |
+
{"role": "user", "content": user_prompt},
|
| 34 |
+
],
|
| 35 |
+
temperature=0.3,
|
| 36 |
+
)
|
| 37 |
+
return resp.choices[0].message.content
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# ------------------------------
|
| 41 |
+
# PO GENERATOR
|
| 42 |
+
# ------------------------------
|
| 43 |
+
def generate_po(buyer_name, buyer_address, supplier_name, supplier_email, items_text, payment_terms, notes):
|
| 44 |
+
system_prompt = """You are a Purchase Order (PO) generation assistant for Procelevate.
|
| 45 |
+
You will take supplier details, buyer details, item list, and payment terms, and output a clean, well-structured PO.
|
| 46 |
+
Output in markdown with clear headings, table for line items, and totals.
|
| 47 |
+
If any field is missing, make reasonable placeholders."""
|
| 48 |
+
user_prompt = f"""
|
| 49 |
+
Create a Purchase Order with the following details:
|
| 50 |
+
|
| 51 |
+
Buyer:
|
| 52 |
+
- Name: {buyer_name}
|
| 53 |
+
- Address: {buyer_address}
|
| 54 |
+
|
| 55 |
+
Supplier:
|
| 56 |
+
- Name: {supplier_name}
|
| 57 |
+
- Email: {supplier_email}
|
| 58 |
+
|
| 59 |
+
Line Items (parse this and make a table with Item, Qty, Unit Price, Line Total):
|
| 60 |
+
{items_text}
|
| 61 |
+
|
| 62 |
+
Payment Terms: {payment_terms}
|
| 63 |
+
Additional Notes: {notes}
|
| 64 |
+
|
| 65 |
+
At the end, show Subtotal, Taxes (assume 18% if not given), and Grand Total.
|
| 66 |
"""
|
| 67 |
+
return call_llm(system_prompt, user_prompt)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# ------------------------------
|
| 71 |
+
# EMAIL RESPONDER
|
| 72 |
+
# ------------------------------
|
| 73 |
+
def generate_email_reply(incoming_email, reply_goal, tone):
|
| 74 |
+
system_prompt = """You are an AI email responder for Procelevate Consulting & Academy.
|
| 75 |
+
You will read an incoming email and write a professional, concise reply matching the selected tone.
|
| 76 |
+
Always add a polite closing with 'Regards, Procelevate Team' unless user says otherwise."""
|
| 77 |
+
user_prompt = f"""
|
| 78 |
+
Incoming email:
|
| 79 |
+
\"\"\"{incoming_email}\"\"\"
|
| 80 |
|
| 81 |
+
Goal of reply: {reply_goal}
|
| 82 |
+
Tone: {tone}
|
|
|
|
| 83 |
|
| 84 |
+
Write the reply email body only.
|
| 85 |
"""
|
| 86 |
+
return call_llm(system_prompt, user_prompt)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# ------------------------------
|
| 90 |
+
# UI
|
| 91 |
+
# ------------------------------
|
| 92 |
+
tab1, tab2 = st.tabs(["📄 PO Automation", "📧 Email Responder"])
|
| 93 |
+
|
| 94 |
+
with tab1:
|
| 95 |
+
st.subheader("📄 Generate Purchase Order")
|
| 96 |
+
col1, col2 = st.columns(2)
|
| 97 |
+
|
| 98 |
+
with col1:
|
| 99 |
+
buyer_name = st.text_input("Buyer Name", value="Procelevate Consulting")
|
| 100 |
+
buyer_address = st.text_area("Buyer Address", value="Bengaluru, Karnataka, India", height=80)
|
| 101 |
+
payment_terms = st.text_input("Payment Terms", value="Net 30 days")
|
| 102 |
+
with col2:
|
| 103 |
+
supplier_name = st.text_input("Supplier Name", value="")
|
| 104 |
+
supplier_email = st.text_input("Supplier Email", value="")
|
| 105 |
+
notes = st.text_area("Additional Notes", value="Please deliver services as per agreed SOW.", height=80)
|
| 106 |
+
|
| 107 |
+
items_text = st.text_area(
|
| 108 |
+
"Line Items (one per line)",
|
| 109 |
+
value="1. AI/ML Bootcamp Seats, Qty 10, Unit Price 2999\n2. Consulting Hours, Qty 5, Unit Price 1500",
|
| 110 |
+
height=120
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
if st.button("Generate PO"):
|
| 114 |
+
po_md = generate_po(
|
| 115 |
+
buyer_name,
|
| 116 |
+
buyer_address,
|
| 117 |
+
supplier_name,
|
| 118 |
+
supplier_email,
|
| 119 |
+
items_text,
|
| 120 |
+
payment_terms,
|
| 121 |
+
notes
|
| 122 |
+
)
|
| 123 |
+
st.markdown("---")
|
| 124 |
+
st.markdown("### ✅ Generated PO")
|
| 125 |
+
st.markdown(po_md)
|
| 126 |
+
|
| 127 |
+
with tab2:
|
| 128 |
+
st.subheader("📧 Generate Email Reply")
|
| 129 |
+
incoming_email = st.text_area(
|
| 130 |
+
"Incoming email",
|
| 131 |
+
value="Hi Mahesh, we are interested in your Smart Daycare Suite. Can you share pricing and a demo slot?",
|
| 132 |
+
height=140
|
| 133 |
+
)
|
| 134 |
+
reply_goal = st.text_input(
|
| 135 |
+
"What should the reply do?",
|
| 136 |
+
value="Acknowledge, share that we have multiple AI/ML modules, and propose 30-min demo."
|
| 137 |
+
)
|
| 138 |
+
tone = st.selectbox(
|
| 139 |
+
"Tone",
|
| 140 |
+
options=["Professional", "Friendly", "Formal-Client", "Follow-up/Reminder"],
|
| 141 |
+
index=0
|
| 142 |
+
)
|
| 143 |
|
| 144 |
+
if st.button("Generate Reply"):
|
| 145 |
+
reply_text = generate_email_reply(incoming_email, reply_goal, tone)
|
| 146 |
+
st.markdown("---")
|
| 147 |
+
st.markdown("### ✅ Reply Email")
|
| 148 |
+
st.markdown(reply_text)
|
| 149 |
+
st.info("You can copy-paste this into Gmail / Outlook.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|