tilents
commited on
Commit
·
5cfdf88
1
Parent(s):
dac36dc
test1
Browse files- .gitignore +3 -0
- UI/MainBlocks.py +7 -0
- app.py +50 -4
- gradiodemo/Demo/AddRemoveItem.py +34 -0
- gradiodemo/Demo/ChatBotSimple.py +24 -0
- gradiodemo/Demo/ChatDemo.py +51 -0
- gradiodemo/Demo/ChatInput.py +53 -0
- gradiodemo/Demo/ChatInterface/Simple.py +7 -0
- gradiodemo/Demo/ClickA.py +39 -0
- gradiodemo/Demo/IMG/didi.jpeg +0 -0
- gradiodemo/Demo/Login.py +6 -0
- gradiodemo/Demo/chatbot_multimodel.py +63 -0
- gradiodemo/Demo/mHtl/D.py +17 -0
- gradiodemo/__init__.py +0 -0
- gradiodemo/button.html +9 -0
- gradiodemo/test.py +7 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
/gradiodemo/flagged/
|
2 |
+
/chatdemo/
|
3 |
+
/gradio_cached_examples/
|
UI/MainBlocks.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class MainBlocks:
|
2 |
+
def __init__(self, name, age):
|
3 |
+
self.name = name
|
4 |
+
self.age = age
|
5 |
+
|
6 |
+
def greet(self):
|
7 |
+
return f"Hello, my name is {self.name} and I am {self.age} years old."
|
app.py
CHANGED
@@ -1,7 +1,53 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import time
|
4 |
|
5 |
+
# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
|
6 |
+
def add_text(history, text):
|
7 |
+
history = history + [(text, None)]
|
8 |
+
return history, gr.update(value="", interactive=False)
|
9 |
+
def add_file(history, file):
|
10 |
+
history = history + [((file.name,), None)]
|
11 |
+
return history
|
12 |
|
13 |
+
|
14 |
+
def bot(history):
|
15 |
+
response = "**That's cool!**"
|
16 |
+
history[-1][1] = ""
|
17 |
+
for character in response:
|
18 |
+
history[-1][1] += character
|
19 |
+
time.sleep(0.05)
|
20 |
+
yield history
|
21 |
+
|
22 |
+
|
23 |
+
with gr.Blocks() as demo:
|
24 |
+
buttonAddText = gr.Button("添加文本框")
|
25 |
+
chatbot = gr.Chatbot(
|
26 |
+
[],
|
27 |
+
elem_id="chatbot",
|
28 |
+
bubble_full_width=False,
|
29 |
+
avatar_images=(None, (os.path.join(os.path.dirname(__file__), "./gradiodemo/Demo/IMG/didi.jpeg"))),
|
30 |
+
)
|
31 |
+
|
32 |
+
with gr.Row():
|
33 |
+
txt = gr.Textbox(
|
34 |
+
scale=4,
|
35 |
+
show_label=False,
|
36 |
+
placeholder="Enter text and press enter, or upload an image",
|
37 |
+
container=False,
|
38 |
+
)
|
39 |
+
btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
45 |
+
bot, chatbot, chatbot
|
46 |
+
)
|
47 |
+
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
48 |
+
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
|
49 |
+
bot, chatbot, chatbot
|
50 |
+
)
|
51 |
+
if __name__ == "__main__":
|
52 |
+
demo.queue()
|
53 |
+
demo.launch(share=False,server_port=8080)
|
gradiodemo/Demo/AddRemoveItem.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
import time
|
4 |
+
# 定义一个 JavaScript 函数来隐藏按钮
|
5 |
+
hide_button_js = """
|
6 |
+
function hideButton() {
|
7 |
+
var button = document.getElementById('my-button');
|
8 |
+
button.style.display = 'none';
|
9 |
+
}
|
10 |
+
"""
|
11 |
+
with gr.Blocks() as demo:
|
12 |
+
chatbot = gr.Chatbot()
|
13 |
+
# 添加按钮到界面
|
14 |
+
btn = gr.Button(value="Submit", elem_id="my-button")
|
15 |
+
btn.click(
|
16 |
+
None, _js=hide_button_js
|
17 |
+
)
|
18 |
+
# btn.visible = False
|
19 |
+
msg = gr.Textbox()
|
20 |
+
clear = gr.ClearButton([msg, chatbot])
|
21 |
+
|
22 |
+
def respond(message, chat_history):
|
23 |
+
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
24 |
+
chat_history.append((message, bot_message))
|
25 |
+
time.sleep(2)
|
26 |
+
print(chat_history)
|
27 |
+
btn.visible = True
|
28 |
+
gr.update(value="", interactive=True)
|
29 |
+
return "", chat_history,btn
|
30 |
+
|
31 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
32 |
+
|
33 |
+
if __name__ == "__main__":
|
34 |
+
demo.launch()
|
gradiodemo/Demo/ChatBotSimple.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
import time
|
4 |
+
|
5 |
+
with gr.Blocks() as demo:
|
6 |
+
chatbot = gr.Chatbot()
|
7 |
+
btn = gr.Button(value="Submit")
|
8 |
+
btn.visible = False
|
9 |
+
msg = gr.Textbox()
|
10 |
+
clear = gr.ClearButton([msg, chatbot])
|
11 |
+
|
12 |
+
def respond(message, chat_history):
|
13 |
+
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
14 |
+
chat_history.append((message, bot_message))
|
15 |
+
time.sleep(2)
|
16 |
+
print(chat_history)
|
17 |
+
btn.visible = True
|
18 |
+
gr.update(value="", interactive=True)
|
19 |
+
return "", chat_history,btn
|
20 |
+
|
21 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
22 |
+
|
23 |
+
if __name__ == "__main__":
|
24 |
+
demo.launch()
|
gradiodemo/Demo/ChatDemo.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
def chat(message, history):
|
5 |
+
history = history or []
|
6 |
+
message = message.lower()
|
7 |
+
if message.startswith("how many"):
|
8 |
+
response = random.randint(1, 10)+"d"
|
9 |
+
elif message.startswith("how"):
|
10 |
+
response = random.choice(["Great", "Good", "Okay", "Bad"])
|
11 |
+
elif message.startswith("where"):
|
12 |
+
response = random.choice(["Here", "There", "Somewhere"])
|
13 |
+
else:
|
14 |
+
response = "I don't know"
|
15 |
+
history.append((message, response))
|
16 |
+
return history, history
|
17 |
+
|
18 |
+
import random
|
19 |
+
import gradio as gr
|
20 |
+
|
21 |
+
import time
|
22 |
+
|
23 |
+
def echo(message, history, system_prompt, tokens):
|
24 |
+
response = f"System prompt: {system_prompt}\n Message: {message}."
|
25 |
+
for i in range(min(len(response), int(tokens))):
|
26 |
+
time.sleep(0.05)
|
27 |
+
yield response[: i+1]
|
28 |
+
|
29 |
+
with gr.Blocks() as demo:
|
30 |
+
system_prompt = gr.Textbox("You are helpful AI.", label="System Prompt")
|
31 |
+
slider = gr.Slider(10, 100, render=False)
|
32 |
+
|
33 |
+
gr.ChatInterface(
|
34 |
+
echo, additional_inputs=[system_prompt, slider]
|
35 |
+
)
|
36 |
+
|
37 |
+
demo.queue().launch()
|
38 |
+
|
39 |
+
# # 设置一个对话窗
|
40 |
+
# chatbot = gr.Chatbot()
|
41 |
+
# demo = gr.Interface(
|
42 |
+
# chat,
|
43 |
+
# # 添加state组件
|
44 |
+
# ["text", "state"],
|
45 |
+
# [chatbot, "state"],
|
46 |
+
# # 设置没有保存数据的按钮
|
47 |
+
# allow_flagging="never",
|
48 |
+
# )
|
49 |
+
# demo.launch()
|
50 |
+
# demo.launch(share=True,server_port=8080,server_name="0.0.0.0")
|
51 |
+
print("運行成功了")
|
gradiodemo/Demo/ChatInput.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
def add_text(history, text):
|
8 |
+
history = history + [(text, None)]
|
9 |
+
return history, gr.update(value="", interactive=False)
|
10 |
+
|
11 |
+
|
12 |
+
def add_file(history, file):
|
13 |
+
history = history + [((file.name,), None)]
|
14 |
+
return history
|
15 |
+
|
16 |
+
|
17 |
+
def bot(history):
|
18 |
+
response = "**That's cool!**"
|
19 |
+
history[-1][1] = ""
|
20 |
+
for character in response:
|
21 |
+
history[-1][1] += character
|
22 |
+
time.sleep(0.05)
|
23 |
+
yield history
|
24 |
+
|
25 |
+
|
26 |
+
with gr.Blocks() as demo:
|
27 |
+
chatbot = gr.Chatbot(
|
28 |
+
[],
|
29 |
+
elem_id="chatbot",
|
30 |
+
bubble_full_width=False,
|
31 |
+
avatar_images=(None, (os.path.join(os.path.dirname(__file__), "avatar.png"))),
|
32 |
+
)
|
33 |
+
|
34 |
+
with gr.Row():
|
35 |
+
txt = gr.Textbox(
|
36 |
+
scale=4,
|
37 |
+
show_label=False,
|
38 |
+
placeholder="Enter text and press enter, or upload an image",
|
39 |
+
container=False,
|
40 |
+
)
|
41 |
+
btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
|
42 |
+
|
43 |
+
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
44 |
+
bot, chatbot, chatbot
|
45 |
+
)
|
46 |
+
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
47 |
+
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
|
48 |
+
bot, chatbot, chatbot
|
49 |
+
)
|
50 |
+
|
51 |
+
demo.queue()
|
52 |
+
if __name__ == "__main__":
|
53 |
+
demo.launch()
|
gradiodemo/Demo/ChatInterface/Simple.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
def echo(message, history):
|
4 |
+
return message
|
5 |
+
|
6 |
+
demo = gr.ChatInterface(fn=echo, examples=["hello", "hola", "merhaba"], title="Echo Bot")
|
7 |
+
demo.launch()
|
gradiodemo/Demo/ClickA.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
# 定义一个全局变量,用于存储按钮的点击状态
|
4 |
+
button_clicked = False
|
5 |
+
|
6 |
+
# 创建一个处理函数,该函数在按钮点击时被调用
|
7 |
+
def button_click():
|
8 |
+
global button_clicked
|
9 |
+
button_clicked = True
|
10 |
+
def dynamic_buttons(user_input):
|
11 |
+
if user_input == "A":
|
12 |
+
button_html = "<button onclick='button_click()'>点击我</button>"
|
13 |
+
elif user_input == "B":
|
14 |
+
button_html = "<button onclick='alert(\"按钮B被点击了!\")'>按钮B</button>"
|
15 |
+
else:
|
16 |
+
button_html = "<button onclick='alert(\"未知按钮被点击了!\")'>未知按钮</button>"
|
17 |
+
return button_html
|
18 |
+
# 创建一个Gradio界面
|
19 |
+
iface = gr.Interface(
|
20 |
+
fn=dynamic_buttons,
|
21 |
+
inputs="text",
|
22 |
+
outputs="html",
|
23 |
+
live=False # 禁用自动更新,因为我们将使用JavaScript来处理
|
24 |
+
)
|
25 |
+
|
26 |
+
# 设置HTML内容,包括一个按钮
|
27 |
+
# button_html = "<button onclick='button_click()'>点击我</button>"
|
28 |
+
# iface.update(button_html)
|
29 |
+
|
30 |
+
# 启动Gradio界面
|
31 |
+
iface.launch()
|
32 |
+
|
33 |
+
# 监听按钮的点击状态
|
34 |
+
while True:
|
35 |
+
if button_clicked:
|
36 |
+
# 按钮被点击,运行您的Python函数
|
37 |
+
# 这里可以添加您的代码来创建新的Gradio按钮或执行其他操作
|
38 |
+
print("按钮A被点击了!")
|
39 |
+
button_clicked = False # 重置按钮点击状态
|
gradiodemo/Demo/IMG/didi.jpeg
ADDED
gradiodemo/Demo/Login.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
def reverse(text):
|
3 |
+
# return text[::-1]
|
4 |
+
return True
|
5 |
+
demo = gr.Interface(reverse, "text", "text")
|
6 |
+
demo.launch(auth=("username", "password"))
|
gradiodemo/Demo/chatbot_multimodel.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
|
5 |
+
# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
def add_file(history, file):
|
12 |
+
history = history + [((file.name,), None)]
|
13 |
+
return history
|
14 |
+
|
15 |
+
|
16 |
+
def bot(history):
|
17 |
+
response = "**That's cool!**"
|
18 |
+
history[-1][1] = ""
|
19 |
+
for character in response:
|
20 |
+
history[-1][1] += character
|
21 |
+
time.sleep(0.05)
|
22 |
+
yield history
|
23 |
+
|
24 |
+
|
25 |
+
with gr.Blocks() as demo:
|
26 |
+
buttonAddText = gr.Button("添加文本框")
|
27 |
+
buttonAddText.visible = True;
|
28 |
+
chatbot = gr.Chatbot(
|
29 |
+
[],
|
30 |
+
elem_id="chatbot",
|
31 |
+
bubble_full_width=False,
|
32 |
+
avatar_images=(None, (os.path.join(os.path.dirname(__file__), "./IMG/didi.jpeg"))),
|
33 |
+
)
|
34 |
+
|
35 |
+
with gr.Row():
|
36 |
+
txt = gr.Textbox(
|
37 |
+
scale=4,
|
38 |
+
show_label=False,
|
39 |
+
placeholder="Enter text and press enter, or upload an image",
|
40 |
+
container=False,
|
41 |
+
)
|
42 |
+
btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
|
43 |
+
|
44 |
+
|
45 |
+
def add_text(history, text):
|
46 |
+
history = history + [(text, None)]
|
47 |
+
# 添加一个按钮组件
|
48 |
+
buttonAddText.visible = False;
|
49 |
+
gr.update(interactive=True)
|
50 |
+
print("add button")
|
51 |
+
return history, gr.update(value="", interactive=True)
|
52 |
+
|
53 |
+
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
54 |
+
bot, chatbot, chatbot
|
55 |
+
)
|
56 |
+
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
57 |
+
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
|
58 |
+
bot, chatbot, chatbot
|
59 |
+
)
|
60 |
+
|
61 |
+
demo.queue()
|
62 |
+
if __name__ == "__main__":
|
63 |
+
demo.launch()
|
gradiodemo/Demo/mHtl/D.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
# 定义处理函数,根据用户输入生成不同的按钮
|
4 |
+
def dynamic_buttons(user_input):
|
5 |
+
if user_input == "A":
|
6 |
+
button_html = "<button onclick='alert(\"按钮A被点击了!\")'>按钮A</button>"
|
7 |
+
elif user_input == "B":
|
8 |
+
button_html = "<button onclick='alert(\"按钮B被点击了!\")'>按钮B</button>"
|
9 |
+
else:
|
10 |
+
button_html = "<button onclick='alert(\"未知按钮被点击了!\")'>未知按钮</button>"
|
11 |
+
return button_html
|
12 |
+
|
13 |
+
# 创建 Gradio 接口,输入是文本,输出是HTML
|
14 |
+
iface = gr.Interface(fn=dynamic_buttons, inputs="text", outputs="html")
|
15 |
+
|
16 |
+
# 启动界面
|
17 |
+
iface.launch()
|
gradiodemo/__init__.py
ADDED
File without changes
|
gradiodemo/button.html
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html>
|
3 |
+
<head>
|
4 |
+
<title>按钮示例</title>
|
5 |
+
</head>
|
6 |
+
<body>
|
7 |
+
<button onclick="alert('按钮被点击了!')">点击我</button>
|
8 |
+
</body>
|
9 |
+
</html>
|
gradiodemo/test.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
def image_classifier(inp):
|
4 |
+
return {'cat': 0.3, 'dog': 0.7}
|
5 |
+
|
6 |
+
demo = gr.Interface(fn=image_classifier, inputs="image", outputs="label")
|
7 |
+
demo.launch()
|