|
import streamlit as st
|
|
from google.oauth2.credentials import Credentials
|
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
from google.auth.transport.requests import Request
|
|
import os.path
|
|
import pickle
|
|
from googleapiclient.discovery import build
|
|
import base64
|
|
from email.mime.text import MIMEText
|
|
import chromadb
|
|
from chromadb.config import Settings
|
|
from ai71 import ChatOpenAI
|
|
|
|
|
|
AI71_BASE_URL = "https://api.ai71.ai/v1/"
|
|
AI71_API_KEY = "api71-api-9dc6a966-c659-4771-91ac-019ff63acb9b"
|
|
llm = ChatOpenAI(model="tiiuae/falcon-180B-chat", api_key=AI71_API_KEY, base_url=AI71_BASE_URL)
|
|
|
|
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send']
|
|
|
|
def retrieve_emails():
|
|
creds = None
|
|
if os.path.exists('token.pickle'):
|
|
with open('token.pickle', 'rb') as token:
|
|
creds = pickle.load(token)
|
|
if not creds or not creds.valid:
|
|
if creds and creds.expired and creds.refresh_token:
|
|
creds.refresh(Request())
|
|
else:
|
|
flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
|
|
creds = flow.run_local_server(port=0)
|
|
with open('token.pickle', 'wb') as token:
|
|
pickle.dump(creds, token)
|
|
|
|
service = build('gmail', 'v1', credentials=creds)
|
|
results = service.users().messages().list(userId='me', labelIds=['INBOX'], q='newer_than:1h', maxResults=10).execute()
|
|
messages = results.get('messages', [])
|
|
email_data = []
|
|
|
|
if not messages:
|
|
return email_data
|
|
|
|
for msg in messages:
|
|
msg_detail = service.users().messages().get(userId='me', id=msg['id'], format='full').execute()
|
|
headers = msg_detail['payload']['headers']
|
|
sender = None
|
|
subject = None
|
|
for header in headers:
|
|
if header['name'] == 'From':
|
|
sender = header['value']
|
|
if header['name'] == 'Subject':
|
|
subject = header['value']
|
|
|
|
parts = msg_detail['payload'].get('parts', [])
|
|
body = ''
|
|
if parts:
|
|
for part in parts:
|
|
if part['mimeType'] == 'text/plain':
|
|
body = base64.urlsafe_b64decode(part['body']['data']).decode('utf-8')
|
|
break
|
|
else:
|
|
body = base64.urlsafe_b64decode(msg_detail['payload']['body']['data']).decode('utf-8')
|
|
|
|
email_data.append({
|
|
'id': msg['id'],
|
|
'sender': sender,
|
|
'subject': subject,
|
|
'body': body
|
|
})
|
|
|
|
return email_data
|
|
|
|
def save_to_chromadb(data):
|
|
client = chromadb.Client(Settings())
|
|
collection = client.create_collection(name="emails")
|
|
for item in data:
|
|
collection.add(documents=[item['body']], metadatas=[{'sender': item['sender'], 'subject': item['subject']}])
|
|
|
|
def main():
|
|
st.title("Email Management App")
|
|
|
|
|
|
if st.button("Retrieve Emails"):
|
|
email_data = retrieve_emails()
|
|
if email_data:
|
|
save_to_chromadb(email_data)
|
|
st.success("Emails retrieved and saved to ChromaDB.")
|
|
else:
|
|
st.info("No new emails found.")
|
|
|
|
|
|
chat_input = st.chat_input("Type your message here...")
|
|
|
|
if chat_input:
|
|
st.write(f"You said: {chat_input}")
|
|
|
|
|
|
client = chromadb.Client(Settings())
|
|
collection = client.get_collection(name="emails")
|
|
senders = set(doc['metadata']['sender'] for doc in collection.get_all())
|
|
|
|
|
|
if senders:
|
|
for sender in senders:
|
|
if st.button(sender):
|
|
query = f"Tell me more about emails from {sender}"
|
|
response = llm(query)
|
|
st.write(f"Response from AI71 API:\n{response}")
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|
|
|
|
|
|
|
|
///////////import streamlit as st
|
|
from google.oauth2.credentials import Credentials
|
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
from google.auth.transport.requests import Request
|
|
import os.path
|
|
import pickle
|
|
from googleapiclient.discovery import build
|
|
import base64
|
|
from email.mime.text import MIMEText
|
|
import chromadb
|
|
from chromadb.config import Settings
|
|
from langchain_openai import ChatOpenAI
|
|
|
|
|
|
AI71_BASE_URL = "https://api.ai71.ai/v1/"
|
|
AI71_API_KEY = "api71-api-9dc6a966-c659-4771-91ac-019ff63acb9b"
|
|
llm = ChatOpenAI(model="tiiuae/falcon-180B-chat", api_key=AI71_API_KEY, base_url=AI71_BASE_URL)
|
|
|
|
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send']
|
|
|
|
def authenticate_google_api():
|
|
"""Authenticate and return Google API service object."""
|
|
creds = None
|
|
if os.path.exists('token.pickle'):
|
|
with open('token.pickle', 'rb') as token:
|
|
creds = pickle.load(token)
|
|
|
|
if not creds or not creds.valid:
|
|
if creds and creds.expired and creds.refresh_token:
|
|
creds.refresh(Request())
|
|
else:
|
|
flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
|
|
creds = flow.run_local_server(port=0)
|
|
|
|
with open('token.pickle', 'wb') as token:
|
|
pickle.dump(creds, token)
|
|
|
|
return build('gmail', 'v1', credentials=creds)
|
|
|
|
def retrieve_emails(service):
|
|
"""Retrieve emails from Gmail and return a list of email data."""
|
|
results = service.users().messages().list(userId='me', labelIds=['INBOX'], q='newer_than:1h', maxResults=10).execute()
|
|
messages = results.get('messages', [])
|
|
email_data = []
|
|
|
|
if not messages:
|
|
st.write("No new messages.")
|
|
return email_data
|
|
|
|
for msg in messages:
|
|
msg_detail = service.users().messages().get(userId='me', id=msg['id'], format='full').execute()
|
|
headers = msg_detail['payload']['headers']
|
|
sender = next((header['value'] for header in headers if header['name'] == 'From'), None)
|
|
subject = next((header['value'] for header in headers if header['name'] == 'Subject'), None)
|
|
|
|
parts = msg_detail['payload'].get('parts', [])
|
|
body = ''
|
|
if parts:
|
|
for part in parts:
|
|
if part['mimeType'] == 'text/plain':
|
|
body = base64.urlsafe_b64decode(part['body']['data']).decode('utf-8')
|
|
break
|
|
else:
|
|
body = base64.urlsafe_b64decode(msg_detail['payload']['body']['data']).decode('utf-8')
|
|
|
|
email_data.append({
|
|
'id': msg['id'],
|
|
'sender': sender,
|
|
'subject': subject,
|
|
'body': body
|
|
})
|
|
|
|
return email_data
|
|
|
|
def save_to_chromadb(data):
|
|
"""Save email data to ChromaDB collection."""
|
|
client = chromadb.Client(Settings())
|
|
collection_name = "emails"
|
|
|
|
existing_collections = [col.name for col in client.list_collections()]
|
|
if collection_name in existing_collections:
|
|
collection = client.get_collection(name=collection_name)
|
|
st.write(f"Collection '{collection_name}' already exists.")
|
|
else:
|
|
collection = client.create_collection(name=collection_name)
|
|
st.write(f"Created new collection '{collection_name}'.")
|
|
|
|
ids = [item['id'] for item in data]
|
|
documents = [item['body'] for item in data]
|
|
metadatas = [{'sender': item['sender'], 'subject': item['subject']} for item in data]
|
|
|
|
collection.add(ids=ids, documents=documents, metadatas=metadatas)
|
|
st.write(f"Saved {len(data)} emails to ChromaDB.")
|
|
|
|
def get_senders_from_chromadb():
|
|
"""Retrieve a list of unique email senders from ChromaDB."""
|
|
client = chromadb.Client(Settings())
|
|
collection = client.get_collection(name="emails")
|
|
|
|
results = collection.query(query_texts="", n_results=100, include=["metadatas"])
|
|
|
|
st.write("ChromaDB Query Results:", results)
|
|
|
|
senders = {metadata.get('sender') for result in results.get('results', []) for metadata in result.get('metadatas', [])}
|
|
return list(senders)
|
|
|
|
def get_response(prompt):
|
|
"""Get response from AI model."""
|
|
try:
|
|
response = llm(prompt)
|
|
response_text = response.strip()
|
|
cleaned_response = response_text.replace('User:', '').replace('Assistant:', '').strip()
|
|
st.write("AI Model Response:", cleaned_response)
|
|
return cleaned_response
|
|
except Exception as e:
|
|
st.write(f"An error occurred: {e}")
|
|
return "Error: An internal server error occurred."
|
|
|
|
def query_chromadb(prompt):
|
|
"""Query ChromaDB collection with the given prompt."""
|
|
client = chromadb.Client(Settings())
|
|
collection = client.get_collection(name="emails")
|
|
|
|
results = collection.query(
|
|
query_texts=prompt,
|
|
n_results=5,
|
|
include=["metadatas",]
|
|
)
|
|
|
|
st.write("ChromaDB Query Results:", results)
|
|
|
|
documents = []
|
|
for result in results.get('results', []):
|
|
documents.extend(result.get('documents', []))
|
|
|
|
return documents
|
|
|
|
|
|
st.set_page_config(page_title="Chat with Falcon-180B", layout="wide")
|
|
st.title("Chat with Falcon-180B")
|
|
|
|
if 'messages' not in st.session_state:
|
|
st.session_state.messages = []
|
|
if 'emails' not in st.session_state:
|
|
st.session_state.emails = []
|
|
|
|
|
|
if st.button("Retrieve Emails"):
|
|
service = authenticate_google_api()
|
|
emails = retrieve_emails(service)
|
|
st.session_state.emails = emails
|
|
save_to_chromadb(emails)
|
|
st.success("Emails retrieved and saved to ChromaDB.")
|
|
|
|
|
|
for msg in st.session_state.messages:
|
|
role = msg.get("role", None)
|
|
with st.chat_message(role):
|
|
st.write(msg.get("content", "No message"))
|
|
|
|
|
|
if st.session_state.emails:
|
|
st.subheader("Email Data")
|
|
for email in st.session_state.emails:
|
|
st.write(f"**Sender:** {email['sender']}")
|
|
st.write(f"**Subject:** {email['subject']}")
|
|
st.write(f"**Body:** {email['body']}")
|
|
st.write("---")
|
|
|
|
|
|
if st.session_state.emails:
|
|
st.sidebar.header("Email Senders")
|
|
senders = get_senders_from_chromadb()
|
|
for i, sender in enumerate(senders):
|
|
if st.sidebar.button(sender, key=f"button_{i}"):
|
|
st.session_state.current_sender = sender
|
|
st.session_state.current_sender_emails = [e for e in st.session_state.emails if e['sender'] == sender]
|
|
|
|
|
|
if 'current_sender_emails' in st.session_state:
|
|
st.write(f"Emails from {st.session_state.current_sender}:")
|
|
for email in st.session_state.current_sender_emails:
|
|
if st.button(f"Summarize email: {email['subject']}", key=f"summarize_{email['id']}"):
|
|
summary = get_response(f"Summarize the following email: {email['body']}")
|
|
st.write(summary)
|
|
|
|
|
|
prompt = st.chat_input("Ask a question or type 'summarize' to summarize emails.")
|
|
|
|
if prompt:
|
|
if prompt.lower() == 'summarize':
|
|
summaries = [{'subject': email['subject'], 'summary': get_response(f"Summarize the following email: {email['body']}")} for email in st.session_state.emails]
|
|
st.write("Summaries:")
|
|
for summary in summaries:
|
|
st.write(f"Subject: {summary['subject']}")
|
|
st.write(f"Summary: {summary['summary']}")
|
|
else:
|
|
with st.chat_message("user"):
|
|
st.write(prompt)
|
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
|
|
documents = query_chromadb(prompt)
|
|
|
|
if documents:
|
|
st.write("Documents retrieved from ChromaDB:", documents)
|
|
|
|
|
|
responses = [get_response(f"Analyze the following information: {doc}") for doc in documents]
|
|
|
|
for response in responses:
|
|
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
st.rerun()
|
|
/////////////////////
|
|
import streamlit as st
|
|
from langchain_openai import ChatOpenAI
|
|
import chromadb
|
|
import streamlit as st
|
|
|
|
|
|
AI71_BASE_URL = "https://api.ai71.ai/v1/"
|
|
AI71_API_KEY = "ai71-api-1ee3a6dd-28c9-427c-800d-afb198e86a1d"
|
|
|
|
|
|
llm = ChatOpenAI(
|
|
model="tiiuae/falcon-180B-chat",
|
|
api_key=AI71_API_KEY,
|
|
base_url=AI71_BASE_URL
|
|
)
|
|
|
|
|
|
emails = [
|
|
{"sender": "[email protected]", "subject": "Meeting Update", "content": "The meeting has been rescheduled to 3 PM."},
|
|
{"sender": "[email protected]", "subject": "Project Deadline", "content": "The project deadline is next Friday."},
|
|
|
|
]
|
|
|
|
|
|
def create_context(emails):
|
|
context = "Here is the email data:\n\n"
|
|
for email in emails:
|
|
context += f"From: {email['sender']}\nSubject: {email['subject']}\nContent: {email['content']}\n\n"
|
|
return context
|
|
|
|
|
|
if 'messages' not in st.session_state:
|
|
st.session_state.messages = []
|
|
|
|
|
|
st.title("Email Analysis with Falcon-180B")
|
|
|
|
for message in st.session_state.messages:
|
|
with st.chat_message(message['role']):
|
|
st.write(message['content'])
|
|
|
|
|
|
prompt = st.chat_input("What cards are you looking for?")
|
|
|
|
if prompt:
|
|
|
|
with st.chat_message("user"):
|
|
st.write(prompt)
|
|
|
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
|
|
context = create_context(emails)
|
|
full_prompt = f"{context}\nQuestion: {prompt}"
|
|
|
|
|
|
response = llm.invoke(full_prompt)
|
|
|
|
|
|
st.subheader("Model Response")
|
|
st.write(response.content)
|
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": response.content})
|
|
|
|
|
|
st.rerun()
|
|
|
|
/////
|
|
import os
|
|
import pickle
|
|
import base64
|
|
import json
|
|
from datetime import datetime, timedelta
|
|
from google.auth.transport.requests import Request
|
|
from google.oauth2.credentials import Credentials
|
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
from googleapiclient.discovery import build
|
|
from langchain_openai import ChatOpenAI
|
|
import streamlit as st
|
|
|
|
|
|
|
|
AI71_BASE_URL = "https://api.ai71.ai/v1/"
|
|
AI71_API_KEY = "ai71-api-1505741d-83e9-4ee9-91ea-a331d47a4680"
|
|
llm = ChatOpenAI(model="tiiuae/falcon-180B-chat", api_key=AI71_API_KEY, base_url=AI71_BASE_URL)
|
|
|
|
def summarize_text(text):
|
|
try:
|
|
response = llm.invoke(input=f"Please summarize the following text:\n{text}")
|
|
summary = response.content.strip()
|
|
return summary
|
|
except Exception as e:
|
|
st.error(f"An error occurred while summarizing: {e}")
|
|
return None
|
|
|
|
def structure_text(text):
|
|
try:
|
|
response = llm.invoke(input=f"Please structure the following text:\n{text}")
|
|
structured_text = response.content.strip()
|
|
return structured_text
|
|
except Exception as e:
|
|
st.error(f"An error occurred while structuring: {e}")
|
|
return None
|
|
|
|
|
|
st.title("Text Summarizer and Structurer")
|
|
|
|
|
|
st.header("Summarize Text")
|
|
text_to_summarize = st.text_area("Enter text to summarize", height=150)
|
|
if st.button("Summarize"):
|
|
if text_to_summarize:
|
|
summary = summarize_text(text_to_summarize)
|
|
st.subheader("Summary")
|
|
st.write(summary)
|
|
else:
|
|
st.error("Please enter some text to summarize.")
|
|
|
|
|
|
st.header("Structure Text")
|
|
text_to_structure = st.text_area("Enter text to structure", height=150)
|
|
if st.button("Structure"):
|
|
if text_to_structure:
|
|
structured_text = structure_text(text_to_structure)
|
|
st.subheader("Structured Text")
|
|
st.write(structured_text)
|
|
else:
|
|
st.error("Please enter some text to structure.")
|
|
|
|
|
|
|
|
|
|
|
|
//////////////
|
|
import os
|
|
import pickle
|
|
import base64
|
|
import re
|
|
import json
|
|
from datetime import datetime, timedelta
|
|
from google.auth.transport.requests import Request
|
|
from google.oauth2.credentials import Credentials
|
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
from googleapiclient.discovery import build
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
|
|
|
|
def authenticate_google_api():
|
|
"""Authenticate and return Google API service object."""
|
|
creds = None
|
|
if os.path.exists('token.pickle'):
|
|
with open('token.pickle', 'rb') as token:
|
|
creds = pickle.load(token)
|
|
|
|
if not creds or not creds.valid:
|
|
if creds and creds.expired and creds.refresh_token:
|
|
creds.refresh(Request())
|
|
else:
|
|
flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
|
|
creds = flow.run_local_server(port=0)
|
|
|
|
with open('token.pickle', 'wb') as token:
|
|
pickle.dump(creds, token)
|
|
|
|
return build('gmail', 'v1', credentials=creds)
|
|
|
|
def remove_html_and_css(text):
|
|
"""Remove HTML tags and CSS from the text."""
|
|
soup = BeautifulSoup(text, 'html.parser')
|
|
|
|
plain_text = soup.get_text()
|
|
return plain_text
|
|
|
|
def retrieve_emails(service, hours):
|
|
"""Retrieve emails from Gmail within the past 'hours' and return a list of email data."""
|
|
|
|
now = datetime.utcnow()
|
|
past_time = now - timedelta(hours=hours)
|
|
|
|
|
|
past_time_str = past_time.strftime('%Y/%m/%d')
|
|
|
|
|
|
query = f'newer_than:{hours}h'
|
|
|
|
results = service.users().messages().list(
|
|
userId='me',
|
|
labelIds=['INBOX'],
|
|
q=query,
|
|
maxResults=10
|
|
).execute()
|
|
|
|
messages = results.get('messages', [])
|
|
email_data = []
|
|
|
|
for message in messages:
|
|
msg = service.users().messages().get(userId='me', id=message['id']).execute()
|
|
payload = msg['payload']
|
|
headers = payload['headers']
|
|
|
|
subject = ""
|
|
sender = ""
|
|
for header in headers:
|
|
if header['name'] == 'Subject':
|
|
subject = header['value']
|
|
if header['name'] == 'From':
|
|
sender = header['value']
|
|
|
|
body = ""
|
|
if 'parts' in payload:
|
|
parts = payload['parts']
|
|
for part in parts:
|
|
data = part['body'].get('data')
|
|
if data:
|
|
body += base64.urlsafe_b64decode(data).decode()
|
|
else:
|
|
data = payload['body'].get('data')
|
|
if data:
|
|
body += base64.urlsafe_b64decode(data).decode()
|
|
|
|
|
|
body = remove_html_and_css(body)
|
|
|
|
email_data.append({
|
|
'sender': sender,
|
|
'subject': subject,
|
|
'content': body
|
|
})
|
|
|
|
return email_data
|
|
|
|
def main():
|
|
service = authenticate_google_api()
|
|
emails = retrieve_emails(service, hours=3)
|
|
print(json.dumps(emails, indent=2))
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|