ayaht commited on
Commit
14adefd
·
verified ·
1 Parent(s): d1c91e2

Upload 6 files

Browse files
Files changed (6) hide show
  1. app.py +250 -0
  2. client_secret.json +1 -0
  3. retrieve_email.py +573 -0
  4. sound.mp3 +0 -0
  5. sound.wav +0 -0
  6. token.pickle +3 -0
app.py ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pickle
3
+ import base64
4
+ import json
5
+ import re
6
+ import time
7
+ from datetime import datetime, timedelta
8
+ from google.auth.transport.requests import Request
9
+ from google.oauth2.credentials import Credentials
10
+ from google_auth_oauthlib.flow import InstalledAppFlow
11
+ from googleapiclient.discovery import build
12
+ from langchain_openai import ChatOpenAI
13
+ import streamlit as st
14
+ from bs4 import BeautifulSoup
15
+ from pydub import AudioSegment
16
+ from pydub.playback import play
17
+ import simpleaudio as sa # Import BeautifulSoup
18
+
19
+ # Initialize the ChatOpenAI instance
20
+ AI71_BASE_URL = "https://api.ai71.ai/v1/"
21
+ AI71_API_KEY = "ai71-api-1505741d-83e9-4ee9-91ea-a331d47a4680"
22
+ llm = ChatOpenAI(model="tiiuae/falcon-180B-chat", api_key=AI71_API_KEY, base_url=AI71_BASE_URL)
23
+ SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send']
24
+
25
+
26
+ def remove_unwanted_text(text):
27
+ """Remove unwanted text from the language model's response."""
28
+ return re.sub(r'User:\s*$', '', text).strip()
29
+
30
+ def chunk_text(text, max_length):
31
+ """Chunk long text into smaller parts."""
32
+ chunks = []
33
+ while len(text) > max_length:
34
+ chunk = text[:max_length]
35
+ last_boundary = chunk.rfind('. ')
36
+ if last_boundary == -1:
37
+ last_boundary = chunk.rfind('\n')
38
+ if last_boundary == -1:
39
+ last_boundary = len(chunk)
40
+ chunks.append(text[:last_boundary+1])
41
+ text = text[last_boundary+1:]
42
+ if text:
43
+ chunks.append(text)
44
+ return chunks
45
+
46
+ def summarize_text(text, model):
47
+ """Summarize text using the language model."""
48
+ chunks = chunk_text(text, max_length=1000) # Adjust max_length according to model's context
49
+ summaries = [model.invoke(input=f"Summarize this text:\n{chunk}").content for chunk in chunks]
50
+ combined_summary = ' '.join(summaries)
51
+ final_summary = model.invoke(input=f"Organize this combined text:\n{combined_summary}").content
52
+ return remove_unwanted_text(final_summary)
53
+
54
+ def structure_text(text):
55
+ try:
56
+ cleaned_text = text
57
+ structured_text = summarize_text(cleaned_text, llm)
58
+ return structured_text
59
+ except Exception as e:
60
+ st.error(f"An error occurred while structuring: {e}")
61
+ return None
62
+
63
+ def authenticate_google_api():
64
+ """Authenticate and return Google API service object."""
65
+ creds = None
66
+ if os.path.exists('token.pickle'):
67
+ with open('token.pickle', 'rb') as token:
68
+ creds = pickle.load(token)
69
+
70
+ if not creds or not creds.valid:
71
+ if creds and creds.expired and creds.refresh_token:
72
+ creds.refresh(Request())
73
+ else:
74
+ flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
75
+ creds = flow.run_local_server(port=0)
76
+
77
+ with open('token.pickle', 'wb') as token:
78
+ pickle.dump(creds, token)
79
+
80
+ return build('gmail', 'v1', credentials=creds)
81
+
82
+ def remove_html_and_css(text):
83
+ """Remove HTML tags and CSS from the text."""
84
+ soup = BeautifulSoup(text, 'html.parser')
85
+ return soup.get_text()
86
+
87
+ def retrieve_emails(service, hours):
88
+ """Retrieve emails from Gmail within the past 'hours' and return a list of email data."""
89
+ now = datetime.utcnow()
90
+ past_time = now - timedelta(hours=hours)
91
+ past_time_str = past_time.strftime('%Y/%m/%d')
92
+ query = f'newer_than:{hours}h'
93
+
94
+ results = service.users().messages().list(
95
+ userId='me',
96
+ labelIds=['INBOX'],
97
+ q=query,
98
+ maxResults=10
99
+ ).execute()
100
+
101
+ messages = results.get('messages', [])
102
+ email_data = []
103
+
104
+ for message in messages:
105
+ msg = service.users().messages().get(userId='me', id=message['id']).execute()
106
+ payload = msg['payload']
107
+ headers = payload['headers']
108
+
109
+ subject = ""
110
+ sender = ""
111
+ for header in headers:
112
+ if header['name'] == 'Subject':
113
+ subject = header['value']
114
+ if header['name'] == 'From':
115
+ sender = header['value']
116
+
117
+ body = ""
118
+ if 'parts' in payload:
119
+ for part in payload['parts']:
120
+ data = part['body'].get('data')
121
+ if data:
122
+ body += base64.urlsafe_b64decode(data).decode()
123
+ else:
124
+ data = payload['body'].get('data')
125
+ if data:
126
+ body += base64.urlsafe_b64decode(data).decode()
127
+ body=remove_html_and_css(body)
128
+ body = structure_text(body)
129
+
130
+ email_data.append({
131
+ 'sender': sender,
132
+ 'subject': subject,
133
+ 'content': body
134
+ })
135
+
136
+ return email_data
137
+ def send_email(service, to, subject, body):
138
+ """Send an email using the Gmail API."""
139
+ try:
140
+ message = {
141
+ 'raw': base64.urlsafe_b64encode(f"To: {to}\nSubject: {subject}\n\n{body}".encode()).decode()
142
+ }
143
+ sent_message = service.users().messages().send(userId='me', body=message).execute()
144
+ st.success(f"Email sent successfully! Message ID: {sent_message['id']}")
145
+ except HttpError as error:
146
+ st.error(f"An error occurred: {error}")
147
+
148
+ def create_context(emails):
149
+ """Create context for the language model from the email data."""
150
+ context = "Here is the email data:\n\n"
151
+ for email in emails:
152
+ context += f"From: {email['sender']}\nSubject: {email['subject']}\nContent: {email['content']}\n\n"
153
+ return context
154
+ def play_end_sound(file_path):
155
+ # Load and play MP3 sound
156
+ sound = AudioSegment.from_mp3(file_path)
157
+ play(sound)
158
+
159
+ def countdown(hours, sound_file):
160
+ total_seconds = hours * 3600
161
+ st.write("Countdown Timer:")
162
+ countdown_display = st.empty() # Create an empty placeholder for the countdown timer
163
+
164
+ while total_seconds > 0:
165
+ # Calculate hours, minutes, and seconds
166
+ hrs, rem = divmod(total_seconds, 3600)
167
+ mins, secs = divmod(rem, 60)
168
+
169
+ countdown_display.text(f"{hrs:02}:{mins:02}:{secs:02} remaining")
170
+ time.sleep(1) # Wait for 1 second before updating the timer
171
+ total_seconds -= 1
172
+
173
+ countdown_display.text("Time's up!")
174
+ play_end_sound(sound_file)
175
+ def main():
176
+ st.title("Time Saver")
177
+
178
+ # Add a slider and button to the sidebar
179
+ with st.sidebar:
180
+
181
+ hours = st.slider("Retrieve emails from the past (hours)", min_value=0, max_value=24, value=1)
182
+
183
+ # Email sending inputs
184
+ st.subheader("Send an Email")
185
+ recipient = st.text_input("To")
186
+ subject = st.text_input("Subject")
187
+ body = st.text_area("Body")
188
+ send_button = st.button("Send Email")
189
+ sound_file = 'sound.wav' # Path to your MP3 sound file
190
+
191
+ if st.button("Start Countdown"):
192
+ countdown(hours, sound_file)
193
+
194
+
195
+
196
+ #retrive
197
+ retrieve_button = st.button("Retrieve Emails")
198
+
199
+
200
+ if retrieve_button:
201
+ service = authenticate_google_api()
202
+ emails = retrieve_emails(service, hours=hours)
203
+ st.session_state.emails = emails
204
+ print(emails)
205
+
206
+ if send_button and recipient and subject and body:
207
+ service = authenticate_google_api()
208
+ send_email(service, recipient, subject, body)
209
+
210
+
211
+ # Initialize session state for chat messages if not already done
212
+ if 'messages' not in st.session_state:
213
+ st.session_state.messages = []
214
+
215
+ # Display existing chat history
216
+ for message in st.session_state.messages:
217
+ with st.chat_message(message['role']):
218
+ st.write(message['content'])
219
+
220
+ # User input for new message
221
+ prompt = st.chat_input("What would you like to ask about the emails?")
222
+
223
+ if prompt:
224
+ # Display user message in chat message container
225
+ with st.chat_message("user"):
226
+ st.write(prompt)
227
+
228
+ # Add user message to chat history
229
+ st.session_state.messages.append({"role": "user", "content": prompt})
230
+
231
+ # Create context for the model
232
+ if 'emails' in st.session_state:
233
+ context = create_context(st.session_state.emails)
234
+ full_prompt = f"{context}\nQuestion: {prompt}"
235
+
236
+ # Query the model
237
+ response = llm.invoke(full_prompt)
238
+
239
+ # Display the model's response
240
+ st.subheader("Model Response")
241
+ st.write(remove_unwanted_text(response.content))
242
+
243
+ # Append the model's response to the session state
244
+ st.session_state.messages.append({"role": "assistant", "content":remove_unwanted_text(response.content)})
245
+
246
+ # Clear the input field and rerun the app
247
+ st.rerun() # Use experimental_rerun for better control over rerunning the app
248
+
249
+ if __name__ == '__main__':
250
+ main()
client_secret.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"installed":{"client_id":"804318812007-ipfljeohm21imleup9je4c3nn83gtjm7.apps.googleusercontent.com","project_id":"pleasework-431315","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost"]}}
retrieve_email.py ADDED
@@ -0,0 +1,573 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from google.oauth2.credentials import Credentials
3
+ from google_auth_oauthlib.flow import InstalledAppFlow
4
+ from google.auth.transport.requests import Request
5
+ import os.path
6
+ import pickle
7
+ from googleapiclient.discovery import build
8
+ import base64
9
+ from email.mime.text import MIMEText
10
+ import chromadb
11
+ from chromadb.config import Settings
12
+ from ai71 import ChatOpenAI
13
+
14
+ # AI71 API configuration
15
+ AI71_BASE_URL = "https://api.ai71.ai/v1/"
16
+ AI71_API_KEY = "api71-api-9dc6a966-c659-4771-91ac-019ff63acb9b"
17
+ llm = ChatOpenAI(model="tiiuae/falcon-180B-chat", api_key=AI71_API_KEY, base_url=AI71_BASE_URL)
18
+
19
+ SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send']
20
+
21
+ def retrieve_emails():
22
+ creds = None
23
+ if os.path.exists('token.pickle'):
24
+ with open('token.pickle', 'rb') as token:
25
+ creds = pickle.load(token)
26
+ if not creds or not creds.valid:
27
+ if creds and creds.expired and creds.refresh_token:
28
+ creds.refresh(Request())
29
+ else:
30
+ flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
31
+ creds = flow.run_local_server(port=0)
32
+ with open('token.pickle', 'wb') as token:
33
+ pickle.dump(creds, token)
34
+
35
+ service = build('gmail', 'v1', credentials=creds)
36
+ results = service.users().messages().list(userId='me', labelIds=['INBOX'], q='newer_than:1h', maxResults=10).execute()
37
+ messages = results.get('messages', [])
38
+ email_data = []
39
+
40
+ if not messages:
41
+ return email_data
42
+
43
+ for msg in messages:
44
+ msg_detail = service.users().messages().get(userId='me', id=msg['id'], format='full').execute()
45
+ headers = msg_detail['payload']['headers']
46
+ sender = None
47
+ subject = None
48
+ for header in headers:
49
+ if header['name'] == 'From':
50
+ sender = header['value']
51
+ if header['name'] == 'Subject':
52
+ subject = header['value']
53
+
54
+ parts = msg_detail['payload'].get('parts', [])
55
+ body = ''
56
+ if parts:
57
+ for part in parts:
58
+ if part['mimeType'] == 'text/plain':
59
+ body = base64.urlsafe_b64decode(part['body']['data']).decode('utf-8')
60
+ break
61
+ else:
62
+ body = base64.urlsafe_b64decode(msg_detail['payload']['body']['data']).decode('utf-8')
63
+
64
+ email_data.append({
65
+ 'id': msg['id'],
66
+ 'sender': sender,
67
+ 'subject': subject,
68
+ 'body': body
69
+ })
70
+
71
+ return email_data
72
+
73
+ def save_to_chromadb(data):
74
+ client = chromadb.Client(Settings())
75
+ collection = client.create_collection(name="emails")
76
+ for item in data:
77
+ collection.add(documents=[item['body']], metadatas=[{'sender': item['sender'], 'subject': item['subject']}])
78
+
79
+ def main():
80
+ st.title("Email Management App")
81
+
82
+ # Retrieve emails and save to ChromaDB
83
+ if st.button("Retrieve Emails"):
84
+ email_data = retrieve_emails()
85
+ if email_data:
86
+ save_to_chromadb(email_data)
87
+ st.success("Emails retrieved and saved to ChromaDB.")
88
+ else:
89
+ st.info("No new emails found.")
90
+
91
+ # Display chat interface
92
+ chat_input = st.chat_input("Type your message here...")
93
+
94
+ if chat_input:
95
+ st.write(f"You said: {chat_input}")
96
+
97
+ # Handle chat input
98
+ client = chromadb.Client(Settings())
99
+ collection = client.get_collection(name="emails")
100
+ senders = set(doc['metadata']['sender'] for doc in collection.get_all())
101
+
102
+ # Display senders as chat buttons
103
+ if senders:
104
+ for sender in senders:
105
+ if st.button(sender):
106
+ query = f"Tell me more about emails from {sender}"
107
+ response = llm(query)
108
+ st.write(f"Response from AI71 API:\n{response}")
109
+
110
+ if __name__ == '__main__':
111
+ main()
112
+
113
+
114
+
115
+
116
+ ///////////import streamlit as st
117
+ from google.oauth2.credentials import Credentials
118
+ from google_auth_oauthlib.flow import InstalledAppFlow
119
+ from google.auth.transport.requests import Request
120
+ import os.path
121
+ import pickle
122
+ from googleapiclient.discovery import build
123
+ import base64
124
+ from email.mime.text import MIMEText
125
+ import chromadb
126
+ from chromadb.config import Settings
127
+ from langchain_openai import ChatOpenAI
128
+
129
+ # AI71 API configuration
130
+ AI71_BASE_URL = "https://api.ai71.ai/v1/"
131
+ AI71_API_KEY = "api71-api-9dc6a966-c659-4771-91ac-019ff63acb9b"
132
+ llm = ChatOpenAI(model="tiiuae/falcon-180B-chat", api_key=AI71_API_KEY, base_url=AI71_BASE_URL)
133
+
134
+ SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send']
135
+
136
+ def authenticate_google_api():
137
+ """Authenticate and return Google API service object."""
138
+ creds = None
139
+ if os.path.exists('token.pickle'):
140
+ with open('token.pickle', 'rb') as token:
141
+ creds = pickle.load(token)
142
+
143
+ if not creds or not creds.valid:
144
+ if creds and creds.expired and creds.refresh_token:
145
+ creds.refresh(Request())
146
+ else:
147
+ flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
148
+ creds = flow.run_local_server(port=0)
149
+
150
+ with open('token.pickle', 'wb') as token:
151
+ pickle.dump(creds, token)
152
+
153
+ return build('gmail', 'v1', credentials=creds)
154
+
155
+ def retrieve_emails(service):
156
+ """Retrieve emails from Gmail and return a list of email data."""
157
+ results = service.users().messages().list(userId='me', labelIds=['INBOX'], q='newer_than:1h', maxResults=10).execute()
158
+ messages = results.get('messages', [])
159
+ email_data = []
160
+
161
+ if not messages:
162
+ st.write("No new messages.")
163
+ return email_data
164
+
165
+ for msg in messages:
166
+ msg_detail = service.users().messages().get(userId='me', id=msg['id'], format='full').execute()
167
+ headers = msg_detail['payload']['headers']
168
+ sender = next((header['value'] for header in headers if header['name'] == 'From'), None)
169
+ subject = next((header['value'] for header in headers if header['name'] == 'Subject'), None)
170
+
171
+ parts = msg_detail['payload'].get('parts', [])
172
+ body = ''
173
+ if parts:
174
+ for part in parts:
175
+ if part['mimeType'] == 'text/plain':
176
+ body = base64.urlsafe_b64decode(part['body']['data']).decode('utf-8')
177
+ break
178
+ else:
179
+ body = base64.urlsafe_b64decode(msg_detail['payload']['body']['data']).decode('utf-8')
180
+
181
+ email_data.append({
182
+ 'id': msg['id'],
183
+ 'sender': sender,
184
+ 'subject': subject,
185
+ 'body': body
186
+ })
187
+
188
+ return email_data
189
+
190
+ def save_to_chromadb(data):
191
+ """Save email data to ChromaDB collection."""
192
+ client = chromadb.Client(Settings())
193
+ collection_name = "emails"
194
+
195
+ existing_collections = [col.name for col in client.list_collections()]
196
+ if collection_name in existing_collections:
197
+ collection = client.get_collection(name=collection_name)
198
+ st.write(f"Collection '{collection_name}' already exists.")
199
+ else:
200
+ collection = client.create_collection(name=collection_name)
201
+ st.write(f"Created new collection '{collection_name}'.")
202
+
203
+ ids = [item['id'] for item in data]
204
+ documents = [item['body'] for item in data]
205
+ metadatas = [{'sender': item['sender'], 'subject': item['subject']} for item in data]
206
+
207
+ collection.add(ids=ids, documents=documents, metadatas=metadatas)
208
+ st.write(f"Saved {len(data)} emails to ChromaDB.")
209
+
210
+ def get_senders_from_chromadb():
211
+ """Retrieve a list of unique email senders from ChromaDB."""
212
+ client = chromadb.Client(Settings())
213
+ collection = client.get_collection(name="emails")
214
+
215
+ results = collection.query(query_texts="", n_results=100, include=["metadatas"]) # Adjust top_k as needed
216
+
217
+ st.write("ChromaDB Query Results:", results) # Debugging line
218
+
219
+ senders = {metadata.get('sender') for result in results.get('results', []) for metadata in result.get('metadatas', [])}
220
+ return list(senders)
221
+
222
+ def get_response(prompt):
223
+ """Get response from AI model."""
224
+ try:
225
+ response = llm(prompt) # Directly call the model with the prompt
226
+ response_text = response.strip()
227
+ cleaned_response = response_text.replace('User:', '').replace('Assistant:', '').strip()
228
+ st.write("AI Model Response:", cleaned_response) # Debugging line
229
+ return cleaned_response
230
+ except Exception as e:
231
+ st.write(f"An error occurred: {e}")
232
+ return "Error: An internal server error occurred."
233
+
234
+ def query_chromadb(prompt):
235
+ """Query ChromaDB collection with the given prompt."""
236
+ client = chromadb.Client(Settings())
237
+ collection = client.get_collection(name="emails")
238
+
239
+ results = collection.query(
240
+ query_texts=prompt,
241
+ n_results=5,
242
+ include=["metadatas",]
243
+ )
244
+
245
+ st.write("ChromaDB Query Results:", results) # Debugging line
246
+
247
+ documents = []
248
+ for result in results.get('results', []):
249
+ documents.extend(result.get('documents', []))
250
+
251
+ return documents
252
+
253
+ # Streamlit app configuration
254
+ st.set_page_config(page_title="Chat with Falcon-180B", layout="wide")
255
+ st.title("Chat with Falcon-180B")
256
+
257
+ if 'messages' not in st.session_state:
258
+ st.session_state.messages = []
259
+ if 'emails' not in st.session_state:
260
+ st.session_state.emails = []
261
+
262
+ # Authenticate Google API and retrieve emails
263
+ if st.button("Retrieve Emails"):
264
+ service = authenticate_google_api()
265
+ emails = retrieve_emails(service)
266
+ st.session_state.emails = emails
267
+ save_to_chromadb(emails)
268
+ st.success("Emails retrieved and saved to ChromaDB.")
269
+
270
+ # Display conversation history
271
+ for msg in st.session_state.messages:
272
+ role = msg.get("role", None)
273
+ with st.chat_message(role):
274
+ st.write(msg.get("content", "No message"))
275
+
276
+ # Display email data
277
+ if st.session_state.emails:
278
+ st.subheader("Email Data")
279
+ for email in st.session_state.emails:
280
+ st.write(f"**Sender:** {email['sender']}")
281
+ st.write(f"**Subject:** {email['subject']}")
282
+ st.write(f"**Body:** {email['body']}")
283
+ st.write("---")
284
+
285
+ # Display email senders as buttons
286
+ if st.session_state.emails:
287
+ st.sidebar.header("Email Senders")
288
+ senders = get_senders_from_chromadb()
289
+ for i, sender in enumerate(senders):
290
+ if st.sidebar.button(sender, key=f"button_{i}"):
291
+ st.session_state.current_sender = sender
292
+ st.session_state.current_sender_emails = [e for e in st.session_state.emails if e['sender'] == sender]
293
+
294
+ # Display emails from the current sender and handle summarization
295
+ if 'current_sender_emails' in st.session_state:
296
+ st.write(f"Emails from {st.session_state.current_sender}:")
297
+ for email in st.session_state.current_sender_emails:
298
+ if st.button(f"Summarize email: {email['subject']}", key=f"summarize_{email['id']}"):
299
+ summary = get_response(f"Summarize the following email: {email['body']}")
300
+ st.write(summary)
301
+
302
+ # Chat input
303
+ prompt = st.chat_input("Ask a question or type 'summarize' to summarize emails.")
304
+
305
+ if prompt:
306
+ if prompt.lower() == 'summarize':
307
+ summaries = [{'subject': email['subject'], 'summary': get_response(f"Summarize the following email: {email['body']}")} for email in st.session_state.emails]
308
+ st.write("Summaries:")
309
+ for summary in summaries:
310
+ st.write(f"Subject: {summary['subject']}")
311
+ st.write(f"Summary: {summary['summary']}")
312
+ else:
313
+ with st.chat_message("user"):
314
+ st.write(prompt)
315
+
316
+ st.session_state.messages.append({"role": "user", "content": prompt})
317
+
318
+ # Query ChromaDB based on user input
319
+ documents = query_chromadb(prompt)
320
+
321
+ if documents:
322
+ st.write("Documents retrieved from ChromaDB:", documents) # Debugging line
323
+
324
+ # Ensure 'documents' are correctly accessed from results
325
+ responses = [get_response(f"Analyze the following information: {doc}") for doc in documents]
326
+
327
+ for response in responses:
328
+ st.session_state.messages.append({"role": "assistant", "content": response})
329
+
330
+ st.rerun()
331
+ /////////////////////
332
+ import streamlit as st
333
+ from langchain_openai import ChatOpenAI
334
+ import chromadb
335
+ import streamlit as st
336
+
337
+ # Configure the LangChain API
338
+ AI71_BASE_URL = "https://api.ai71.ai/v1/"
339
+ AI71_API_KEY = "ai71-api-1ee3a6dd-28c9-427c-800d-afb198e86a1d"
340
+
341
+ # Initialize the LangChain model
342
+ llm = ChatOpenAI(
343
+ model="tiiuae/falcon-180B-chat",
344
+ api_key=AI71_API_KEY,
345
+ base_url=AI71_BASE_URL
346
+ )
347
+
348
+ # Define your email data
349
+ emails = [
350
+ {"sender": "[email protected]", "subject": "Meeting Update", "content": "The meeting has been rescheduled to 3 PM."},
351
+ {"sender": "[email protected]", "subject": "Project Deadline", "content": "The project deadline is next Friday."},
352
+ # Add more emails as needed
353
+ ]
354
+
355
+ # Convert email data to a context string
356
+ def create_context(emails):
357
+ context = "Here is the email data:\n\n"
358
+ for email in emails:
359
+ context += f"From: {email['sender']}\nSubject: {email['subject']}\nContent: {email['content']}\n\n"
360
+ return context
361
+
362
+ # Initialize session state if not already done
363
+ if 'messages' not in st.session_state:
364
+ st.session_state.messages = []
365
+
366
+ # Display existing chat history
367
+ st.title("Email Analysis with Falcon-180B")
368
+
369
+ for message in st.session_state.messages:
370
+ with st.chat_message(message['role']):
371
+ st.write(message['content'])
372
+
373
+ # User input for new message
374
+ prompt = st.chat_input("What cards are you looking for?")
375
+
376
+ if prompt:
377
+ # Display user message in chat message container
378
+ with st.chat_message("user"):
379
+ st.write(prompt)
380
+
381
+ # Add user message to chat history
382
+ st.session_state.messages.append({"role": "user", "content": prompt})
383
+
384
+ # Create context for the model
385
+ context = create_context(emails)
386
+ full_prompt = f"{context}\nQuestion: {prompt}"
387
+
388
+ # Query the model
389
+ response = llm.invoke(full_prompt)
390
+
391
+ # Display the model's response
392
+ st.subheader("Model Response")
393
+ st.write(response.content)
394
+
395
+ # Append the model's response to the session state
396
+ st.session_state.messages.append({"role": "assistant", "content": response.content})
397
+
398
+ # Clear the input field and rerun the app
399
+ st.rerun()
400
+
401
+ /////
402
+ import os
403
+ import pickle
404
+ import base64
405
+ import json
406
+ from datetime import datetime, timedelta
407
+ from google.auth.transport.requests import Request
408
+ from google.oauth2.credentials import Credentials
409
+ from google_auth_oauthlib.flow import InstalledAppFlow
410
+ from googleapiclient.discovery import build
411
+ from langchain_openai import ChatOpenAI
412
+ import streamlit as st
413
+ # Replace with actual import path
414
+
415
+ # Initialize the ChatOpenAI instance
416
+ AI71_BASE_URL = "https://api.ai71.ai/v1/"
417
+ AI71_API_KEY = "ai71-api-1505741d-83e9-4ee9-91ea-a331d47a4680"
418
+ llm = ChatOpenAI(model="tiiuae/falcon-180B-chat", api_key=AI71_API_KEY, base_url=AI71_BASE_URL)
419
+
420
+ def summarize_text(text):
421
+ try:
422
+ response = llm.invoke(input=f"Please summarize the following text:\n{text}")
423
+ summary = response.content.strip()
424
+ return summary
425
+ except Exception as e:
426
+ st.error(f"An error occurred while summarizing: {e}")
427
+ return None
428
+
429
+ def structure_text(text):
430
+ try:
431
+ response = llm.invoke(input=f"Please structure the following text:\n{text}")
432
+ structured_text = response.content.strip()
433
+ return structured_text
434
+ except Exception as e:
435
+ st.error(f"An error occurred while structuring: {e}")
436
+ return None
437
+
438
+
439
+ st.title("Text Summarizer and Structurer")
440
+
441
+ # Text input for summarization
442
+ st.header("Summarize Text")
443
+ text_to_summarize = st.text_area("Enter text to summarize", height=150)
444
+ if st.button("Summarize"):
445
+ if text_to_summarize:
446
+ summary = summarize_text(text_to_summarize)
447
+ st.subheader("Summary")
448
+ st.write(summary)
449
+ else:
450
+ st.error("Please enter some text to summarize.")
451
+
452
+ # Text input for structuring
453
+ st.header("Structure Text")
454
+ text_to_structure = st.text_area("Enter text to structure", height=150)
455
+ if st.button("Structure"):
456
+ if text_to_structure:
457
+ structured_text = structure_text(text_to_structure)
458
+ st.subheader("Structured Text")
459
+ st.write(structured_text)
460
+ else:
461
+ st.error("Please enter some text to structure.")
462
+
463
+
464
+
465
+
466
+
467
+ //////////////
468
+ import os
469
+ import pickle
470
+ import base64
471
+ import re
472
+ import json
473
+ from datetime import datetime, timedelta
474
+ from google.auth.transport.requests import Request
475
+ from google.oauth2.credentials import Credentials
476
+ from google_auth_oauthlib.flow import InstalledAppFlow
477
+ from googleapiclient.discovery import build
478
+ from bs4 import BeautifulSoup # Import BeautifulSoup
479
+
480
+ # Scopes for Gmail API
481
+ SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
482
+
483
+ def authenticate_google_api():
484
+ """Authenticate and return Google API service object."""
485
+ creds = None
486
+ if os.path.exists('token.pickle'):
487
+ with open('token.pickle', 'rb') as token:
488
+ creds = pickle.load(token)
489
+
490
+ if not creds or not creds.valid:
491
+ if creds and creds.expired and creds.refresh_token:
492
+ creds.refresh(Request())
493
+ else:
494
+ flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
495
+ creds = flow.run_local_server(port=0)
496
+
497
+ with open('token.pickle', 'wb') as token:
498
+ pickle.dump(creds, token)
499
+
500
+ return build('gmail', 'v1', credentials=creds)
501
+
502
+ def remove_html_and_css(text):
503
+ """Remove HTML tags and CSS from the text."""
504
+ soup = BeautifulSoup(text, 'html.parser')
505
+ # Get plain text from HTML
506
+ plain_text = soup.get_text()
507
+ return plain_text
508
+
509
+ def retrieve_emails(service, hours):
510
+ """Retrieve emails from Gmail within the past 'hours' and return a list of email data."""
511
+ # Calculate the time range
512
+ now = datetime.utcnow()
513
+ past_time = now - timedelta(hours=hours)
514
+
515
+ # Convert time to RFC 3339 format
516
+ past_time_str = past_time.strftime('%Y/%m/%d')
517
+
518
+ # Query to get emails newer than the calculated time
519
+ query = f'newer_than:{hours}h'
520
+
521
+ results = service.users().messages().list(
522
+ userId='me',
523
+ labelIds=['INBOX'],
524
+ q=query,
525
+ maxResults=10
526
+ ).execute()
527
+
528
+ messages = results.get('messages', [])
529
+ email_data = []
530
+
531
+ for message in messages:
532
+ msg = service.users().messages().get(userId='me', id=message['id']).execute()
533
+ payload = msg['payload']
534
+ headers = payload['headers']
535
+
536
+ subject = ""
537
+ sender = ""
538
+ for header in headers:
539
+ if header['name'] == 'Subject':
540
+ subject = header['value']
541
+ if header['name'] == 'From':
542
+ sender = header['value']
543
+
544
+ body = ""
545
+ if 'parts' in payload:
546
+ parts = payload['parts']
547
+ for part in parts:
548
+ data = part['body'].get('data')
549
+ if data:
550
+ body += base64.urlsafe_b64decode(data).decode()
551
+ else:
552
+ data = payload['body'].get('data')
553
+ if data:
554
+ body += base64.urlsafe_b64decode(data).decode()
555
+
556
+ # Remove HTML tags and CSS from the body
557
+ body = remove_html_and_css(body)
558
+
559
+ email_data.append({
560
+ 'sender': sender,
561
+ 'subject': subject,
562
+ 'content': body
563
+ })
564
+
565
+ return email_data
566
+
567
+ def main():
568
+ service = authenticate_google_api()
569
+ emails = retrieve_emails(service, hours=3) # Get emails from the past 3 hours
570
+ print(json.dumps(emails, indent=2))
571
+
572
+ if __name__ == '__main__':
573
+ main()
sound.mp3 ADDED
Binary file (50.9 kB). View file
 
sound.wav ADDED
Binary file (244 kB). View file
 
token.pickle ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:34af1d481cb4713d82d3b90e43bc648338381d9c76d1a50354cb827af6ea609d
3
+ size 1076