Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,15 +5,14 @@ from email.mime.text import MIMEText
|
|
| 5 |
from groq import Groq
|
| 6 |
from gtts import gTTS
|
| 7 |
import tempfile
|
| 8 |
-
import pygame
|
| 9 |
import streamlit as st
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
-
import
|
|
|
|
| 12 |
# ==========================
|
| 13 |
-
# π Secrets (
|
| 14 |
# ==========================
|
| 15 |
load_dotenv()
|
| 16 |
-
|
| 17 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 18 |
EMAIL_ADDRESS = os.getenv("EMAIL_ADDRESS")
|
| 19 |
EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")
|
|
@@ -58,8 +57,7 @@ def translate_email(content, target_language):
|
|
| 58 |
model="llama3-8b-8192",
|
| 59 |
messages=[{"role": "user", "content": prompt}]
|
| 60 |
)
|
| 61 |
-
|
| 62 |
-
return translated
|
| 63 |
except Exception as e:
|
| 64 |
return f"Translation failed: {e}"
|
| 65 |
|
|
@@ -67,19 +65,16 @@ def translate_email(content, target_language):
|
|
| 67 |
# π€ Email Sender Agent
|
| 68 |
# ==========================
|
| 69 |
def email_sender_agent(context):
|
| 70 |
-
email_address = os.environ["EMAIL_ADDRESS"]
|
| 71 |
-
email_password = os.environ["EMAIL_PASSWORD"]
|
| 72 |
-
|
| 73 |
message = MIMEText(context["email_content"])
|
| 74 |
message["Subject"] = "AI Agent Email"
|
| 75 |
-
message["From"] =
|
| 76 |
|
| 77 |
success = 0
|
| 78 |
for recipient in context["recipient_list"]:
|
| 79 |
message["To"] = recipient
|
| 80 |
try:
|
| 81 |
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
|
| 82 |
-
server.login(
|
| 83 |
server.send_message(message)
|
| 84 |
success += 1
|
| 85 |
except Exception as e:
|
|
@@ -95,9 +90,9 @@ def speak_and_download(text, lang="en"):
|
|
| 95 |
try:
|
| 96 |
tts = gTTS(text=text, lang=lang)
|
| 97 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 98 |
-
|
| 99 |
-
tts.save(
|
| 100 |
-
return
|
| 101 |
except Exception as e:
|
| 102 |
st.error(f"TTS error: {e}")
|
| 103 |
return None
|
|
@@ -105,17 +100,11 @@ def speak_and_download(text, lang="en"):
|
|
| 105 |
# ==========================
|
| 106 |
# π Whisper Transcriber
|
| 107 |
# ==========================
|
| 108 |
-
from pydub import AudioSegment
|
| 109 |
-
|
| 110 |
def transcribe_audio(file):
|
| 111 |
try:
|
| 112 |
-
# Handle if file is a path (str)
|
| 113 |
if isinstance(file, str):
|
| 114 |
-
|
| 115 |
-
audio = AudioSegment.from_file(file_path)
|
| 116 |
else:
|
| 117 |
-
# Handle file-like object from uploader
|
| 118 |
-
file_path = file.name
|
| 119 |
if file.name.endswith(".mp3"):
|
| 120 |
audio = AudioSegment.from_mp3(file)
|
| 121 |
elif file.name.endswith(".wav"):
|
|
@@ -124,11 +113,10 @@ def transcribe_audio(file):
|
|
| 124 |
st.error("Unsupported file type.")
|
| 125 |
return ""
|
| 126 |
|
| 127 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as
|
| 128 |
-
audio.export(
|
| 129 |
-
result = whisper_model.transcribe(
|
| 130 |
return result["text"]
|
| 131 |
-
|
| 132 |
except Exception as e:
|
| 133 |
st.error(f"β Transcription error: {e}")
|
| 134 |
return ""
|
|
@@ -139,24 +127,25 @@ def transcribe_audio(file):
|
|
| 139 |
st.set_page_config(page_title="Email Generator AI", layout="centered")
|
| 140 |
st.title("π§ AI Email Generator with Voice Input")
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
mode = st.radio("Choose Input Mode", ["Text Input", "Upload Audio File", "Paste Full Email"])
|
| 143 |
recipient_input = st.text_input("Recipient Emails (comma-separated)", "")
|
| 144 |
tone = st.text_input("Email Tone (Formal, Friendly, Apologetic, etc.)", "Formal")
|
| 145 |
-
|
| 146 |
recipient_list = [r.strip() for r in recipient_input.split(",") if r.strip()]
|
| 147 |
|
| 148 |
-
context = None
|
| 149 |
-
|
| 150 |
if mode == "Text Input":
|
| 151 |
user_request = st.text_area("What should the email say?")
|
| 152 |
if st.button("Generate Email"):
|
| 153 |
-
context = init_context(user_request, recipient_list, tone)
|
| 154 |
-
context = email_generator_agent(context)
|
| 155 |
|
| 156 |
elif mode == "Paste Full Email":
|
| 157 |
full_email = st.text_area("Paste your complete email")
|
| 158 |
if st.button("Use Email"):
|
| 159 |
-
context = {
|
| 160 |
"user_input": "",
|
| 161 |
"recipient_list": recipient_list,
|
| 162 |
"email_type": "manual",
|
|
@@ -165,24 +154,24 @@ elif mode == "Paste Full Email":
|
|
| 165 |
}
|
| 166 |
|
| 167 |
elif mode == "Upload Audio File":
|
| 168 |
-
uploaded_audio = st.file_uploader("Upload
|
| 169 |
if uploaded_audio and st.button("Transcribe and Generate Email"):
|
| 170 |
-
|
| 171 |
-
tmp.write(uploaded_audio.read())
|
| 172 |
-
tmp_path = tmp.name
|
| 173 |
-
transcribed = transcribe_audio(tmp_path)
|
| 174 |
st.success(f"Transcribed Text: {transcribed}")
|
| 175 |
-
context = init_context(transcribed, recipient_list, tone)
|
| 176 |
-
context = email_generator_agent(context)
|
| 177 |
|
| 178 |
-
|
|
|
|
|
|
|
| 179 |
st.subheader("π¨ Generated Email")
|
| 180 |
st.text_area("Email Content", context["email_content"], height=200)
|
| 181 |
-
|
| 182 |
audio_path = speak_and_download(context["email_content"])
|
| 183 |
if audio_path:
|
| 184 |
st.audio(audio_path)
|
| 185 |
|
|
|
|
| 186 |
if st.checkbox("π Translate Email"):
|
| 187 |
lang = st.text_input("Translate to (e.g., Urdu, Spanish, German)")
|
| 188 |
lang_map = {
|
|
@@ -193,6 +182,7 @@ if context:
|
|
| 193 |
"english": "en"
|
| 194 |
}
|
| 195 |
target_lang_code = lang_map.get(lang.lower(), "en")
|
|
|
|
| 196 |
if lang and st.button("Translate"):
|
| 197 |
translated = translate_email(context["email_content"], lang)
|
| 198 |
st.success("Translated Email:")
|
|
@@ -200,8 +190,9 @@ if context:
|
|
| 200 |
translated_audio_path = speak_and_download(translated, lang=target_lang_code)
|
| 201 |
if translated_audio_path:
|
| 202 |
st.audio(translated_audio_path)
|
| 203 |
-
context["email_content"] = translated
|
| 204 |
|
|
|
|
| 205 |
if st.button("βοΈ Send Email"):
|
| 206 |
-
context = email_sender_agent(context)
|
| 207 |
-
st.success(context["status"])
|
|
|
|
| 5 |
from groq import Groq
|
| 6 |
from gtts import gTTS
|
| 7 |
import tempfile
|
|
|
|
| 8 |
import streamlit as st
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
+
from pydub import AudioSegment
|
| 11 |
+
|
| 12 |
# ==========================
|
| 13 |
+
# π Secrets (stored in .env)
|
| 14 |
# ==========================
|
| 15 |
load_dotenv()
|
|
|
|
| 16 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 17 |
EMAIL_ADDRESS = os.getenv("EMAIL_ADDRESS")
|
| 18 |
EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")
|
|
|
|
| 57 |
model="llama3-8b-8192",
|
| 58 |
messages=[{"role": "user", "content": prompt}]
|
| 59 |
)
|
| 60 |
+
return response.choices[0].message.content.strip()
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
return f"Translation failed: {e}"
|
| 63 |
|
|
|
|
| 65 |
# π€ Email Sender Agent
|
| 66 |
# ==========================
|
| 67 |
def email_sender_agent(context):
|
|
|
|
|
|
|
|
|
|
| 68 |
message = MIMEText(context["email_content"])
|
| 69 |
message["Subject"] = "AI Agent Email"
|
| 70 |
+
message["From"] = EMAIL_ADDRESS
|
| 71 |
|
| 72 |
success = 0
|
| 73 |
for recipient in context["recipient_list"]:
|
| 74 |
message["To"] = recipient
|
| 75 |
try:
|
| 76 |
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
|
| 77 |
+
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
|
| 78 |
server.send_message(message)
|
| 79 |
success += 1
|
| 80 |
except Exception as e:
|
|
|
|
| 90 |
try:
|
| 91 |
tts = gTTS(text=text, lang=lang)
|
| 92 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 93 |
+
path = fp.name
|
| 94 |
+
tts.save(path)
|
| 95 |
+
return path
|
| 96 |
except Exception as e:
|
| 97 |
st.error(f"TTS error: {e}")
|
| 98 |
return None
|
|
|
|
| 100 |
# ==========================
|
| 101 |
# π Whisper Transcriber
|
| 102 |
# ==========================
|
|
|
|
|
|
|
| 103 |
def transcribe_audio(file):
|
| 104 |
try:
|
|
|
|
| 105 |
if isinstance(file, str):
|
| 106 |
+
audio = AudioSegment.from_file(file)
|
|
|
|
| 107 |
else:
|
|
|
|
|
|
|
| 108 |
if file.name.endswith(".mp3"):
|
| 109 |
audio = AudioSegment.from_mp3(file)
|
| 110 |
elif file.name.endswith(".wav"):
|
|
|
|
| 113 |
st.error("Unsupported file type.")
|
| 114 |
return ""
|
| 115 |
|
| 116 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 117 |
+
audio.export(tmp.name, format="wav")
|
| 118 |
+
result = whisper_model.transcribe(tmp.name)
|
| 119 |
return result["text"]
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
st.error(f"β Transcription error: {e}")
|
| 122 |
return ""
|
|
|
|
| 127 |
st.set_page_config(page_title="Email Generator AI", layout="centered")
|
| 128 |
st.title("π§ AI Email Generator with Voice Input")
|
| 129 |
|
| 130 |
+
# Session state init
|
| 131 |
+
if "context" not in st.session_state:
|
| 132 |
+
st.session_state.context = None
|
| 133 |
+
|
| 134 |
mode = st.radio("Choose Input Mode", ["Text Input", "Upload Audio File", "Paste Full Email"])
|
| 135 |
recipient_input = st.text_input("Recipient Emails (comma-separated)", "")
|
| 136 |
tone = st.text_input("Email Tone (Formal, Friendly, Apologetic, etc.)", "Formal")
|
|
|
|
| 137 |
recipient_list = [r.strip() for r in recipient_input.split(",") if r.strip()]
|
| 138 |
|
|
|
|
|
|
|
| 139 |
if mode == "Text Input":
|
| 140 |
user_request = st.text_area("What should the email say?")
|
| 141 |
if st.button("Generate Email"):
|
| 142 |
+
st.session_state.context = init_context(user_request, recipient_list, tone)
|
| 143 |
+
st.session_state.context = email_generator_agent(st.session_state.context)
|
| 144 |
|
| 145 |
elif mode == "Paste Full Email":
|
| 146 |
full_email = st.text_area("Paste your complete email")
|
| 147 |
if st.button("Use Email"):
|
| 148 |
+
st.session_state.context = {
|
| 149 |
"user_input": "",
|
| 150 |
"recipient_list": recipient_list,
|
| 151 |
"email_type": "manual",
|
|
|
|
| 154 |
}
|
| 155 |
|
| 156 |
elif mode == "Upload Audio File":
|
| 157 |
+
uploaded_audio = st.file_uploader("Upload an audio file (.wav or .mp3)", type=["wav", "mp3"])
|
| 158 |
if uploaded_audio and st.button("Transcribe and Generate Email"):
|
| 159 |
+
transcribed = transcribe_audio(uploaded_audio)
|
|
|
|
|
|
|
|
|
|
| 160 |
st.success(f"Transcribed Text: {transcribed}")
|
| 161 |
+
st.session_state.context = init_context(transcribed, recipient_list, tone)
|
| 162 |
+
st.session_state.context = email_generator_agent(st.session_state.context)
|
| 163 |
|
| 164 |
+
# ========== Show Email Output ========== #
|
| 165 |
+
if st.session_state.context:
|
| 166 |
+
context = st.session_state.context
|
| 167 |
st.subheader("π¨ Generated Email")
|
| 168 |
st.text_area("Email Content", context["email_content"], height=200)
|
| 169 |
+
|
| 170 |
audio_path = speak_and_download(context["email_content"])
|
| 171 |
if audio_path:
|
| 172 |
st.audio(audio_path)
|
| 173 |
|
| 174 |
+
# Translation
|
| 175 |
if st.checkbox("π Translate Email"):
|
| 176 |
lang = st.text_input("Translate to (e.g., Urdu, Spanish, German)")
|
| 177 |
lang_map = {
|
|
|
|
| 182 |
"english": "en"
|
| 183 |
}
|
| 184 |
target_lang_code = lang_map.get(lang.lower(), "en")
|
| 185 |
+
|
| 186 |
if lang and st.button("Translate"):
|
| 187 |
translated = translate_email(context["email_content"], lang)
|
| 188 |
st.success("Translated Email:")
|
|
|
|
| 190 |
translated_audio_path = speak_and_download(translated, lang=target_lang_code)
|
| 191 |
if translated_audio_path:
|
| 192 |
st.audio(translated_audio_path)
|
| 193 |
+
st.session_state.context["email_content"] = translated
|
| 194 |
|
| 195 |
+
# Email Sending
|
| 196 |
if st.button("βοΈ Send Email"):
|
| 197 |
+
st.session_state.context = email_sender_agent(st.session_state.context)
|
| 198 |
+
st.success(st.session_state.context["status"])
|