|
import time |
|
import gradio as gr |
|
import os |
|
from UI.JS.ShowAndDisable import GetHideObject |
|
from UI.LoginPanel import LoginPanel |
|
|
|
|
|
class MainBlocks: |
|
def __init__(self, name): |
|
self.loginPanel = None |
|
self.name = name |
|
self.main = self.GetDemo() |
|
self.main.queue() |
|
self.main.launch() |
|
|
|
|
|
def add_text(self, history, text): |
|
history = history + [(text, None)] |
|
return history, gr.update(value="", interactive=False) |
|
|
|
def add_file(self, history, file): |
|
history = history + [((file.name,), None)] |
|
return history |
|
|
|
def bot(self, history): |
|
if self.loginPanel.sub == 0: |
|
response = "請登錄再聊" |
|
|
|
else: |
|
response = "hey its cool" |
|
history[-1][1] = "" |
|
for character in response: |
|
history[-1][1] += character |
|
time.sleep(0.05) |
|
yield history |
|
|
|
def GetDemo(self): |
|
with gr.Blocks() as demo: |
|
|
|
self.loginPanel = LoginPanel() |
|
buttonAddText = gr.Button(value="Submit", elem_id="my-button") |
|
|
|
buttonAddText.click(None, _js=GetHideObject("my-button")) |
|
chatbot = gr.Chatbot( |
|
[], |
|
elem_id="chatbot", |
|
bubble_full_width=False, |
|
show_label=False, |
|
|
|
avatar_images=("https://huggingface.co/front/assets/huggingface_logo-noborder.svg", (os.path.join(os.path.dirname(__file__), "../gradiodemo/Demo/IMG/didi.jpeg"))), |
|
) |
|
with gr.Row(): |
|
txt = gr.Textbox( |
|
scale=4, |
|
show_label=False, |
|
placeholder="Enter text and press enter", |
|
container=False, |
|
) |
|
|
|
|
|
txt_msg = txt.submit(self.add_text, [chatbot, txt], [chatbot, txt], queue=False) |
|
txt_msg.then( |
|
self.bot, chatbot, chatbot |
|
) |
|
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False, _js=GetHideObject("my-button")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
return demo |
|
|
|
|
|
if __name__ == "__main__": |
|
mainBlocks1 = MainBlocks("test") |
|
|