0Learn's picture
Upload 9 files
cf6b61d verified
# app.py
# Filepath: ai-email-assistant/app.py
# Main application entry point
import json
import gradio as gr
from chat import create_chat_interface
from profile_management import create_profile_management_interface
from email_actions import create_email_actions_interface, get_template_summaries
from groq_client import set_api_key
from google_sheets_utils import (
get_or_create_spreadsheet,
get_profile_summaries,
)
import logging
from dotenv import load_dotenv
import os
# Set up logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def load_config():
load_dotenv()
return {
'groq_api_key': os.getenv('GROQ_API_KEY')
}
def main():
logger.debug("Starting the application")
config = load_config()
logger.debug(f"Loaded config: {config}")
api_key = config.get('groq_api_key')
if api_key:
set_api_key(api_key)
logger.debug("API key set successfully")
else:
logger.warning("No API key found in .env file")
spreadsheet = get_or_create_spreadsheet()
if not spreadsheet:
logger.error("Failed to connect to Google Sheets")
gr.Error("Failed to connect to Google Sheets. Please check your credentials and internet connection.")
return
logger.debug("Connected to Google Sheets successfully")
with gr.Blocks() as app:
gr.Markdown("# AI Email Assistant")
# State variables
current_sender = gr.State(None)
current_receiver = gr.State(None)
current_template = gr.State(None)
with gr.Tabs() as tabs:
with gr.Tab("Profile Management"):
with gr.Tabs() as profile_tabs:
with gr.TabItem("Sender Profiles"):
sender_components = create_profile_management_interface(
spreadsheet, "Sender Profiles", current_sender
)
sender_block = sender_components["block"]
with gr.TabItem("Receiver Profiles"):
receiver_components = create_profile_management_interface(
spreadsheet, "Receiver Profiles", current_receiver
)
receiver_block = receiver_components["block"]
with gr.Tab("Email Actions"):
email_actions_components = create_email_actions_interface(
spreadsheet, current_template
)
email_actions_block = email_actions_components["block"]
with gr.Tab("Chat"):
# Get summaries for context
sender_summaries = get_profile_summaries(
spreadsheet.worksheet("Sender Profiles"), "Sender Profile"
)
receiver_summaries = get_profile_summaries(
spreadsheet.worksheet("Receiver Profiles"), "Receiver Profile"
)
template_summaries = get_template_summaries(spreadsheet)
chat_interface = create_chat_interface(
sender_summaries, receiver_summaries, template_summaries,
current_sender, current_receiver, current_template,
sender_components, receiver_components, email_actions_components
)
# Refresh profiles and templates when tabs are selected
def refresh_data(tab: gr.SelectData):
if tab.index == 0: # Profile Management tab
return (
sender_components["refresh_profiles"](),
receiver_components["refresh_profiles"](),
None
)
elif tab.index == 1: # Email Actions tab
return (
None,
None,
email_actions_components["refresh_templates"]()
)
return None, None, None # Return None for all other cases
tabs.select(
refresh_data,
inputs=None,
outputs=[
sender_components["profiles_list"],
receiver_components["profiles_list"],
email_actions_components["templates_list"]
]
)
# Refresh data on first load
app.load(
lambda: (
sender_components["refresh_profiles"](),
receiver_components["refresh_profiles"](),
email_actions_components["refresh_templates"]()
),
inputs=None,
outputs=[
sender_components["profiles_list"],
receiver_components["profiles_list"],
email_actions_components["templates_list"]
]
)
# Update the select event handlers for profile lists and template list
sender_components["profiles_list"].select(
sender_components["load_profile"],
inputs=[
sender_components["name"],
sender_components["email"],
sender_components["position"],
sender_components["company"],
sender_components["context"],
sender_components["profile_id"]
],
outputs=[
sender_components["name"],
sender_components["email"],
sender_components["position"],
sender_components["company"],
sender_components["context"],
sender_components["profile_id"]
]
)
receiver_components["profiles_list"].select(
receiver_components["load_profile"],
inputs=[
receiver_components["name"],
receiver_components["email"],
receiver_components["position"],
receiver_components["company"],
receiver_components["context"],
receiver_components["profile_id"]
],
outputs=[
receiver_components["name"],
receiver_components["email"],
receiver_components["position"],
receiver_components["company"],
receiver_components["context"],
receiver_components["profile_id"]
]
)
email_actions_components["templates_list"].select(
email_actions_components["load_template"],
inputs=[
email_actions_components["t_name"],
email_actions_components["t_type"],
email_actions_components["t_subject"],
email_actions_components["t_body"],
email_actions_components["t_id"]
],
outputs=[
email_actions_components["t_name"],
email_actions_components["t_type"],
email_actions_components["t_subject"],
email_actions_components["t_body"],
email_actions_components["t_id"]
]
)
app.launch()
if __name__ == "__main__":
main()