root
commited on
Commit
•
92e38f5
1
Parent(s):
178c449
Update space
Browse files
app.py
CHANGED
@@ -1,190 +1,97 @@
|
|
1 |
import gradio as gr
|
2 |
-
#from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
class Conversation:
|
61 |
-
def __init__(self, system_prompt='iii', num_of_round = 5):
|
62 |
-
self.num_of_round = num_of_round
|
63 |
-
self.history = []
|
64 |
-
self.initialized = False
|
65 |
-
self.history.append({"role": "system", "content": system_prompt})
|
66 |
-
|
67 |
-
if len(system_prompt) > 0:
|
68 |
-
#logger.info(f'Conversation initialized with system prompt: {system_prompt}')
|
69 |
-
self.initialized = True
|
70 |
-
|
71 |
-
def is_initialized(self):
|
72 |
-
return self.initialized
|
73 |
-
|
74 |
-
def append_question(self, question):
|
75 |
-
self.history.append({"role": "user", "content": question})
|
76 |
-
|
77 |
-
def append_answer(self, answer):
|
78 |
-
self.history.append({"role": "assistant", "content": answer})
|
79 |
-
|
80 |
-
if len(self.history) > self.num_of_round * 2:
|
81 |
-
del self.history[1:3]
|
82 |
-
|
83 |
-
def clear(self):
|
84 |
-
self.history.clear()
|
85 |
-
self.initialized = False
|
86 |
-
|
87 |
-
def get_prompts(self):
|
88 |
-
return self.history
|
89 |
-
|
90 |
-
def round_size(self):
|
91 |
-
return 0 if len(self.history) < 2 else len(self.hitory) - 1
|
92 |
-
|
93 |
-
def get_history_messages(self):
|
94 |
-
return [(u['content'], b['content']) for u,b in zip(self.history[1::2], self.history[2::2])]
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
chat_api = ChatgptAPI()
|
99 |
-
|
100 |
-
def predict(system_input, password_input, user_in_file, user_input, conversation):
|
101 |
-
if password_input != os.environ.get("USER_KEY"):
|
102 |
-
return [(None, "Wrong password!")], conversation, user_input
|
103 |
-
|
104 |
-
if conversation.is_initialized() == False:
|
105 |
-
conversation = Conversation(system_input, 5)
|
106 |
-
_, conversation = chat_api.get_single_round_completion(user_in_file, user_input, conversation)
|
107 |
-
return conversation.get_history_messages(), conversation, None
|
108 |
-
#_, conversation = chat_api.get_multi_round_completion(user_input, conversation)
|
109 |
-
#return conversation.get_history_messages(), conversation, None
|
110 |
-
|
111 |
-
|
112 |
-
def clear_history(conversation):
|
113 |
-
conversation.clear()
|
114 |
-
return None, conversation
|
115 |
-
|
116 |
-
|
117 |
-
with gr.Blocks(css="#chatbot{height:350px} .overflow-y-auto{height:600px}") as demo:
|
118 |
-
chatbot = gr.Chatbot(elem_id="chatbot")
|
119 |
-
conversation = gr.State(value=Conversation())
|
120 |
-
|
121 |
-
with gr.Row():
|
122 |
-
system_in_txt = gr.Textbox(lines=1, label="User Name:", placeholder="Enter user name")
|
123 |
-
password_in_txt = gr.Textbox(lines=1, label="Password:", placeholder="Enter password")
|
124 |
-
|
125 |
-
with gr.Row():
|
126 |
-
user_in_file = gr.File(label="Upload File")
|
127 |
-
user_in_txt = gr.Textbox(lines=3, label="User role content:", placeholder="Enter text...", container=False)
|
128 |
-
|
129 |
-
with gr.Row():
|
130 |
-
reset_button = gr.Button("Reset")
|
131 |
-
submit_button = gr.Button("Submit")
|
132 |
-
|
133 |
-
submit_button.click(predict, [system_in_txt, password_in_txt, user_in_file, user_in_txt, conversation], [chatbot, conversation, user_in_txt])
|
134 |
-
reset_button.click(clear_history, [conversation], [chatbot, conversation], queue=False)
|
135 |
-
|
136 |
-
'''
|
137 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
138 |
-
|
139 |
-
|
140 |
-
def respond(
|
141 |
-
message,
|
142 |
-
history: list[tuple[str, str]],
|
143 |
-
system_message,
|
144 |
-
max_tokens,
|
145 |
-
temperature,
|
146 |
-
top_p,
|
147 |
-
):
|
148 |
-
messages = [{"role": "system", "content": system_message}]
|
149 |
-
|
150 |
-
for val in history:
|
151 |
-
if val[0]:
|
152 |
-
messages.append({"role": "user", "content": val[0]})
|
153 |
-
if val[1]:
|
154 |
-
messages.append({"role": "assistant", "content": val[1]})
|
155 |
-
|
156 |
-
messages.append({"role": "user", "content": message})
|
157 |
-
|
158 |
-
response = ""
|
159 |
-
|
160 |
-
for message in client.chat_completion(
|
161 |
-
messages,
|
162 |
-
max_tokens=max_tokens,
|
163 |
-
stream=True,
|
164 |
-
temperature=temperature,
|
165 |
-
top_p=top_p,
|
166 |
-
):
|
167 |
-
token = message.choices[0].delta.content
|
168 |
-
|
169 |
-
response += token
|
170 |
-
yield response
|
171 |
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
186 |
)
|
187 |
-
'''
|
188 |
|
189 |
-
|
190 |
-
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
+
# 自定义HTML/CSS/JavaScript
|
4 |
+
custom_html = """
|
5 |
+
<style>
|
6 |
+
body {
|
7 |
+
font-family: Arial, sans-serif;
|
8 |
+
background-color: #f0f0f0;
|
9 |
+
}
|
10 |
+
.container {
|
11 |
+
max-width: 800px;
|
12 |
+
margin: 0 auto;
|
13 |
+
padding: 20px;
|
14 |
+
background-color: white;
|
15 |
+
border-radius: 10px;
|
16 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
17 |
+
}
|
18 |
+
h1 {
|
19 |
+
color: #333;
|
20 |
+
}
|
21 |
+
#custom-button {
|
22 |
+
background-color: #4CAF50;
|
23 |
+
border: none;
|
24 |
+
color: white;
|
25 |
+
padding: 15px 32px;
|
26 |
+
text-align: center;
|
27 |
+
text-decoration: none;
|
28 |
+
display: inline-block;
|
29 |
+
font-size: 16px;
|
30 |
+
margin: 4px 2px;
|
31 |
+
cursor: pointer;
|
32 |
+
}
|
33 |
+
#update-area {
|
34 |
+
margin-top: 20px;
|
35 |
+
padding: 10px;
|
36 |
+
border: 1px solid #ddd;
|
37 |
+
}
|
38 |
+
</style>
|
39 |
+
|
40 |
+
<div class="container">
|
41 |
+
<h1>动态更新界面示例</h1>
|
42 |
+
<button id="custom-button" onclick="updateInterface()">更新界面</button>
|
43 |
+
<div id="update-area">
|
44 |
+
点击按钮来更新这里的内容
|
45 |
+
</div>
|
46 |
+
</div>
|
47 |
+
|
48 |
+
<script>
|
49 |
+
function updateInterface() {
|
50 |
+
// 调用Gradio函数
|
51 |
+
var gradioApp = document.querySelector("gradio-app");
|
52 |
+
if (gradioApp == null) gradioApp = document.querySelector("body > gradio-app");
|
53 |
+
gradioApp.querySelector("#component-0").querySelector("button").click();
|
54 |
+
}
|
55 |
+
|
56 |
+
// 监听Gradio输出的变化
|
57 |
+
document.addEventListener('DOMContentLoaded', (event) => {
|
58 |
+
var gradioApp = document.querySelector("gradio-app");
|
59 |
+
if (gradioApp == null) gradioApp = document.querySelector("body > gradio-app");
|
60 |
+
var outputElement = gradioApp.querySelector("#component-2");
|
61 |
|
62 |
+
var observer = new MutationObserver(function(mutations) {
|
63 |
+
mutations.forEach(function(mutation) {
|
64 |
+
if (mutation.type === "childList") {
|
65 |
+
document.getElementById("update-area").innerHTML = outputElement.textContent;
|
66 |
+
}
|
67 |
+
});
|
68 |
+
});
|
69 |
+
|
70 |
+
var config = { childList: true, subtree: true };
|
71 |
+
observer.observe(outputElement, config);
|
72 |
+
});
|
73 |
+
</script>
|
74 |
+
|
75 |
+
<!-- Gradio组件将被插入到这里 -->
|
76 |
+
<div id="gradio-app"></div>
|
77 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
+
# Gradio函数
|
80 |
+
def update_content():
|
81 |
+
import datetime
|
82 |
+
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
83 |
+
return f"界面已更新,当前时间: {current_time}"
|
84 |
+
|
85 |
+
# 创建Gradio界面
|
86 |
+
iface = gr.Interface(
|
87 |
+
fn=update_content,
|
88 |
+
inputs=[],
|
89 |
+
outputs="text",
|
90 |
+
title=None,
|
91 |
+
description=None,
|
92 |
+
article=None,
|
93 |
+
layout="vertical"
|
94 |
)
|
|
|
95 |
|
96 |
+
|
97 |
+
iface.launch(inline=False, share=True, html=custom_html)
|