import gradio as gr
from workflow import (
MicroSiteGenerator,
) # Make sure workflow.py is in the same directory or accessible
from agno.workflow import RunEvent
import os
from dotenv import load_dotenv
import traceback # Import traceback for detailed error logging
# Load environment variables from .env file, if present
load_dotenv()
# Instantiate the workflow
# This will also initialize the agents defined in the workflow.
microsite_workflow: MicroSiteGenerator = None
try:
microsite_workflow = MicroSiteGenerator()
except Exception as e:
print(f"Error initializing MicroSiteGenerator: {e}")
traceback.print_exc()
# Gradio UI will show a message if microsite_workflow is None
def generate_microsite_app(audio_file_obj, audio_format_str, use_cache_bool):
"""
Gradio function to process audio and generate a microsite using the local workflow.
audio_file_obj: Output from gr.Audio (type="filepath").
audio_format_str: The user-selected audio format.
use_cache_bool: Boolean indicating whether to use the transcription cache.
"""
if microsite_workflow is None:
return (
"Critical Error: Workflow failed to initialize. Check console logs.",
"App is not functional. Please ensure all configurations (like API keys) are correctly set.",
)
if audio_file_obj is None:
return "Status: Idle", "Please upload an audio file to begin. đ¤"
audio_source_path = audio_file_obj # This is already the path string
processing_log_entries = ["đ Starting microsite generation..."]
final_result_markdown = "âŗ Processing... please wait."
try:
# The run method is a generator. We iterate to get the final result.
for response in microsite_workflow.run(
audio_source=audio_source_path,
audio_format=audio_format_str.lower(), # Ensure format is lowercase
):
processing_log_entries.append(f"đ Workflow event: {response.event.value}")
if response.event == RunEvent.workflow_completed:
content = response.content # This is site_details from the workflow
if isinstance(content, dict): # Expected site_details dictionary
if content.get("success"):
site_url = content.get("site", {}).get("url")
site_name = content.get("site", {}).get("name", "N/A")
admin_url = content.get("site", {}).get("admin_url", "#")
if site_url:
final_result_markdown = (
f"đ **Microsite '{site_name}' Deployed!** đ\n\n"
f"đ **Access it here:** [{site_url}]({site_url})\n\n"
f" Admin URL: {admin_url}âšī¸ Deployment Details (Admin Link)
"
f"
{escaped_description}
WORKFLOW INITIALIZATION FAILED. Please check console logs for errors. API keys might be missing or other configurations might be incorrect.
" else: workflow_desc_html = "Workflow description not available.
" app_title = "MicrositePilot đī¸âĄī¸đ" app_intro_markdown = f""" {env_warning_html} Welcome to **MicrositePilot**! Upload a product demo call recording (audio file). The AI will transcribe it, extract key information, and generate a personalized recap microsite, automatically deployed to Netlify. {workflow_desc_html} """ custom_css = """ body { font-family: 'Inter', sans-serif; } .gradio-container { max-width: 900px !important; margin: auto !important; } footer { display: none !important; } /* Hide default Gradio footer */ h1 { text-align: center; } .gr-button { box-shadow: 0 1px 3px 0 rgba(0,0,0,.1), 0 1px 2px 0 rgba(0,0,0,.06); } """ with gr.Blocks(theme="dark", css=custom_css) as demo: gr.Markdown(f"Note: Example audio file 'Listen to an A.I. sales rep cold call (and close) a prospect. #ai #sales.mp3' not found. Examples disabled.
" ) submit_button.click( fn=generate_microsite_app, inputs=[audio_input, audio_format_input, cache_checkbox], outputs=[log_output, microsite_link_output], api_name="generate_microsite", ) if __name__ == "__main__": if microsite_workflow is None: print("CRITICAL: MicroSiteGenerator workflow failed to initialize. The Gradio app might not function correctly.") print("Please check for errors above, ensure API keys (e.g., GOOGLE_API_KEY, NETLIFY_PERSONAL_ACCESS_TOKEN) are set in your .env file or environment, and all dependencies are installed.") else: print("MicroSiteGenerator workflow initialized successfully.") if not os.getenv("NETLIFY_PERSONAL_ACCESS_TOKEN"): print("CONSOLE REMINDER: NETLIFY_PERSONAL_ACCESS_TOKEN is not set. Deployment to Netlify will fail.") if not os.getenv("GOOGLE_API_KEY"): print("CONSOLE REMINDER: GOOGLE_API_KEY is not set. AI agent calls may fail.") print("Gradio app starting...") demo.launch()