-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
484 lines (251 loc) · 10.6 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import joblib
import random
import time
import streamlit as st
import pandas as pd
from bs4 import BeautifulSoup
import google.generativeai as genai
import os
import requests
from io import BytesIO, TextIOWrapper
import PyPDF2
import docx2txt
import csv
new_chat_id = f'{time.time()}'
MODEL_ROLE = 'ai'
AI_AVATAR_ICON = '✨'
######################################################################################################################################
# Function to scrape data
def scrape_data(url):
# Send HTTP request and parse content
response = requests.get(url)
# print(response)
soup = BeautifulSoup(response.content, 'html.parser')
# Scraping logic - use BeautifulSoup to find and extract various types of content
texts = [element.text for element in soup.find_all(['p', 'a', 'img'])]
links = [element.get('href') for element in soup.find_all('a') if element.get('href')]
images = [element.get('src') for element in soup.find_all('img') if element.get('src')]
# Ensure all lists are of the same length by padding the shorter ones with None
max_length = max(len(texts), len(links), len(images))
texts += [None] * (max_length - len(texts))
links += [None] * (max_length - len(links))
images += [None] * (max_length - len(images))
# Create a DataFrame using pandas for texts, links, and images
data = {'Text': texts, 'Links': links, 'Images': images}
df = pd.DataFrame(data)
# return the processed data
return df
# Function to extract text from a PDF file
def extract_text_from_pdf(file_bytes):
pdf_reader = PyPDF2.PdfReader(BytesIO(file_bytes))
num_pages = len(pdf_reader.pages)
text = ""
for page_num in range(num_pages):
page = pdf_reader.pages[page_num]
text += page.extract_text()
return text.replace('\t', ' ').replace('\n', ' ')
# Function to extract text from a TXT file
def extract_text_from_txt(file_bytes):
text = file_bytes.decode('utf-8')
return text
# Function to extract text from a DOCX file
def extract_text_from_docx(file_bytes):
docx = docx2txt.process(BytesIO(file_bytes))
return docx.replace('\t', ' ').replace('\n', ' ')
def extract_text_from_csv(file_bytes, encoding='utf-8'):
# Convert bytes to text using the specified encoding
file_text = file_bytes.decode(encoding)
# Use CSV reader to read the content
csv_reader = csv.reader(TextIOWrapper(BytesIO(file_text.encode(encoding)), encoding=encoding))
# Concatenate all rows and columns into a single text
text = ""
for row in csv_reader:
text += ' '.join(row) + ' '
return text.replace('\t', ' ').replace('\n', ' ')
######################################################################################################################################
# Create a data/ folder if it doesn't already exist
try:
os.mkdir('data/')
except:
# data/ folder already exists
pass
try:
past_chats: dict = joblib.load('data/past_chats_list')
except:
past_chats = {}
######################################################################################################################################
# Sidebar allows a list of past chats
with st.sidebar:
st.write('# GOOGLE API KEY')
# take google api key as a text input
GOOGLE_API_KEY = st.text_input('Google API Key')
if GOOGLE_API_KEY == '':
# st.error("API Key is not set. Please enter your Google API Key.")
pass
else:
genai.configure(api_key=GOOGLE_API_KEY)
st.write('# Scrap Link')
# take link as a text input
url_input = st.text_input('Enter the website URL: ', '')
if url_input:
if 'https://' not in url_input:
url_input = 'https://' + url_input
scraped_data = scrape_data(url_input)
paragraph = ' '.join(scraped_data['Text'].dropna())
url_input = paragraph
# st.write(scraped_data)
# st.write(paragraph)
st.write('# File Upload')
# st.write("Upload a PDF, TXT, or DOCX file.")
uploaded_file = st.file_uploader("Upload a PDF, TXT, or DOCX file.")
print(uploaded_file)
if uploaded_file:
# Get the file extension
file_name, file_extension = os.path.splitext(uploaded_file.name)
if file_extension:
# Extract text based on the file extension
if file_extension == ".pdf":
uploaded_file = extract_text_from_pdf(uploaded_file.getvalue())
elif file_extension == ".txt":
uploaded_file = extract_text_from_txt(uploaded_file.getvalue())
elif file_extension == ".docx":
uploaded_file = extract_text_from_docx(uploaded_file.getvalue())
elif file_extension == ".csv":
uploaded_file = extract_text_from_csv(uploaded_file.getvalue())
else:
st.error("Unsupported file type.")
sData = st.checkbox('Show scrapped data')
if sData:
st.write(url_input)
st.download_button(
label="Download Scrapped Data",
data=url_input,
file_name=f"scraped_data_{time.time()}.txt",
mime="text/plain"
)
fData = st.checkbox('Show uploaded file')
if fData:
st.write(uploaded_file)
st.download_button(
label="Download Uploaded File",
data=uploaded_file,
file_name=f"uploaded_file_{time.time()}.txt",
mime="text/plain"
)
st.write('# Chat History')
if st.session_state.get('chat_id') is None:
st.session_state.chat_id = st.selectbox(
label='Pick a past chat',
options=[new_chat_id] + list(past_chats.keys()),
format_func=lambda x: past_chats.get(x, 'New Chat'),
placeholder='_',
)
else:
st.session_state.chat_id = st.selectbox(
label='Pick a past chat',
options=[new_chat_id, st.session_state.chat_id] + list(past_chats.keys()),
index=1,
format_func=lambda x: past_chats.get(x, 'New Chat' if x != st.session_state.chat_id else st.session_state.chat_title),
placeholder='_',
)
st.session_state.chat_title = f'ChatSession-{random.randint(1,10)} at {int(time.time())}'
######################################################################################################################################
st.write('# Chat with Gemini')
# Chat history (allows to ask multiple questions)
try:
st.session_state.messages = joblib.load(
f'data/{st.session_state.chat_id}-st_messages'
)
st.session_state.gemini_history = joblib.load(
f'data/{st.session_state.chat_id}-gemini_messages'
)
print('old cache')
except:
st.session_state.messages = []
st.session_state.gemini_history = []
print('new_cache made')
st.session_state.model = genai.GenerativeModel('gemini-pro')
st.session_state.chat = st.session_state.model.start_chat(
history=st.session_state.gemini_history,
)
######################################################################################################################################
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(
name=message['role'],
avatar=message.get('avatar'),
):
st.markdown(message['content'])
# React to user input
if prompt := st.chat_input('Your message here...'):
# Save this as a chat for later
if st.session_state.chat_id not in past_chats.keys():
past_chats[st.session_state.chat_id] = st.session_state.chat_title
joblib.dump(past_chats, 'data/past_chats_list')
# Display user message in chat message container
with st.chat_message('user'):
st.markdown(prompt)
# Add user message to chat history
st.session_state.messages.append(
dict(
role='user',
content=prompt,
)
)
######################################################################################################################################
if uploaded_file:
prompt = uploaded_file + ' ' +"Take the given data above, as information and generate a response based on this prompt: " + prompt
if url_input:
prompt = url_input + ' ' +"Take the given data above, as information and generate a response based on this prompt: " + prompt
if uploaded_file and url_input:
prompt = uploaded_file + ' ' + url_input + ' ' +"Take the given data above , as information and generate a response based on this prompt: " + prompt
######################################################################################################################################
## Send message to AI
response = st.session_state.chat.send_message(
prompt,
stream=True,
)
# Display assistant response in chat message container
with st.chat_message(
name=MODEL_ROLE,
avatar=AI_AVATAR_ICON,
):
message_placeholder = st.empty()
full_response = ''
for chunk in response:
# Simulate stream of chunk
# TODO: Chunk missing `text` if API stops mid-stream ("safety"?)
if hasattr(chunk, 'text'):
for ch in chunk.text.split(' '):
full_response += ch + ' '
time.sleep(0.05)
# Rewrites with a cursor at end
message_placeholder.write(full_response + '▌')
elif hasattr(chunk, 'parts') and len(chunk.parts) > 0:
full_response += chunk.parts[0].text
time.sleep(0.05)
message_placeholder.write(full_response + '▌')
else:
# If no text or parts, wait for the next chunk
time.sleep(0.1)
# Write full message with placeholder
message_placeholder.write(full_response)
# Add assistant response to chat history
st.session_state.messages.append(
dict(
role=MODEL_ROLE,
content=st.session_state.chat.history[-1].parts[0].text,
avatar=AI_AVATAR_ICON,
)
)
st.session_state.gemini_history = st.session_state.chat.history
######################################################################################################################################
# Save to file
joblib.dump(
st.session_state.messages,
f'data/{st.session_state.chat_id}-st_messages',
)
joblib.dump(
st.session_state.gemini_history,
f'data/{st.session_state.chat_id}-gemini_messages',
)