Update app.py
Browse files
app.py
CHANGED
@@ -98,6 +98,13 @@ def _parse_text(text):
|
|
98 |
text = "".join(lines)
|
99 |
return text
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
def _launch_demo(args, model, tokenizer):
|
103 |
uploaded_file_dir = os.environ.get("GRADIO_TEMP_DIR") or str(
|
@@ -122,13 +129,27 @@ def _launch_demo(args, model, tokenizer):
|
|
122 |
|
123 |
if 'image' in query[0]:
|
124 |
image = tokenizer.draw_bbox_on_latest_picture(response)
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
127 |
else:
|
128 |
_chatbot[-1] = (chat_query, response)
|
129 |
-
|
130 |
return _chatbot
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
def regenerate(_chatbot, task_history):
|
133 |
if not task_history:
|
134 |
return _chatbot
|
@@ -180,15 +201,6 @@ def _launch_demo(args, model, tokenizer):
|
|
180 |
def reset_state(task_history):
|
181 |
task_history.clear()
|
182 |
return []
|
183 |
-
|
184 |
-
def save_image(image):
|
185 |
-
temp_dir = secrets.token_hex(20)
|
186 |
-
temp_dir = Path(uploaded_file_dir) / temp_dir
|
187 |
-
temp_dir.mkdir(exist_ok=True, parents=True)
|
188 |
-
name = f"tmp{secrets.token_hex(5)}.jpg"
|
189 |
-
filename = temp_dir / name
|
190 |
-
image.save(str(filename))
|
191 |
-
return str(filename)
|
192 |
|
193 |
|
194 |
with gr.Blocks() as demo:
|
@@ -219,6 +231,12 @@ Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder
|
|
219 |
outputs=[chatbot]
|
220 |
)
|
221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
submit_file_btn.click(
|
223 |
fn=add_file,
|
224 |
inputs=[chatbot, task_history, file_upload],
|
|
|
98 |
text = "".join(lines)
|
99 |
return text
|
100 |
|
101 |
+
def add_file(history, task_history, file):
|
102 |
+
if file is None:
|
103 |
+
return history, task_history # Return if no file is uploaded
|
104 |
+
file_path = save_image(file)
|
105 |
+
history = history + [((file_path,), None)]
|
106 |
+
task_history = task_history + [((file_path,), None)]
|
107 |
+
return history, task_history
|
108 |
|
109 |
def _launch_demo(args, model, tokenizer):
|
110 |
uploaded_file_dir = os.environ.get("GRADIO_TEMP_DIR") or str(
|
|
|
129 |
|
130 |
if 'image' in query[0]:
|
131 |
image = tokenizer.draw_bbox_on_latest_picture(response)
|
132 |
+
if image is not None:
|
133 |
+
image_path = save_image(image)
|
134 |
+
_chatbot[-1] = (chat_query, (image_path,))
|
135 |
+
else:
|
136 |
+
_chatbot[-1] = (chat_query, "No image to display.")
|
137 |
else:
|
138 |
_chatbot[-1] = (chat_query, response)
|
139 |
+
|
140 |
return _chatbot
|
141 |
|
142 |
+
def save_image(image):
|
143 |
+
if image is None:
|
144 |
+
return None
|
145 |
+
temp_dir = secrets.token_hex(20)
|
146 |
+
temp_dir = Path(uploaded_file_dir) / temp_dir
|
147 |
+
temp_dir.mkdir(exist_ok=True, parents=True)
|
148 |
+
name = f"tmp{secrets.token_hex(5)}.jpg"
|
149 |
+
filename = temp_dir / name
|
150 |
+
image.save(str(filename))
|
151 |
+
return str(filename)
|
152 |
+
|
153 |
def regenerate(_chatbot, task_history):
|
154 |
if not task_history:
|
155 |
return _chatbot
|
|
|
201 |
def reset_state(task_history):
|
202 |
task_history.clear()
|
203 |
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
|
206 |
with gr.Blocks() as demo:
|
|
|
231 |
outputs=[chatbot]
|
232 |
)
|
233 |
|
234 |
+
submit_file_btn.click(
|
235 |
+
fn=predict,
|
236 |
+
inputs=[chatbot, task_history],
|
237 |
+
outputs=[chatbot]
|
238 |
+
)
|
239 |
+
|
240 |
submit_file_btn.click(
|
241 |
fn=add_file,
|
242 |
inputs=[chatbot, task_history, file_upload],
|