Spaces:
Sleeping
Sleeping
File size: 987 Bytes
0a9fac2 f9084b5 ea7d224 0a9fac2 9201e28 ea7d224 31be1c9 0a9fac2 f9084b5 ea7d224 f9084b5 716d1a2 ea7d224 f9084b5 |
1 2 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 |
from fastapi import FastAPI, Request
import logging
import gradio as gr
from threading import Thread
from pydantic import BaseModel
app = FastAPI()
# Shared state to store the IP address
class SharedState(BaseModel):
client_ip: str = ""
shared_state = SharedState()
@app.get("/")
async def read_root(request: Request):
client_ip = request.client.host
logging.info(f'Client IP Address: {client_ip}')
shared_state.client_ip = client_ip
return {"Client IP Address": client_ip}
def gradio_interface():
def greet(name):
# Debug print
print(f"Shared state IP: {shared_state.client_ip}")
return f"Hello {name}! Last client IP: {shared_state.client_ip}"
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
iface.launch()
if __name__ == "__main__":
# Run FastAPI app
from uvicorn import run
Thread(target=lambda: run(app, host="0.0.0.0", port=8000)).start()
# Run Gradio app
gradio_interface()
|