Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
-
import pandas as pd
|
| 6 |
import streamlit as st
|
|
|
|
| 7 |
import matplotlib
|
| 8 |
matplotlib.use("Agg")
|
| 9 |
import matplotlib.pyplot as plt
|
|
@@ -15,102 +17,188 @@ try:
|
|
| 15 |
except ImportError:
|
| 16 |
MarkdownIt = None
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
-
os.environ["STREAMLIT_CONFIG_DIR"] = tempfile.gettempdir()
|
| 22 |
-
os.environ["MPLCONFIGDIR"] = tempfile.gettempdir()
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
FONT_DIR = Path(__file__).parent
|
| 25 |
FONT_NAME = "NotoSans"
|
| 26 |
FONT_REGULAR_TTF = FONT_DIR / "NotoSans-Regular.ttf"
|
| 27 |
FONT_BOLD_TTF = FONT_DIR / "NotoSans-Bold.ttf"
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 36 |
if not api_key:
|
| 37 |
-
st.error("β `GEMINI_API_KEY`
|
| 38 |
st.stop()
|
| 39 |
|
| 40 |
-
|
| 41 |
-
try:
|
| 42 |
-
genai.Client(api_key=api_key)
|
| 43 |
-
except Exception as e:
|
| 44 |
-
st.exception(e)
|
| 45 |
-
st.stop()
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
from google.genai import types
|
| 58 |
-
|
| 59 |
-
svc = InMemorySessionService()
|
| 60 |
-
session_id = str(uuid.uuid4())
|
| 61 |
-
await svc.create_session(app_name="report_app",
|
| 62 |
-
user_id="user1",
|
| 63 |
-
session_id=session_id)
|
| 64 |
-
|
| 65 |
-
runner = Runner(agent=root_agent,
|
| 66 |
-
app_name="report_app",
|
| 67 |
-
session_service=svc)
|
| 68 |
-
content = types.Content(role="user",
|
| 69 |
-
parts=[types.Part(text=json.dumps(context))])
|
| 70 |
-
|
| 71 |
-
async for ev in runner.run_async(user_id="user1",
|
| 72 |
-
session_id=session_id,
|
| 73 |
-
new_message=content):
|
| 74 |
-
if ev.is_final_response():
|
| 75 |
-
return ev.content.parts[0].text
|
| 76 |
-
return None
|
| 77 |
-
|
| 78 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 79 |
-
# 4οΈβ£ Main execution
|
| 80 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 81 |
-
if run_button:
|
| 82 |
-
# 4.1 Load dataset
|
| 83 |
if not uploaded:
|
| 84 |
-
st.warning("
|
| 85 |
st.stop()
|
| 86 |
|
|
|
|
| 87 |
try:
|
| 88 |
df = (pd.read_excel(uploaded)
|
| 89 |
if uploaded.name.lower().endswith(".xlsx")
|
| 90 |
else pd.read_csv(uploaded))
|
| 91 |
-
st.success(f"Loaded **{df.shape[0]} rows Γ {df.shape[1]} columns**")
|
| 92 |
except Exception as e:
|
| 93 |
st.error(f"Failed to read file: {e}")
|
| 94 |
st.stop()
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
# 4.2 Build
|
| 97 |
-
|
| 98 |
-
instruction = """
|
| 99 |
You are a senior business analyst. Write an executive-level Markdown report
|
| 100 |
covering descriptive statistics, key insights, and recommendations.
|
| 101 |
-
|
| 102 |
-
<generate_chart: "bar chart of total_sales by region">
|
| 103 |
"""
|
| 104 |
report_agent = LlmAgent(
|
| 105 |
name="ReportAgent",
|
| 106 |
model="gemini-2.5-flash",
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
)
|
| 110 |
-
root_agent = SequentialAgent(name="Pipeline",
|
| 111 |
-
sub_agents=[report_agent])
|
| 112 |
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
ctx = {
|
| 115 |
"dataset_info": {
|
| 116 |
"shape": df.shape,
|
|
@@ -122,108 +210,139 @@ Insert placeholder tags for visualisations like:
|
|
| 122 |
"preview": df.head().to_dict(),
|
| 123 |
}
|
| 124 |
|
| 125 |
-
# 4.4 Run
|
| 126 |
-
with st.spinner("
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
pandas_agent.run(f"Create a {desc} using matplotlib")
|
| 146 |
fig = plt.gcf()
|
| 147 |
-
|
| 148 |
-
fig.savefig(
|
| 149 |
plt.close()
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
###############################################################################
|
| 2 |
+
# 0. Imports & constants
|
| 3 |
+
###############################################################################
|
| 4 |
+
import os, re, json, uuid, tempfile, asyncio, base64, io, hashlib
|
| 5 |
from pathlib import Path
|
| 6 |
|
|
|
|
| 7 |
import streamlit as st
|
| 8 |
+
import pandas as pd
|
| 9 |
import matplotlib
|
| 10 |
matplotlib.use("Agg")
|
| 11 |
import matplotlib.pyplot as plt
|
|
|
|
| 17 |
except ImportError:
|
| 18 |
MarkdownIt = None
|
| 19 |
|
| 20 |
+
from pptx import Presentation
|
| 21 |
+
from pptx.util import Inches, Pt
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Google Slides
|
| 24 |
+
from google.oauth2.service_account import Credentials
|
| 25 |
+
from googleapiclient.discovery import build
|
| 26 |
+
|
| 27 |
+
# Gemini
|
| 28 |
+
from google import genai
|
| 29 |
+
from google.genai import types
|
| 30 |
+
from google.adk.agents import LlmAgent, SequentialAgent
|
| 31 |
+
from google.adk.runners import Runner
|
| 32 |
+
from google.adk.sessions import InMemorySessionService
|
| 33 |
+
from langchain_experimental.agents import create_pandas_dataframe_agent
|
| 34 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 35 |
+
|
| 36 |
+
###############################################################################
|
| 37 |
+
# 1. Config
|
| 38 |
+
###############################################################################
|
| 39 |
FONT_DIR = Path(__file__).parent
|
| 40 |
FONT_NAME = "NotoSans"
|
| 41 |
FONT_REGULAR_TTF = FONT_DIR / "NotoSans-Regular.ttf"
|
| 42 |
FONT_BOLD_TTF = FONT_DIR / "NotoSans-Bold.ttf"
|
| 43 |
|
| 44 |
+
SLIDE_COUNT = 7
|
| 45 |
+
TTS_MODEL = "gemini-2.5-flash-preview-tts"
|
| 46 |
+
GOOGLE_CREDENTIALS_JSON = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON") # optional
|
| 47 |
+
|
| 48 |
+
os.environ["STREAMLIT_CONFIG_DIR"] = tempfile.gettempdir()
|
| 49 |
+
os.environ["MPLCONFIGDIR"] = tempfile.gettempdir()
|
| 50 |
+
|
| 51 |
+
###############################################################################
|
| 52 |
+
# 2. Utility helpers
|
| 53 |
+
###############################################################################
|
| 54 |
+
def _hash_df(df: pd.DataFrame) -> str:
|
| 55 |
+
"""Create a stable hash of the dataframe content for cache keys."""
|
| 56 |
+
return hashlib.sha1(pd.util.hash_pandas_object(df, index=True).values).hexdigest()
|
| 57 |
+
|
| 58 |
+
@st.cache_resource(show_spinner=False)
|
| 59 |
+
def _get_pandas_agent(df_hash, api_key):
|
| 60 |
+
# df_hash ensures a new agent when data changes
|
| 61 |
+
dummy_df = st.session_state["dataframe"] # leverage existing df in state
|
| 62 |
+
return create_pandas_dataframe_agent(
|
| 63 |
+
llm=ChatGoogleGenerativeAI(model="gemini-2.0-flash",
|
| 64 |
+
google_api_key=api_key),
|
| 65 |
+
df=dummy_df,
|
| 66 |
+
verbose=False,
|
| 67 |
+
allow_dangerous_code=True,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
def generate_tts_audio(_client, text_to_speak):
|
| 71 |
+
"""Generate TTS via Gemini (provided by user)."""
|
| 72 |
+
try:
|
| 73 |
+
if len(text_to_speak) > 500:
|
| 74 |
+
text_to_speak = text_to_speak[:500] + "..."
|
| 75 |
+
resp = _client.models.generate_content(
|
| 76 |
+
model=TTS_MODEL,
|
| 77 |
+
contents=f"Say clearly: {text_to_speak}",
|
| 78 |
+
config=types.GenerateContentConfig(
|
| 79 |
+
response_modalities=["AUDIO"],
|
| 80 |
+
speech_config=types.SpeechConfig(
|
| 81 |
+
voice_config=types.VoiceConfig(
|
| 82 |
+
prebuilt_voice_config=types.PrebuiltVoiceConfig(
|
| 83 |
+
voice_name="Kore"
|
| 84 |
+
)
|
| 85 |
+
)
|
| 86 |
+
),
|
| 87 |
+
),
|
| 88 |
+
)
|
| 89 |
+
audio_part = resp.candidates[0].content.parts[0]
|
| 90 |
+
return audio_part.inline_data.data, audio_part.inline_data.mime_type
|
| 91 |
+
except Exception as e:
|
| 92 |
+
st.error(f"TTS error: {e}")
|
| 93 |
+
return None, None
|
| 94 |
+
|
| 95 |
+
def _convert_pcm_to_wav(raw_bytes: bytes, sample_rate=24000, sample_width=2):
|
| 96 |
+
"""Wrap raw PCM -> WAV header so browsers can play it."""
|
| 97 |
+
import wave, contextlib
|
| 98 |
+
buf = io.BytesIO()
|
| 99 |
+
with contextlib.closing(wave.open(buf, "wb")) as wf:
|
| 100 |
+
wf.setnchannels(1)
|
| 101 |
+
wf.setsampwidth(sample_width)
|
| 102 |
+
wf.setframerate(sample_rate)
|
| 103 |
+
wf.writeframes(raw_bytes)
|
| 104 |
+
return buf.getvalue()
|
| 105 |
+
|
| 106 |
+
def upload_pptx_to_slides(pptx_path: Path) -> str | None:
|
| 107 |
+
"""Uploads the PPTX to Google Slides; returns the Slides URL."""
|
| 108 |
+
if not GOOGLE_CREDENTIALS_JSON:
|
| 109 |
+
st.warning("Service-account JSON not configured βοΈ; skipping Slides upload.")
|
| 110 |
+
return None
|
| 111 |
+
creds = Credentials.from_service_account_file(
|
| 112 |
+
GOOGLE_CREDENTIALS_JSON,
|
| 113 |
+
scopes=["https://www.googleapis.com/auth/drive.file",
|
| 114 |
+
"https://www.googleapis.com/auth/presentations"]
|
| 115 |
+
)
|
| 116 |
+
drive = build("drive", "v3", credentials=creds)
|
| 117 |
+
slides = build("slides", "v1", credentials=creds)
|
| 118 |
+
|
| 119 |
+
file_metadata = {
|
| 120 |
+
"name": f"AI-Slides-{uuid.uuid4().hex[:6]}",
|
| 121 |
+
"mimeType": "application/vnd.google-apps.presentation",
|
| 122 |
+
}
|
| 123 |
+
media = {"mimeType": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
| 124 |
+
"body": open(pptx_path, "rb")}
|
| 125 |
+
drive_file = drive.files().create(body=file_metadata, media_body=media).execute()
|
| 126 |
+
presentation_id = drive_file["id"]
|
| 127 |
+
return f"https://docs.google.com/presentation/d/{presentation_id}"
|
| 128 |
+
|
| 129 |
+
###############################################################################
|
| 130 |
+
# 3. Streamlit UI scaffolding
|
| 131 |
+
###############################################################################
|
| 132 |
+
st.set_page_config(page_title="AI Business Report & Slides", layout="wide")
|
| 133 |
+
st.title("π AI-Generated Report & Presentation")
|
| 134 |
|
| 135 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 136 |
if not api_key:
|
| 137 |
+
st.error("β Set `GEMINI_API_KEY` in the environment.")
|
| 138 |
st.stop()
|
| 139 |
|
| 140 |
+
client = genai.Client(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
+
MODE = st.radio("Choose output:", ["Report", "Presentation", "Both"], horizontal=True)
|
| 143 |
+
|
| 144 |
+
uploaded = st.file_uploader("Upload CSV / XLSX", ["csv", "xlsx"])
|
| 145 |
+
user_ctx = st.text_area("Optional business context / objectives")
|
| 146 |
+
run_btn = st.button("π Generate")
|
| 147 |
+
|
| 148 |
+
###############################################################################
|
| 149 |
+
# 4. Run pipeline
|
| 150 |
+
###############################################################################
|
| 151 |
+
if run_btn:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
if not uploaded:
|
| 153 |
+
st.warning("Upload a dataset first.")
|
| 154 |
st.stop()
|
| 155 |
|
| 156 |
+
# 4.1 Load data
|
| 157 |
try:
|
| 158 |
df = (pd.read_excel(uploaded)
|
| 159 |
if uploaded.name.lower().endswith(".xlsx")
|
| 160 |
else pd.read_csv(uploaded))
|
|
|
|
| 161 |
except Exception as e:
|
| 162 |
st.error(f"Failed to read file: {e}")
|
| 163 |
st.stop()
|
| 164 |
+
st.session_state["dataframe"] = df
|
| 165 |
+
df_hash = _hash_df(df)
|
| 166 |
|
| 167 |
+
# 4.2 Build agents ------------------------------------------------------
|
| 168 |
+
instruction_report = """
|
|
|
|
| 169 |
You are a senior business analyst. Write an executive-level Markdown report
|
| 170 |
covering descriptive statistics, key insights, and recommendations.
|
| 171 |
+
Use placeholder tags for visuals, e.g. <generate_chart: "histogram of sales">.
|
|
|
|
| 172 |
"""
|
| 173 |
report_agent = LlmAgent(
|
| 174 |
name="ReportAgent",
|
| 175 |
model="gemini-2.5-flash",
|
| 176 |
+
instruction=instruction_report,
|
| 177 |
+
description="Writes analytical report",
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
instruction_presentation = f"""
|
| 181 |
+
Create exactly {SLIDE_COUNT} presentation slides in concise Markdown.
|
| 182 |
+
Each slide = one paragraph or bullet block suitable for a slide.
|
| 183 |
+
Preface each slide with **Slide X:** then the content.
|
| 184 |
+
Use <generate_chart: "..."> tags wherever a chart would aid understanding.
|
| 185 |
+
"""
|
| 186 |
+
presentation_agent = LlmAgent(
|
| 187 |
+
name="PresentationAgent",
|
| 188 |
+
model="gemini-2.5-flash",
|
| 189 |
+
instruction=instruction_presentation,
|
| 190 |
+
description="Writes slide deck",
|
| 191 |
)
|
|
|
|
|
|
|
| 192 |
|
| 193 |
+
agents = []
|
| 194 |
+
if MODE in ("Report", "Both"):
|
| 195 |
+
agents.append(report_agent)
|
| 196 |
+
if MODE in ("Presentation", "Both"):
|
| 197 |
+
agents.append(presentation_agent)
|
| 198 |
+
|
| 199 |
+
root_agent = SequentialAgent(name="RootPipeline", sub_agents=agents)
|
| 200 |
+
|
| 201 |
+
# 4.3 Context passed to Gemini
|
| 202 |
ctx = {
|
| 203 |
"dataset_info": {
|
| 204 |
"shape": df.shape,
|
|
|
|
| 210 |
"preview": df.head().to_dict(),
|
| 211 |
}
|
| 212 |
|
| 213 |
+
# 4.4 Run the ADK pipeline --------------------------------------------
|
| 214 |
+
with st.spinner("π‘ LLM reasoningβ¦"):
|
| 215 |
+
svc = InMemorySessionService()
|
| 216 |
+
sid = str(uuid.uuid4())
|
| 217 |
+
awaitable = svc.create_session(app_name="app", user_id="user1", session_id=sid)
|
| 218 |
+
asyncio.run(awaitable)
|
| 219 |
+
runner = Runner(root_agent, "app", svc)
|
| 220 |
+
|
| 221 |
+
final_texts = {}
|
| 222 |
+
async def _run():
|
| 223 |
+
async for ev in runner.run_async("user1", sid,
|
| 224 |
+
new_message=types.Content(
|
| 225 |
+
role="user",
|
| 226 |
+
parts=[types.Part(text=json.dumps(ctx))]
|
| 227 |
+
)):
|
| 228 |
+
if ev.is_final_response():
|
| 229 |
+
final_texts[ev.agent_name] = ev.content.parts[0].text
|
| 230 |
+
asyncio.run(_run())
|
| 231 |
+
|
| 232 |
+
# 4.5 Generate charts once for both outputs ---------------------------
|
| 233 |
+
chart_descs = re.findall(r'<generate_chart:\s*"([^"]+)"\s*>',
|
| 234 |
+
"\n".join(final_texts.values()))
|
| 235 |
+
desc2path = {}
|
| 236 |
+
if chart_descs:
|
| 237 |
+
pandas_agent = _get_pandas_agent(df_hash, api_key)
|
| 238 |
+
for desc in chart_descs:
|
| 239 |
+
if desc in desc2path: # reuse if duplicate
|
| 240 |
+
continue
|
| 241 |
pandas_agent.run(f"Create a {desc} using matplotlib")
|
| 242 |
fig = plt.gcf()
|
| 243 |
+
p = Path(tempfile.gettempdir()) / f"{uuid.uuid4()}.png"
|
| 244 |
+
fig.savefig(p, dpi=300, bbox_inches="tight")
|
| 245 |
plt.close()
|
| 246 |
+
desc2path[desc] = str(p)
|
| 247 |
+
|
| 248 |
+
########################################################################
|
| 249 |
+
# 5. REPORT TAB -------------------------------------------------------
|
| 250 |
+
########################################################################
|
| 251 |
+
if MODE in ("Report", "Both"):
|
| 252 |
+
report_md = final_texts["ReportAgent"]
|
| 253 |
+
for d,p in desc2path.items():
|
| 254 |
+
report_md = re.sub(rf'<generate_chart:\s*"{re.escape(d)}"\s*>',
|
| 255 |
+
f'<img src="{p}" />', report_md)
|
| 256 |
+
|
| 257 |
+
tab_rep = st.tabs(["Report"])[0] if MODE=="Both" else st
|
| 258 |
+
with tab_rep:
|
| 259 |
+
st.markdown("## π Report Preview")
|
| 260 |
+
st.markdown(report_md, unsafe_allow_html=True)
|
| 261 |
+
|
| 262 |
+
# --- PDF build (re-use function from previous iterations omitted
|
| 263 |
+
# here for brevity; paste the last working PDF builder block) ----
|
| 264 |
+
# pdf_bytes = build_pdf(report_md, desc2path, ...) <-- insert
|
| 265 |
+
# st.download_button("Download PDF", pdf_bytes, "report.pdf")
|
| 266 |
+
|
| 267 |
+
########################################################################
|
| 268 |
+
# 6. PRESENTATION TAB -------------------------------------------------
|
| 269 |
+
########################################################################
|
| 270 |
+
if MODE in ("Presentation", "Both"):
|
| 271 |
+
pres_raw = final_texts["PresentationAgent"]
|
| 272 |
+
# Split "Slide X:" markers
|
| 273 |
+
slide_texts = [s.strip() for s in re.split(r'\bSlide\s+\d+:', pres_raw) if s.strip()]
|
| 274 |
+
slide_texts = slide_texts[:SLIDE_COUNT] # enforce 7
|
| 275 |
+
st.session_state.app_state = {
|
| 276 |
+
"steps": slide_texts,
|
| 277 |
+
"current": 0
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
tab_pres = st.tabs(["Presentation"])[1] if MODE=="Both" else st
|
| 281 |
+
with tab_pres:
|
| 282 |
+
st.header("π Slide Preview")
|
| 283 |
+
def render_step(idx, text):
|
| 284 |
+
total = len(st.session_state.app_state['steps'])
|
| 285 |
+
st.markdown(f"### Slide {idx+1} of {total}")
|
| 286 |
+
st.write(text)
|
| 287 |
+
if st.button(f"π Narrate Slide {idx+1}", key=f"tts_{idx}"):
|
| 288 |
+
with st.spinner("Generating narrationβ¦"):
|
| 289 |
+
audio, mime = generate_tts_audio(client, text)
|
| 290 |
+
if audio:
|
| 291 |
+
if 'L16' in mime or 'pcm' in mime:
|
| 292 |
+
wav = _convert_pcm_to_wav(audio)
|
| 293 |
+
st.audio(wav, format="audio/wav")
|
| 294 |
+
else:
|
| 295 |
+
st.audio(audio, format=mime)
|
| 296 |
+
col_l, col_r = st.columns([1,1])
|
| 297 |
+
if col_l.button("β¬
οΈ Back", disabled=st.session_state.app_state['current']==0):
|
| 298 |
+
st.session_state.app_state['current'] -= 1
|
| 299 |
+
if col_r.button("Next β‘οΈ",
|
| 300 |
+
disabled=st.session_state.app_state['current']==len(slide_texts)-1):
|
| 301 |
+
st.session_state.app_state['current'] += 1
|
| 302 |
+
render_step(st.session_state.app_state['current'],
|
| 303 |
+
slide_texts[st.session_state.app_state['current']])
|
| 304 |
+
|
| 305 |
+
# 6.1 PPTX creation -------------------------------------------------
|
| 306 |
+
@st.cache_resource(show_spinner=False)
|
| 307 |
+
def _build_pptx(txts, desc2path):
|
| 308 |
+
prs = Presentation()
|
| 309 |
+
blank = prs.slide_layouts[6]
|
| 310 |
+
for idx, txt in enumerate(txts):
|
| 311 |
+
slide = prs.slides.add_slide(blank)
|
| 312 |
+
left = Inches(0.5); top = Inches(0.5); width = Inches(9); height=Inches(1)
|
| 313 |
+
tx_box = slide.shapes.add_textbox(left, top, width, height)
|
| 314 |
+
tf = tx_box.text_frame
|
| 315 |
+
tf.clear()
|
| 316 |
+
p = tf.paragraphs[0]
|
| 317 |
+
p.text = f"Slide {idx+1}"
|
| 318 |
+
p.font.size = Pt(28)
|
| 319 |
+
p.font.bold = True
|
| 320 |
+
|
| 321 |
+
body = slide.shapes.add_textbox(left, Inches(1.3), width, Inches(4))
|
| 322 |
+
body.text_frame.text = re.sub(r'<.*?>', '', txt) # strip chart tags
|
| 323 |
+
|
| 324 |
+
# embed chart if referenced
|
| 325 |
+
match = re.search(r'<generate_chart:\s*"([^"]+)"\s*>', txt)
|
| 326 |
+
if match:
|
| 327 |
+
path = desc2path.get(match.group(1))
|
| 328 |
+
if path:
|
| 329 |
+
slide.shapes.add_picture(path, Inches(1), Inches(3.5),
|
| 330 |
+
width=Inches(8))
|
| 331 |
+
|
| 332 |
+
out_path = Path(tempfile.gettempdir()) / f"slides_{uuid.uuid4().hex}.pptx"
|
| 333 |
+
prs.save(out_path)
|
| 334 |
+
return out_path
|
| 335 |
+
|
| 336 |
+
pptx_path = _build_pptx(slide_texts, desc2path)
|
| 337 |
+
st.download_button("β¬οΈ Download PPTX", pptx_path.read_bytes(),
|
| 338 |
+
file_name="presentation.pptx",
|
| 339 |
+
mime="application/vnd.openxmlformats-officedocument.presentationml.presentation")
|
| 340 |
+
|
| 341 |
+
if st.button("β¬οΈ Upload to Google Slides"):
|
| 342 |
+
url = upload_pptx_to_slides(pptx_path)
|
| 343 |
+
if url:
|
| 344 |
+
st.success(f"Uploaded! π [Open Slides]({url})")
|
| 345 |
+
|
| 346 |
+
###############################################################################
|
| 347 |
+
# End of file
|
| 348 |
+
###############################################################################
|