Skip to content

Commit

Permalink
Check if image is ready before chatting about it (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
felladrin committed Jul 24, 2023
1 parent 242dbfd commit acc15e6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion minigpt4/webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
description = """<h3>This is the demo of MiniGPT-4 with ggml (cpu only!). Upload your images and start chatting!</h3>"""
article = """<div style='display:flex; gap: 0.25rem; '><a href='https://github.com/Vision-CAIR/MiniGPT-4'><img src='https://img.shields.io/badge/Github-Code-blue'></a></div>
"""
image_ready = False

global minigpt4_chatbot
minigpt4_chatbot: minigpt4_library.MiniGPT4ChatBot
Expand All @@ -27,6 +28,9 @@ def user(message, history):
def chat(history, limit: int = 1024, temp: float = 0.8, top_k: int = 40, top_p: float = 0.9, repeat_penalty: float = 1.1):
history = history or []

if not image_ready:
return "Please upload an image first.", history

message = history[-1][0]

history[-1][1] = ""
Expand All @@ -43,15 +47,19 @@ def chat(history, limit: int = 1024, temp: float = 0.8, top_k: int = 40, top_p:
yield history, history

def clear_state(history, chat_message, image):
global image_ready
history = []
minigpt4_chatbot.reset_chat()
image_ready = False
return history, gr.update(value=None, interactive=True), gr.update(placeholder='Upload image first', interactive=False), gr.update(value="Upload & Start Chat", interactive=True)

def upload_image(image, history):
global image_ready
if image is None:
return None, None, gr.update(interactive=True), history
history = []
minigpt4_chatbot.upload_image(image.convert('RGB'))
image_ready = True
return gr.update(interactive=False), gr.update(interactive=True, placeholder='Type and press Enter'), gr.update(value="Start Chatting", interactive=False), history

def start(share: bool):
Expand Down Expand Up @@ -119,4 +127,4 @@ def start(share: bool):
exit(1)

minigpt4_chatbot = minigpt4_library.MiniGPT4ChatBot(model_path, llm_model_path, verbosity=minigpt4_library.Verbosity.SILENT)
start(share_link)
start(share_link)

0 comments on commit acc15e6

Please sign in to comment.