Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| from openpyxl import load_workbook, Workbook | |
| from datetime import datetime | |
| import uuid | |
| def load_or_create_excel(file_path): | |
| try: | |
| wb = load_workbook(file_path) | |
| except FileNotFoundError: | |
| wb = Workbook() | |
| wb.create_sheet("Users") | |
| wb.create_sheet("GeneratedEmails") | |
| wb.create_sheet("TemplateEmails") | |
| users_sheet = wb["Users"] | |
| users_sheet.append(["UserID", "Name", "Email", "Company", "Role"]) | |
| generated_emails_sheet = wb["GeneratedEmails"] | |
| generated_emails_sheet.append(["EmailID", "UserID", "Subject", "Body", "Timestamp"]) | |
| template_emails_sheet = wb["TemplateEmails"] | |
| template_emails_sheet.append(["TemplateID", "Name", "Subject", "Body"]) | |
| wb.save(file_path) | |
| return wb | |
| # Function to add user to Excel | |
| def add_user_to_excel(file_path, user_data): | |
| wb = load_or_create_excel(file_path) | |
| sheet = wb["Users"] | |
| user_id = str(uuid.uuid4()) | |
| sheet.append([user_id, user_data['name'], user_data['email'], user_data['company'], user_data['role']]) | |
| wb.save(file_path) | |
| return user_id | |
| # Function to save generated email | |
| def save_email_to_excel(file_path, user_id, subject, body): | |
| wb = load_or_create_excel(file_path) | |
| sheet = wb["GeneratedEmails"] | |
| email_id = str(uuid.uuid4()) | |
| sheet.append([email_id, user_id, subject, body, datetime.now().strftime("%Y-%m-%d %H:%M:%S")]) | |
| wb.save(file_path) | |
| # Function to add template email | |
| def add_template_email(file_path, name, subject, body): | |
| wb = load_or_create_excel(file_path) | |
| sheet = wb["TemplateEmails"] | |
| template_id = str(uuid.uuid4()) | |
| sheet.append([template_id, name, subject, body]) | |
| wb.save(file_path) | |
| # Function to get all template emails | |
| def get_template_emails(file_path): | |
| df = pd.read_excel(file_path, sheet_name="TemplateEmails") | |
| return df.to_dict('records') | |
| # Function to generate email using Groq LLM (placeholder) | |
| def generate_email(prompt): | |
| # Implement your Groq LLM email generation logic here | |
| return f"Subject: Generated Email\n\nBody: {prompt}" | |
| def get_user_by_id(file_path, user_id): | |
| wb = load_workbook(file_path) | |
| sheet = wb["Users"] | |
| for row in sheet.iter_rows(min_row=2, values_only=True): | |
| if row[0] == user_id: | |
| return {'name': row[1], 'email': row[2], 'company': row[3], 'role': row[4]} | |
| return None | |
| def update_user_in_excel(file_path, user_id, user_data): | |
| wb = load_workbook(file_path) | |
| sheet = wb["Users"] | |
| for row in sheet.iter_rows(min_row=2): | |
| if row[0].value == user_id: | |
| row[1].value = user_data['name'] | |
| row[2].value = user_data['email'] | |
| row[3].value = user_data['company'] | |
| row[4].value = user_data['role'] | |
| break | |
| wb.save(file_path) | |
| def store_sent_email(file_path, sender_email, receiver_email, subject, body): | |
| wb = load_or_create_excel(file_path) | |
| sheet = wb["GeneratedEmails"] | |
| email_id = str(uuid.uuid4()) | |
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| # Append the sent email details to the sheet | |
| sheet.append([email_id, sender_email, receiver_email, subject, body, timestamp]) | |
| wb.save(file_path) | |
| return email_id |