Spaces:
Sleeping
Sleeping
debug text_feedback, add comments
Browse files- app_drive.py +57 -32
app_drive.py
CHANGED
@@ -10,11 +10,12 @@ from lc_base.dnd_database import create_dnd_database
|
|
10 |
from driveapi.drive import upload_chat_to_drive
|
11 |
from driveapi.drive_database import create_chroma_db
|
12 |
|
13 |
-
|
|
|
14 |
time_diff = 0
|
15 |
-
|
16 |
-
# model_name = "gpt-4-1106-preview"
|
17 |
-
model_name = "gpt-4-0125-preview"
|
18 |
search_type = "stuff"
|
19 |
input_question = ""
|
20 |
model_response = ""
|
@@ -24,13 +25,7 @@ dir = ""
|
|
24 |
title = """<h1 align="center">ResearchBuddy</h1>"""
|
25 |
description = """<br><br><h3 align="center">This is a GPT based Research Buddy to assist in navigating new research topics.</h3>"""
|
26 |
|
27 |
-
|
28 |
-
def save_drive_link(drive_link):
|
29 |
-
drive_link += "?usp=sharing"
|
30 |
-
os.environ['DRIVE_LINK'] = str(drive_link)
|
31 |
-
print("Drive link saved in the environment")
|
32 |
-
return None
|
33 |
-
|
34 |
def create_data_from_drive(drive_link):
|
35 |
global db
|
36 |
|
@@ -41,11 +36,13 @@ def create_data_from_drive(drive_link):
|
|
41 |
db = create_chroma_db()
|
42 |
return "Processing Completed - You can start the chat now!"
|
43 |
|
|
|
44 |
def check_pdfs(pdf_files):
|
45 |
global db
|
46 |
db = create_dnd_database(pdf_files)
|
47 |
return "Processing Completed - You can start the chat now!"
|
48 |
|
|
|
49 |
def user(user_message, history):
|
50 |
return "", history + [[user_message, None]]
|
51 |
|
@@ -53,20 +50,15 @@ def respond(message, chat_history):
|
|
53 |
|
54 |
global time_diff, model_response, input_question
|
55 |
|
56 |
-
print("Database is ...................")
|
57 |
-
print(type(db))
|
58 |
question = str(message)
|
59 |
chain = openai_chain(inp_dir=dir)
|
60 |
|
61 |
query = question
|
62 |
-
|
63 |
start_time = time.time()
|
64 |
|
65 |
output = chain.get_response_from_drive(query=query, database=db, k=10, model_name=model_name, type=search_type)
|
66 |
-
print(output)
|
67 |
|
68 |
-
|
69 |
-
# Update global variables to log
|
70 |
time_diff = time.time() - start_time
|
71 |
model_response = output
|
72 |
input_question = question
|
@@ -77,6 +69,8 @@ def respond(message, chat_history):
|
|
77 |
time.sleep(2)
|
78 |
return " ", chat_history
|
79 |
|
|
|
|
|
80 |
def save_feedback(feedback):
|
81 |
global user_feedback
|
82 |
user_feedback = feedback
|
@@ -98,7 +92,7 @@ def default_feedback():
|
|
98 |
def default_text():
|
99 |
return ""
|
100 |
|
101 |
-
def
|
102 |
global text_feedback
|
103 |
text_feedback = feedback
|
104 |
|
@@ -111,17 +105,19 @@ def text_feedback(feedback):
|
|
111 |
|
112 |
upload_chat_to_drive(log_data, file_name)
|
113 |
|
|
|
|
|
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 |
with gr.Row(equal_height=True):
|
120 |
with gr.Column():
|
121 |
with gr.Row():
|
122 |
pdf_files_dnd = gr.File(file_count='multiple', height=250, label="Upload PDF Files")
|
123 |
|
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...")
|
@@ -129,16 +125,25 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", neutral_hue="slate"))
|
|
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 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
chatbot = gr.Chatbot(height=750)
|
139 |
msg = gr.Textbox(label="Send a message", placeholder="Send a message",
|
140 |
show_label=False, container=False)
|
141 |
|
|
|
142 |
with gr.Row():
|
143 |
with gr.Column():
|
144 |
gr.Examples([
|
@@ -149,6 +154,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", neutral_hue="slate"))
|
|
149 |
], inputs=msg, label= "Click on any example to copy in the chatbox"
|
150 |
)
|
151 |
|
|
|
152 |
with gr.Row():
|
153 |
with gr.Column():
|
154 |
feedback_radio = gr.Radio(
|
@@ -161,27 +167,46 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", neutral_hue="slate"))
|
|
161 |
feedback_text = gr.Textbox(lines=1, label="Additional comments on the current response...")
|
162 |
|
163 |
|
164 |
-
|
165 |
-
msg.submit(
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
|
|
168 |
feedback_radio.change(
|
169 |
fn=save_feedback,
|
170 |
inputs=[feedback_radio]
|
171 |
)
|
172 |
|
173 |
feedback_text.submit(
|
174 |
-
fn=
|
175 |
-
inputs=[feedback_text]
|
|
|
176 |
)
|
177 |
|
178 |
feedback_text.submit(
|
179 |
fn=default_text,
|
180 |
-
outputs=[feedback_text]
|
|
|
181 |
)
|
182 |
|
|
|
183 |
gr.HTML(description)
|
184 |
|
185 |
-
|
186 |
chat.queue()
|
187 |
chat.launch()
|
|
|
10 |
from driveapi.drive import upload_chat_to_drive
|
11 |
from driveapi.drive_database import create_chroma_db
|
12 |
|
13 |
+
############################# Global Params #############################
|
14 |
+
|
15 |
time_diff = 0
|
16 |
+
model_name="gpt-3.5-turbo-1106" # FOR TESTING
|
17 |
+
# model_name = "gpt-4-1106-preview"
|
18 |
+
# model_name = "gpt-4-0125-preview"
|
19 |
search_type = "stuff"
|
20 |
input_question = ""
|
21 |
model_response = ""
|
|
|
25 |
title = """<h1 align="center">ResearchBuddy</h1>"""
|
26 |
description = """<br><br><h3 align="center">This is a GPT based Research Buddy to assist in navigating new research topics.</h3>"""
|
27 |
|
28 |
+
############################# Drive API specific function #############################
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
def create_data_from_drive(drive_link):
|
30 |
global db
|
31 |
|
|
|
36 |
db = create_chroma_db()
|
37 |
return "Processing Completed - You can start the chat now!"
|
38 |
|
39 |
+
############################# Drag and Drop PDF processing #############################
|
40 |
def check_pdfs(pdf_files):
|
41 |
global db
|
42 |
db = create_dnd_database(pdf_files)
|
43 |
return "Processing Completed - You can start the chat now!"
|
44 |
|
45 |
+
############################# Chatbot Specific functions #############################
|
46 |
def user(user_message, history):
|
47 |
return "", history + [[user_message, None]]
|
48 |
|
|
|
50 |
|
51 |
global time_diff, model_response, input_question
|
52 |
|
|
|
|
|
53 |
question = str(message)
|
54 |
chain = openai_chain(inp_dir=dir)
|
55 |
|
56 |
query = question
|
|
|
57 |
start_time = time.time()
|
58 |
|
59 |
output = chain.get_response_from_drive(query=query, database=db, k=10, model_name=model_name, type=search_type)
|
|
|
60 |
|
61 |
+
# Update global variables for logging
|
|
|
62 |
time_diff = time.time() - start_time
|
63 |
model_response = output
|
64 |
input_question = question
|
|
|
69 |
time.sleep(2)
|
70 |
return " ", chat_history
|
71 |
|
72 |
+
############################# Feedback Specific functions #############################
|
73 |
+
|
74 |
def save_feedback(feedback):
|
75 |
global user_feedback
|
76 |
user_feedback = feedback
|
|
|
92 |
def default_text():
|
93 |
return ""
|
94 |
|
95 |
+
def save_text_feedback(feedback):
|
96 |
global text_feedback
|
97 |
text_feedback = feedback
|
98 |
|
|
|
105 |
|
106 |
upload_chat_to_drive(log_data, file_name)
|
107 |
|
108 |
+
|
109 |
+
############################# Gradio Application Block #############################
|
110 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", neutral_hue="slate")) as chat:
|
111 |
gr.HTML(title)
|
112 |
|
113 |
global db
|
114 |
|
115 |
+
# PDF Drag and Drop + Drive link Input + Status containers
|
116 |
with gr.Row(equal_height=True):
|
117 |
with gr.Column():
|
118 |
with gr.Row():
|
119 |
pdf_files_dnd = gr.File(file_count='multiple', height=250, label="Upload PDF Files")
|
120 |
|
|
|
121 |
with gr.Column():
|
122 |
with gr.Row():
|
123 |
drive_link_input = gr.Textbox(lines=1, label="Enter your shared drive link, then press Enter...")
|
|
|
125 |
status_message = gr.Text(label="Status", value="⬆️Submit a (shared) drive link containing only PDFs \n-or- \n⬅️Upload PDF files", text_align='center')
|
126 |
|
127 |
|
128 |
+
# What happens when PDF is uploaded or a drive link is submitted
|
129 |
+
drive_link_input.submit(
|
130 |
+
fn = create_data_from_drive,
|
131 |
+
inputs = [drive_link_input],
|
132 |
+
outputs = [status_message])
|
133 |
+
|
134 |
+
pdf_files_dnd.change(
|
135 |
+
fn=check_pdfs,
|
136 |
+
inputs=[pdf_files_dnd],
|
137 |
+
outputs=[status_message],
|
138 |
+
preprocess=False,
|
139 |
+
postprocess=False) # Set preprocess and postprocess to False, to avoid the tmpfile object creation, instead get a Dict
|
140 |
+
|
141 |
+
# Chatbot container
|
142 |
chatbot = gr.Chatbot(height=750)
|
143 |
msg = gr.Textbox(label="Send a message", placeholder="Send a message",
|
144 |
show_label=False, container=False)
|
145 |
|
146 |
+
# Sample questions
|
147 |
with gr.Row():
|
148 |
with gr.Column():
|
149 |
gr.Examples([
|
|
|
154 |
], inputs=msg, label= "Click on any example to copy in the chatbox"
|
155 |
)
|
156 |
|
157 |
+
# Feedback options container
|
158 |
with gr.Row():
|
159 |
with gr.Column():
|
160 |
feedback_radio = gr.Radio(
|
|
|
167 |
feedback_text = gr.Textbox(lines=1, label="Additional comments on the current response...")
|
168 |
|
169 |
|
170 |
+
# Change when a message is submitted to the chatbot
|
171 |
+
msg.submit(
|
172 |
+
fn = respond,
|
173 |
+
inputs = [msg, chatbot],
|
174 |
+
outputs = [msg, chatbot],
|
175 |
+
queue = True)
|
176 |
+
|
177 |
+
msg.submit(
|
178 |
+
fn = default_feedback,
|
179 |
+
outputs=[feedback_radio],
|
180 |
+
queue = True
|
181 |
+
)
|
182 |
+
|
183 |
+
# Whenever there is some change in the chatbot
|
184 |
+
chatbot.change(
|
185 |
+
fn = save_feedback,
|
186 |
+
inputs=[feedback_radio],
|
187 |
+
queue = True)
|
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 |
feedback_text.submit(
|
202 |
fn=default_text,
|
203 |
+
outputs=[feedback_text],
|
204 |
+
queue=True
|
205 |
)
|
206 |
|
207 |
+
# Description at the bottom of the application
|
208 |
gr.HTML(description)
|
209 |
|
210 |
+
# Enable queing
|
211 |
chat.queue()
|
212 |
chat.launch()
|