test_user_login / app.py
sudowind's picture
Create app.py
26869e7 verified
import hashlib
import gradio as gr
import random
import time
def generate_user_id(request_headers):
# 提取所需的信息
ip_address = request_headers['host']
user_agent = request_headers['user-agent']
accept_language = request_headers['accept-language']
if 'cookie' in request_headers:
cookie = request_headers['cookie']
else:
cookie = 'empty'
print(f'cookie not in request_headers: {request_headers}')
# 将信息组合成一个字符串
combined_info = f"{ip_address}-{user_agent}-{accept_language}-{cookie}"
print('combined_info:', combined_info)
# 使用哈希函数生成唯一标识符
user_id = hashlib.sha256(combined_info.encode()).hexdigest()
print('user_id:', user_id)
return f"{combined_info}\n user_id:{user_id}"
def page_load(request: gr.Request):
uuid = generate_user_id(request.headers)
print('[page_load] uuid:', uuid)
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.ClearButton([msg, chatbot])
def respond(message, chat_history, request: gr.Request):
uuid = generate_user_id(request_headers=request.headers)
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
chat_history.append((message, bot_message))
time.sleep(2)
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
demo.load(page_load, None, None)
if __name__ == "__main__":
demo.launch()