Spaces:
Building
Building
import gradio as gr | |
from fastapi import FastAPI, Request, Response | |
from fastapi.middleware.cors import CORSMiddleware | |
from pydantic import BaseModel | |
import threading | |
# ---------- FastAPI Setup ---------- | |
app = FastAPI() | |
# Allow CORS for your Bubble frontend | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=['https://app.ingaze.ai'], # Replace with your actual Bubble app domain | |
allow_methods=['POST'], | |
allow_headers=['*'], | |
allow_credentials=True, | |
) | |
# State to track if token is received | |
token_received = {"status": False} | |
# Data model for incoming POST | |
class TokenIn(BaseModel): | |
accessToken: str | |
async def set_token(payload: TokenIn, response: Response): | |
token_received["status"] = True | |
session_token = payload.accessToken | |
response.set_cookie( | |
key='session_token', | |
value=session_token, | |
httponly=True, | |
secure=True, | |
samesite='none', | |
#domain='GuglielmoTor--LinkedinMonitor.hf.space', # Replace with your actual domain if needed | |
max_age=3600, | |
path='/' | |
) | |
return {'status': 'ok'} | |
# ---------- Gradio UI ---------- | |
def check_status(): | |
return "β Code received" if token_received["status"] else "β Code not received" | |
with gr.Blocks() as demo: | |
status_box = gr.Textbox(value=check_status(), label="Token Status", interactive=False) | |
refresh_btn = gr.Button("Refresh") | |
refresh_btn.click(fn=check_status, outputs=status_box) | |
# Mount Gradio inside FastAPI | |
import gradio as gr | |
from fastapi.middleware.wsgi import WSGIMiddleware | |
from gradio.routes import App as GradioApp | |
gradio_app = gr.mount_gradio_app(app, demo, path="/") | |
# This is what Hugging Face Spaces expects to run | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=7860) | |