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()