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 API configuration 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") # Retrieve emails and save to ChromaDB 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.") # Display chat interface chat_input = st.chat_input("Type your message here...") if chat_input: st.write(f"You said: {chat_input}") # Handle chat input client = chromadb.Client(Settings()) collection = client.get_collection(name="emails") senders = set(doc['metadata']['sender'] for doc in collection.get_all()) # Display senders as chat buttons 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 API configuration 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"]) # Adjust top_k as needed st.write("ChromaDB Query Results:", results) # Debugging line 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) # Directly call the model with the prompt response_text = response.strip() cleaned_response = response_text.replace('User:', '').replace('Assistant:', '').strip() st.write("AI Model Response:", cleaned_response) # Debugging line 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) # Debugging line documents = [] for result in results.get('results', []): documents.extend(result.get('documents', [])) return documents # Streamlit app configuration 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 = [] # Authenticate Google API and retrieve 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.") # Display conversation history for msg in st.session_state.messages: role = msg.get("role", None) with st.chat_message(role): st.write(msg.get("content", "No message")) # Display email data 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("---") # Display email senders as buttons 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] # Display emails from the current sender and handle summarization 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) # Chat input 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}) # Query ChromaDB based on user input documents = query_chromadb(prompt) if documents: st.write("Documents retrieved from ChromaDB:", documents) # Debugging line # Ensure 'documents' are correctly accessed from results 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 # Configure the LangChain API AI71_BASE_URL = "https://api.ai71.ai/v1/" AI71_API_KEY = "ai71-api-1ee3a6dd-28c9-427c-800d-afb198e86a1d" # Initialize the LangChain model llm = ChatOpenAI( model="tiiuae/falcon-180B-chat", api_key=AI71_API_KEY, base_url=AI71_BASE_URL ) # Define your email data emails = [ {"sender": "alice@example.com", "subject": "Meeting Update", "content": "The meeting has been rescheduled to 3 PM."}, {"sender": "bob@example.com", "subject": "Project Deadline", "content": "The project deadline is next Friday."}, # Add more emails as needed ] # Convert email data to a context string 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 # Initialize session state if not already done if 'messages' not in st.session_state: st.session_state.messages = [] # Display existing chat history st.title("Email Analysis with Falcon-180B") for message in st.session_state.messages: with st.chat_message(message['role']): st.write(message['content']) # User input for new message prompt = st.chat_input("What cards are you looking for?") if prompt: # Display user message in chat message container with st.chat_message("user"): st.write(prompt) # Add user message to chat history st.session_state.messages.append({"role": "user", "content": prompt}) # Create context for the model context = create_context(emails) full_prompt = f"{context}\nQuestion: {prompt}" # Query the model response = llm.invoke(full_prompt) # Display the model's response st.subheader("Model Response") st.write(response.content) # Append the model's response to the session state st.session_state.messages.append({"role": "assistant", "content": response.content}) # Clear the input field and rerun the app 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 # Replace with actual import path # Initialize the ChatOpenAI instance 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") # Text input for summarization 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.") # Text input for structuring 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 # Import BeautifulSoup # Scopes for Gmail API 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') # Get plain text from HTML 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.""" # Calculate the time range now = datetime.utcnow() past_time = now - timedelta(hours=hours) # Convert time to RFC 3339 format past_time_str = past_time.strftime('%Y/%m/%d') # Query to get emails newer than the calculated time 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() # Remove HTML tags and CSS from the body 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) # Get emails from the past 3 hours print(json.dumps(emails, indent=2)) if __name__ == '__main__': main()