File size: 7,258 Bytes
971b0ff
 
 
 
cf6b61d
971b0ff
 
cf6b61d
971b0ff
 
cf6b61d
 
 
 
 
 
 
 
 
 
 
971b0ff
cf6b61d
 
 
 
 
971b0ff
 
cf6b61d
 
 
 
 
971b0ff
 
cf6b61d
 
 
 
 
 
 
 
 
971b0ff
cf6b61d
971b0ff
 
 
 
 
 
 
 
 
cf6b61d
971b0ff
cf6b61d
 
 
 
 
 
 
 
 
 
 
 
971b0ff
 
cf6b61d
 
 
 
971b0ff
 
 
cf6b61d
 
 
 
 
 
 
971b0ff
 
 
 
 
 
cf6b61d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
971b0ff
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
# 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()