File size: 7,129 Bytes
8765707
 
 
 
 
 
 
 
9c551fe
92d2e30
 
8765707
92d2e30
8765707
9c551fe
 
 
 
 
 
8765707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92d2e30
8765707
 
 
 
 
 
 
 
 
92d2e30
8765707
 
 
 
 
 
92d2e30
8765707
 
 
 
b464319
8765707
 
 
 
b464319
 
8765707
 
 
 
 
 
 
92d2e30
 
 
8765707
 
 
 
 
 
 
9c551fe
2a8eef7
 
92d2e30
9c551fe
2a8eef7
 
 
 
 
 
 
 
92d2e30
 
 
2a8eef7
 
 
 
9c551fe
8765707
 
 
 
 
 
92d2e30
 
 
 
8765707
 
 
 
 
 
 
 
92d2e30
 
8765707
 
 
 
92d2e30
8765707
 
 
 
 
 
 
 
92d2e30
8765707
92d2e30
8765707
92d2e30
 
8765707
92d2e30
 
 
8765707
 
92d2e30
8765707
 
 
 
92d2e30
8765707
 
 
 
 
 
 
 
 
 
92d2e30
8765707
 
 
 
 
 
 
92d2e30
8765707
92d2e30
8765707
92d2e30
 
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
198
199
200
201
202
import os
import smtplib
import whisper
from email.mime.text import MIMEText
from groq import Groq
from gtts import gTTS
import tempfile
import streamlit as st
from dotenv import load_dotenv
from pydub import AudioSegment

# ==========================
# πŸ” Secrets (stored in .env)
# ==========================
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
EMAIL_ADDRESS = os.getenv("EMAIL_ADDRESS")
EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")

client = Groq(api_key=GROQ_API_KEY)
whisper_model = whisper.load_model("base")

# ==========================
# πŸ“„ MCP Context
# ==========================
def init_context(user_input, recipients, tone="formal"):
    return {
        "user_input": user_input,
        "recipient_list": recipients,
        "email_type": tone,
        "email_content": "",
        "status": ""
    }

# ==========================
# ✍️ Email Generator Agent
# ==========================
def email_generator_agent(context):
    prompt = f"Write a {context['email_type']} email with the following details:\n\n{context['user_input']}"
    try:
        response = client.chat.completions.create(
            model="llama3-8b-8192",
            messages=[{"role": "user", "content": prompt}]
        )
        context["email_content"] = response.choices[0].message.content.strip()
    except Exception as e:
        context["email_content"] = f"Error generating email: {e}"
    return context

# ==========================
# 🌐 Translator Agent
# ==========================
def translate_email(content, target_language):
    prompt = f"Translate the following email into {target_language}:\n\n{content}"
    try:
        response = client.chat.completions.create(
            model="llama3-8b-8192",
            messages=[{"role": "user", "content": prompt}]
        )
        return response.choices[0].message.content.strip()
    except Exception as e:
        return f"Translation failed: {e}"

# ==========================
# πŸ“€ Email Sender Agent
# ==========================
def email_sender_agent(context):
    message = MIMEText(context["email_content"])
    message["Subject"] = "AI Agent Email"
    message["From"] = EMAIL_ADDRESS

    success = 0
    for recipient in context["recipient_list"]:
        message["To"] = recipient
        try:
            with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
                server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
                server.send_message(message)
                success += 1
        except Exception as e:
            st.error(f"❌ Failed to send to {recipient}: {e}")
            print(f"[DEBUG] Email send error for {recipient}: {e}")  # Debug print

    context["status"] = f"βœ… Email sent to {success} recipient(s)."
    return context



# ==========================
# 🧠 TTS and Playback
# ==========================
def speak_and_download(text, lang="en"):
    try:
        tts = gTTS(text=text, lang=lang)
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
            path = fp.name
            tts.save(path)
        return path
    except Exception as e:
        st.error(f"TTS error: {e}")
        return None

# ==========================
# πŸ“œ Whisper Transcriber
# ==========================
def transcribe_audio(file):
    try:
        if isinstance(file, str):
            audio = AudioSegment.from_file(file)
        else:
            if file.name.endswith(".mp3"):
                audio = AudioSegment.from_mp3(file)
            elif file.name.endswith(".wav"):
                audio = AudioSegment.from_wav(file)
            else:
                st.error("Unsupported file type.")
                return ""

        with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
            audio.export(tmp.name, format="wav")
            result = whisper_model.transcribe(tmp.name)
            return result["text"]
    except Exception as e:
        st.error(f"❌ Transcription error: {e}")
        return ""

# ==========================
# πŸš€ Streamlit Interface
# ==========================
st.set_page_config(page_title="Email Generator AI", layout="centered")
st.title("πŸ“§ AI Email Generator with Voice Input")

# Session state init
if "context" not in st.session_state:
    st.session_state.context = None

mode = st.radio("Choose Input Mode", ["Text Input", "Upload Audio File", "Paste Full Email"])
recipient_input = st.text_input("Recipient Emails (comma-separated)", "")
tone = st.text_input("Email Tone (Formal, Friendly, Apologetic, etc.)", "Formal")
recipient_list = [r.strip() for r in recipient_input.split(",") if r.strip()]

if mode == "Text Input":
    user_request = st.text_area("What should the email say?")
    if st.button("Generate Email"):
        st.session_state.context = init_context(user_request, recipient_list, tone)
        st.session_state.context = email_generator_agent(st.session_state.context)

elif mode == "Paste Full Email":
    full_email = st.text_area("Paste your complete email")
    if st.button("Use Email"):
        st.session_state.context = {
            "user_input": "",
            "recipient_list": recipient_list,
            "email_type": "manual",
            "email_content": full_email.strip(),
            "status": ""
        }

elif mode == "Upload Audio File":
    uploaded_audio = st.file_uploader("Upload an audio file (.wav or .mp3)", type=["wav", "mp3"])
    if uploaded_audio and st.button("Transcribe and Generate Email"):
        transcribed = transcribe_audio(uploaded_audio)
        st.success(f"Transcribed Text: {transcribed}")
        st.session_state.context = init_context(transcribed, recipient_list, tone)
        st.session_state.context = email_generator_agent(st.session_state.context)

# ========== Show Email Output ========== #
if st.session_state.context:
    context = st.session_state.context
    st.subheader("πŸ“¨ Generated Email")
    st.text_area("Email Content", context["email_content"], height=200)

    audio_path = speak_and_download(context["email_content"])
    if audio_path:
        st.audio(audio_path)

    # Translation
    if st.checkbox("🌐 Translate Email"):
        lang = st.text_input("Translate to (e.g., Urdu, Spanish, German)")
        lang_map = {
            "german": "de",
            "spanish": "es",
            "urdu": "ur",
            "french": "fr",
            "english": "en"
        }
        target_lang_code = lang_map.get(lang.lower(), "en")

        if lang and st.button("Translate"):
            translated = translate_email(context["email_content"], lang)
            st.success("Translated Email:")
            st.text_area("Translated Email", translated, height=200)
            translated_audio_path = speak_and_download(translated, lang=target_lang_code)
            if translated_audio_path:
                st.audio(translated_audio_path)
            st.session_state.context["email_content"] = translated

    # Email Sending
    if st.button("βœ‰οΈ Send Email"):
        st.session_state.context = email_sender_agent(st.session_state.context)
        st.success(st.session_state.context["status"])