lindsay-qu commited on
Commit
62bb2b9
·
verified ·
1 Parent(s): 67b6ddf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -7
app.py CHANGED
@@ -4,6 +4,13 @@ import models
4
  import time
5
  import gradio as gr
6
  import os
 
 
 
 
 
 
 
7
 
8
  api_key = os.environ["OPENAI_API_KEY"]
9
  api_base = os.environ["OPENAI_API_BASE"]
@@ -20,14 +27,17 @@ def chatbot_initialize():
20
  Chatbot = core.chatbot.RetrievalChatbot(retriever=retriever)
21
  return Chatbot
22
 
23
- def respond(query, chat_history, img_path, chat_history_string):
 
24
  global Chatbot
25
- response, logs = Chatbot.response(query, image_path=img_path, return_logs=True)
26
  chat_history.append((query, response))
27
- if img_path is None:
28
- chat_history_string += "Query: " + query + "\nImage: None" + "\nRepsonse: " + response + "\n\n\n"
29
  else:
30
- chat_history_string += "Query: " + query + "\nImage: " + img_path + "\nRepsonse: " + response + "\n\n\n"
 
 
31
  return "", chat_history, logs, chat_history_string
32
 
33
  if __name__ == "__main__":
@@ -35,14 +45,21 @@ if __name__ == "__main__":
35
  Chatbot=chatbot_initialize()
36
 
37
  with gr.Blocks() as demo:
 
 
 
 
 
 
 
38
  with gr.Row():
39
  with gr.Column(scale=2):
40
  chatbot = gr.Chatbot()
41
  msg = gr.Textbox(label="Query", show_label=True)
42
- img = gr.Image(type="filepath")
43
  clear = gr.ClearButton([msg, chatbot])
44
  with gr.Column(scale=1):
45
  sidebar = gr.Textbox(label="Subquestions", show_label=True, show_copy_button=True, interactive=False, max_lines=30)
46
  history = gr.Textbox(label="Copy Chat History", show_label=True, show_copy_button=True, interactive=False, max_lines=5)
47
- msg.submit(respond, inputs=[msg, chatbot, img, history], outputs=[msg, chatbot, sidebar, history])
48
  demo.queue().launch()
 
4
  import time
5
  import gradio as gr
6
  import os
7
+ import asyncio
8
+ import time
9
+
10
+ # openai.api_base = "https://lonlie.plus7.plus/v1"
11
+ # openai.api_key = "sk-ac6SSdDqwB6Syk0U3e88FcF49f6b4c3c9c4e0247A38aB699"
12
+ # os.environ["OPENAI_API_KEY"]="sk-ac6SSdDqwB6Syk0U3e88FcF49f6b4c3c9c4e0247A38aB699"
13
+ # os.environ["OPENAI_API_BASE"]="https://lonlie.plus7.plus/v1"
14
 
15
  api_key = os.environ["OPENAI_API_KEY"]
16
  api_base = os.environ["OPENAI_API_BASE"]
 
27
  Chatbot = core.chatbot.RetrievalChatbot(retriever=retriever)
28
  return Chatbot
29
 
30
+ async def respond(query, chat_history, img_path_list, chat_history_string):
31
+ time1 = time.time()
32
  global Chatbot
33
+ response, logs = await Chatbot.response(query, image_paths=img_path_list, return_logs=True)
34
  chat_history.append((query, response))
35
+ if img_path_list is None:
36
+ chat_history_string += "Query: " + query + "\nImage: None" + "\nResponse: " + response + "\n\n\n"
37
  else:
38
+ chat_history_string += "Query: " + query + "\nImages: " + "\n".join([path.name for path in img_path_list]) + "\nResponse: " + response + "\n\n\n"
39
+ time2 = time.time()
40
+ print(f"Total: {time2-time1}")
41
  return "", chat_history, logs, chat_history_string
42
 
43
  if __name__ == "__main__":
 
45
  Chatbot=chatbot_initialize()
46
 
47
  with gr.Blocks() as demo:
48
+ # chat = gr.ChatInterface(
49
+ # fn=respond,
50
+ # chatbot=gr.Chatbot(show_label=True, show_copy_button=True),
51
+ # additional_inputs=[
52
+ # gr.Image(type="filepath"),
53
+ # ]
54
+ # )
55
  with gr.Row():
56
  with gr.Column(scale=2):
57
  chatbot = gr.Chatbot()
58
  msg = gr.Textbox(label="Query", show_label=True)
59
+ imgs = gr.File(file_count='multiple', file_types=['image'], type="file", label='Upload Images')
60
  clear = gr.ClearButton([msg, chatbot])
61
  with gr.Column(scale=1):
62
  sidebar = gr.Textbox(label="Subquestions", show_label=True, show_copy_button=True, interactive=False, max_lines=30)
63
  history = gr.Textbox(label="Copy Chat History", show_label=True, show_copy_button=True, interactive=False, max_lines=5)
64
+ msg.submit(respond, inputs=[msg, chatbot, imgs, history], outputs=[msg, chatbot, sidebar, history])
65
  demo.queue().launch()