Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -263,12 +263,16 @@ with gr.Blocks() as demo:
|
|
| 263 |
label='Select Model',
|
| 264 |
value='gpt-4o-mini'
|
| 265 |
)
|
| 266 |
-
|
| 267 |
# Chat Interface
|
| 268 |
-
chatbot = gr.Chatbot(label="Chat History",
|
| 269 |
msg = gr.Textbox(label="Enter your question here", lines=2)
|
| 270 |
submit_btn = gr.Button("Submit")
|
| 271 |
clear = gr.ClearButton([msg, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
# Example Questions
|
| 274 |
gr.Examples(
|
|
@@ -277,52 +281,50 @@ with gr.Blocks() as demo:
|
|
| 277 |
label="PRE-DEFINED QUESTIONS: Click on a question to auto-fill the input box",
|
| 278 |
)
|
| 279 |
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
chat_history = []
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
if url_value.strip() == '' and file_value is None:
|
| 295 |
-
chat_history.append({"role": "user", "content": message})
|
| 296 |
-
chat_history.append({"role": "assistant", "content": '[ERROR]: Both URL and PDF are empty. Provide at least one'})
|
| 297 |
-
return "", chat_history
|
| 298 |
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
load_recommender('corpus.pdf')
|
| 303 |
-
else:
|
| 304 |
-
old_file_name = file_value.name
|
| 305 |
-
file_name = old_file_name[:-12] + old_file_name[-4:]
|
| 306 |
-
os.rename(old_file_name, file_name)
|
| 307 |
-
load_recommender(file_name)
|
| 308 |
|
| 309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
|
| 314 |
-
|
|
|
|
|
|
|
| 315 |
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
return "", chat_history
|
| 320 |
|
| 321 |
submit_btn.click(
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
|
| 327 |
msg.submit(
|
| 328 |
respond,
|
|
|
|
| 263 |
label='Select Model',
|
| 264 |
value='gpt-4o-mini'
|
| 265 |
)
|
| 266 |
+
|
| 267 |
# Chat Interface
|
| 268 |
+
chatbot = gr.Chatbot(label="Chat History") # default: list of [user, bot] pairs
|
| 269 |
msg = gr.Textbox(label="Enter your question here", lines=2)
|
| 270 |
submit_btn = gr.Button("Submit")
|
| 271 |
clear = gr.ClearButton([msg, chatbot])
|
| 272 |
+
#chatbot = gr.Chatbot(label="Chat History", type="messages")
|
| 273 |
+
#msg = gr.Textbox(label="Enter your question here", lines=2)
|
| 274 |
+
#submit_btn = gr.Button("Submit")
|
| 275 |
+
#clear = gr.ClearButton([msg, chatbot])
|
| 276 |
|
| 277 |
# Example Questions
|
| 278 |
gr.Examples(
|
|
|
|
| 281 |
label="PRE-DEFINED QUESTIONS: Click on a question to auto-fill the input box",
|
| 282 |
)
|
| 283 |
|
| 284 |
+
def respond(message, chat_history, url_value, file_value, key_value, model_value):
|
| 285 |
+
if message.strip() == "":
|
| 286 |
+
return "", chat_history # nothing to do
|
| 287 |
|
| 288 |
+
# Initialize chat_history as a list of [user, assistant] pairs
|
| 289 |
+
if chat_history is None:
|
| 290 |
+
chat_history = []
|
|
|
|
| 291 |
|
| 292 |
+
try:
|
| 293 |
+
# Basic validations
|
| 294 |
+
if key_value.strip() == '':
|
| 295 |
+
chat_history.append([message, '[ERROR]: Please enter your OpenAI API key'])
|
| 296 |
+
return "", chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
+
if url_value.strip() == '' and file_value is None:
|
| 299 |
+
chat_history.append([message, '[ERROR]: Both URL and PDF are empty. Provide at least one'])
|
| 300 |
+
return "", chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
+
# Prepare corpus (URL or uploaded file)
|
| 303 |
+
if url_value.strip() != '':
|
| 304 |
+
download_pdf(url_value, 'corpus.pdf')
|
| 305 |
+
load_recommender('corpus.pdf')
|
| 306 |
+
else:
|
| 307 |
+
old_file_name = file_value.name
|
| 308 |
+
file_name = old_file_name[:-12] + old_file_name[-4:]
|
| 309 |
+
os.rename(old_file_name, file_name)
|
| 310 |
+
load_recommender(file_name)
|
| 311 |
|
| 312 |
+
# Generate answer
|
| 313 |
+
answer = generate_answer(message, key_value, model_value)
|
| 314 |
|
| 315 |
+
# Append pair [user, assistant]
|
| 316 |
+
chat_history.append([message, answer])
|
| 317 |
+
return "", chat_history
|
| 318 |
|
| 319 |
+
except Exception as e:
|
| 320 |
+
chat_history.append([message, f'[ERROR]: {str(e)}'])
|
| 321 |
+
return "", chat_history
|
|
|
|
| 322 |
|
| 323 |
submit_btn.click(
|
| 324 |
+
respond,
|
| 325 |
+
[msg, chatbot, url, file, openAI_key, model],
|
| 326 |
+
[msg, chatbot]
|
| 327 |
+
)
|
| 328 |
|
| 329 |
msg.submit(
|
| 330 |
respond,
|