|
import gradio as gr |
|
import socket |
|
|
|
def predict(text, request: gr.Request): |
|
client_ip = request.client.host |
|
local_ip = socket.gethostbyname(socket.gethostbyname("")) |
|
x_forwarded_for = request.headers.get('x-forwarded-for') |
|
headers = request.headers |
|
if x_forwarded_for: |
|
client_ip = x_forwarded_for |
|
print("client_ip", client_ip, text) |
|
print("x_forwarded_for", x_forwarded_for) |
|
return text, {"client_ip": client_ip, |
|
"local_ip": local_ip, |
|
"headers": headers} |
|
|
|
|
|
with gr.Blocks() as block: |
|
gr.Markdown("## Gradio get client IP") |
|
text = gr.Textbox(label="dummy input") |
|
output = gr.JSON({}) |
|
btn = gr.Button("Test") |
|
|
|
btn.click(predict, inputs=[text], outputs=[text, output]) |
|
|
|
block.queue() |
|
block.launch(share=False, server_name='0.0.0.0', show_api=True) |
|
|