Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import hashlib
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import random
|
5 |
+
import time
|
6 |
+
|
7 |
+
def generate_user_id(request_headers):
|
8 |
+
# 提取所需的信息
|
9 |
+
ip_address = request_headers['host']
|
10 |
+
user_agent = request_headers['user-agent']
|
11 |
+
accept_language = request_headers['accept-language']
|
12 |
+
if 'cookie' in request_headers:
|
13 |
+
cookie = request_headers['cookie']
|
14 |
+
else:
|
15 |
+
cookie = 'empty'
|
16 |
+
print(f'cookie not in request_headers: {request_headers}')
|
17 |
+
|
18 |
+
# 将信息组合成一个字符串
|
19 |
+
combined_info = f"{ip_address}-{user_agent}-{accept_language}-{cookie}"
|
20 |
+
print('combined_info:', combined_info)
|
21 |
+
|
22 |
+
# 使用哈希函数生成唯一标识符
|
23 |
+
user_id = hashlib.sha256(combined_info.encode()).hexdigest()
|
24 |
+
print('user_id:', user_id)
|
25 |
+
|
26 |
+
return f"{combined_info}\n user_id:{user_id}"
|
27 |
+
|
28 |
+
def page_load(request: gr.Request):
|
29 |
+
uuid = generate_user_id(request.headers)
|
30 |
+
print('[page_load] uuid:', uuid)
|
31 |
+
|
32 |
+
with gr.Blocks() as demo:
|
33 |
+
chatbot = gr.Chatbot()
|
34 |
+
msg = gr.Textbox()
|
35 |
+
clear = gr.ClearButton([msg, chatbot])
|
36 |
+
|
37 |
+
def respond(message, chat_history, request: gr.Request):
|
38 |
+
uuid = generate_user_id(request_headers=request.headers)
|
39 |
+
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
40 |
+
chat_history.append((message, bot_message))
|
41 |
+
time.sleep(2)
|
42 |
+
return "", chat_history
|
43 |
+
|
44 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
45 |
+
demo.load(page_load, None, None)
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
demo.launch()
|