Spaces:
Sleeping
Sleeping
add new_chat/clear history buttons
Browse files- app_drive.py +71 -209
app_drive.py
CHANGED
@@ -1,213 +1,75 @@
|
|
1 |
-
#
|
2 |
|
3 |
-
import
|
4 |
-
import time
|
5 |
-
import datetime
|
6 |
import os
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
from
|
11 |
-
from
|
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 |
else:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
# Update global variables for logging
|
65 |
-
time_diff = time.time() - start_time
|
66 |
-
model_response = output
|
67 |
-
input_question = question
|
68 |
-
save_text_feedback(feedback="Default Conversation Save!!!") # Upload chatlog to drive after every response irrespective of feedback
|
69 |
-
|
70 |
-
bot_message = output
|
71 |
-
chat_history.append((message, bot_message))
|
72 |
-
|
73 |
-
time.sleep(1) # Pause for a second to avoid overloading
|
74 |
-
return " ", chat_history
|
75 |
-
|
76 |
-
############################# Feedback Specific functions #############################
|
77 |
-
|
78 |
-
def save_feedback(feedback):
|
79 |
-
global user_feedback
|
80 |
-
user_feedback = feedback
|
81 |
-
|
82 |
-
curr_date = datetime.datetime.now()
|
83 |
-
file_name = f"chat_{curr_date.day}_{curr_date.month}_{curr_date.hour}_{curr_date.minute}_{curr_date.second}.csv"
|
84 |
-
log_data = [
|
85 |
-
["Question", "Response", "Model", "Time", "Feedback"],
|
86 |
-
[input_question, model_response, model_name, time_diff, user_feedback]
|
87 |
-
]
|
88 |
-
|
89 |
-
if model_response and user_feedback[0] != "None":
|
90 |
-
upload_chat_to_drive(log_data, file_name)
|
91 |
-
|
92 |
-
|
93 |
-
def default_feedback():
|
94 |
-
return "None"
|
95 |
-
|
96 |
-
def default_text():
|
97 |
-
return ""
|
98 |
-
|
99 |
-
def save_text_feedback(feedback):
|
100 |
-
global text_feedback
|
101 |
-
text_feedback = feedback
|
102 |
-
|
103 |
-
curr_date = datetime.datetime.now()
|
104 |
-
file_name = f"chat_{curr_date.day}_{curr_date.month}_{curr_date.hour}_{curr_date.minute}_{curr_date.second}.csv"
|
105 |
-
log_data = [
|
106 |
-
["Question", "Response", "Model", "Time", "Feedback"],
|
107 |
-
[input_question, model_response, model_name, time_diff, text_feedback]
|
108 |
-
]
|
109 |
-
|
110 |
-
upload_chat_to_drive(log_data, file_name)
|
111 |
-
|
112 |
-
|
113 |
-
############################# Gradio Application Block #############################
|
114 |
-
with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", neutral_hue="slate")) as chat:
|
115 |
-
gr.HTML(title)
|
116 |
-
|
117 |
-
global db
|
118 |
-
|
119 |
-
# PDF Drag and Drop + Drive link Input + Status containers
|
120 |
-
with gr.Row(equal_height=True):
|
121 |
-
with gr.Column():
|
122 |
-
with gr.Row():
|
123 |
-
pdf_files_dnd = gr.File(file_count='multiple', height=250, label="Upload PDF Files")
|
124 |
-
|
125 |
-
with gr.Column():
|
126 |
-
with gr.Row():
|
127 |
-
drive_link_input = gr.Textbox(lines=1, label="Enter your shared drive link, then press Enter...")
|
128 |
-
with gr.Row():
|
129 |
-
status_message = gr.Text(label="Status", value="⬆️Submit a (shared) drive link containing only PDFs \n-or- \n⬅️Upload PDF files", text_align='center')
|
130 |
-
|
131 |
-
|
132 |
-
# What happens when PDF is uploaded or a drive link is submitted
|
133 |
-
drive_link_input.submit(
|
134 |
-
fn = create_data_from_drive,
|
135 |
-
inputs = [drive_link_input],
|
136 |
-
outputs = [status_message])
|
137 |
-
|
138 |
-
pdf_files_dnd.change(
|
139 |
-
fn=check_pdfs,
|
140 |
-
inputs=[pdf_files_dnd],
|
141 |
-
outputs=[status_message],
|
142 |
-
preprocess=False,
|
143 |
-
postprocess=False) # Set preprocess and postprocess to False, to avoid the tmpfile object creation, instead get a Dict
|
144 |
-
|
145 |
-
# Chatbot container
|
146 |
-
chatbot = gr.Chatbot(height=750)
|
147 |
-
msg = gr.Textbox(label="Send a message", placeholder="Send a message",
|
148 |
-
show_label=False, container=False)
|
149 |
-
|
150 |
-
# Sample questions
|
151 |
-
with gr.Row():
|
152 |
-
with gr.Column():
|
153 |
-
gr.Examples([
|
154 |
-
["Explain these documents to me in simpler terms."],
|
155 |
-
["What does these documents talk about?"],
|
156 |
-
["Give the key topics covered in these documents in less than 10 words."],
|
157 |
-
["What are the key findings in these documents?"],
|
158 |
-
], inputs=msg, label= "Click on any example to copy in the chatbox"
|
159 |
-
)
|
160 |
-
|
161 |
-
# Feedback options container
|
162 |
-
with gr.Row():
|
163 |
-
with gr.Column():
|
164 |
-
feedback_radio = gr.Radio(
|
165 |
-
choices=["1", "2", "3", "4", "5", "6", "None"],
|
166 |
-
value=["None"],
|
167 |
-
label="On a scale from 1 (very unsatisfied) to 6 (very satisfied), how would you rate the current response?",
|
168 |
-
)
|
169 |
|
170 |
-
|
171 |
-
feedback_text = gr.Textbox(lines=1, label="Additional comments on the current response...")
|
172 |
-
|
173 |
-
|
174 |
-
# Get a response when a message is submitted to the chatbot
|
175 |
-
msg.submit(
|
176 |
-
fn = respond,
|
177 |
-
inputs = [msg, chatbot],
|
178 |
-
outputs = [msg, chatbot],
|
179 |
-
queue = True)
|
180 |
-
|
181 |
-
|
182 |
-
# Set default feedback to None after a message is submitted
|
183 |
-
msg.submit(
|
184 |
-
fn = default_feedback,
|
185 |
-
outputs=[feedback_radio],
|
186 |
-
queue = True
|
187 |
-
)
|
188 |
-
|
189 |
-
# Change whenever some feedback is given (Numeric or Text)
|
190 |
-
feedback_radio.change(
|
191 |
-
fn=save_feedback,
|
192 |
-
inputs=[feedback_radio]
|
193 |
-
)
|
194 |
-
|
195 |
-
feedback_text.submit(
|
196 |
-
fn=save_text_feedback,
|
197 |
-
inputs=[feedback_text],
|
198 |
-
queue=True
|
199 |
-
)
|
200 |
-
|
201 |
-
# Clear the text feedback after it is submitted
|
202 |
-
feedback_text.submit(
|
203 |
-
fn=default_text,
|
204 |
-
outputs=[feedback_text],
|
205 |
-
queue=True
|
206 |
-
)
|
207 |
-
|
208 |
-
# Description at the bottom of the application
|
209 |
-
gr.HTML(description)
|
210 |
-
|
211 |
-
# Enable queing
|
212 |
-
chat.queue()
|
213 |
-
chat.launch()
|
|
|
1 |
+
# ADDING GOOGLE DRIVE SUPPORT
|
2 |
|
3 |
+
import io
|
|
|
|
|
4 |
import os
|
5 |
+
import csv
|
6 |
+
import PyPDF2
|
7 |
+
|
8 |
+
from google.oauth2 import service_account
|
9 |
+
from googleapiclient.discovery import build
|
10 |
+
from googleapiclient.http import MediaIoBaseUpload, MediaIoBaseDownload
|
11 |
+
from driveapi.service import get_credentials
|
12 |
+
|
13 |
+
credentials_info = get_credentials()
|
14 |
+
credentials = service_account.Credentials.from_service_account_info(credentials_info)
|
15 |
+
service = build('drive', 'v3', credentials=credentials)
|
16 |
+
|
17 |
+
logs_id = os.environ.get('LOGS_ID')
|
18 |
+
|
19 |
+
# Save Logs
|
20 |
+
def upload_chat_to_drive(chat_history, file_name):
|
21 |
+
# Convert chat history to CSV
|
22 |
+
csv_output = io.StringIO()
|
23 |
+
writer = csv.writer(csv_output)
|
24 |
+
writer.writerows(chat_history)
|
25 |
+
csv_output.seek(0)
|
26 |
+
|
27 |
+
# File metadata
|
28 |
+
file_metadata = {
|
29 |
+
'name': file_name,
|
30 |
+
'mimeType': 'application/vnd.google-apps.spreadsheet',
|
31 |
+
'parents': [logs_id]
|
32 |
+
}
|
33 |
+
|
34 |
+
# Upload file
|
35 |
+
media = MediaIoBaseUpload(csv_output, mimetype='text/csv')
|
36 |
+
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
37 |
+
|
38 |
+
|
39 |
+
## Read PDF files
|
40 |
+
def download_file(file_id):
|
41 |
+
service = build('drive', 'v3', credentials=credentials)
|
42 |
+
request = service.files().get_media(fileId=file_id)
|
43 |
+
fh = io.BytesIO()
|
44 |
+
downloader = MediaIoBaseDownload(fh, request)
|
45 |
+
done = False
|
46 |
+
while done is False:
|
47 |
+
status, done = downloader.next_chunk()
|
48 |
+
fh.seek(0)
|
49 |
+
return fh
|
50 |
+
|
51 |
+
# Function to process a PDF file
|
52 |
+
def process_pdf(file_stream):
|
53 |
+
if isinstance(file_stream, dict): # Check if PDF was obtained using Drag and Drop or Drive link
|
54 |
+
file_path = file_stream['name'] # Use 'path' for local testing and 'name' for Gradio
|
55 |
+
pdf_reader = PyPDF2.PdfReader(file_path)
|
56 |
else:
|
57 |
+
pdf_reader = PyPDF2.PdfReader(file_stream)
|
58 |
+
text = ""
|
59 |
+
for page_num in range(len(pdf_reader.pages)):
|
60 |
+
page = pdf_reader.pages[page_num]
|
61 |
+
text += page.extract_text()
|
62 |
+
return text
|
63 |
+
|
64 |
+
def drive_content(shared_folder_id):
|
65 |
+
# List files in the folder
|
66 |
+
results = service.files().list(q=f"'{shared_folder_id}' in parents", fields="files(id, name, mimeType)").execute()
|
67 |
+
items = results.get('files', [])
|
68 |
+
|
69 |
+
content = ''
|
70 |
+
for item in items:
|
71 |
+
print(f"Processing file: {item['name']}")
|
72 |
+
file_stream = download_file(item['id'])
|
73 |
+
content += str(process_pdf(file_stream))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
return content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|