Spaces:
Running
Running
File size: 1,823 Bytes
b560569 d070696 b560569 d070696 b560569 |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
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
@app.post('/api/set_token')
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)
|