GuglielmoTor commited on
Commit
b560569
·
verified ·
1 Parent(s): 82f6fc7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from fastapi import FastAPI, Request, Response
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from pydantic import BaseModel
5
+ import threading
6
+
7
+ # ---------- FastAPI Setup ----------
8
+ app = FastAPI()
9
+
10
+ # Allow CORS for your Bubble frontend
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=['https://your-bubble-app.com'], # Replace with your actual Bubble app domain
14
+ allow_methods=['POST'],
15
+ allow_headers=['*'],
16
+ allow_credentials=True,
17
+ )
18
+
19
+ # State to track if token is received
20
+ token_received = {"status": False}
21
+
22
+ # Data model for incoming POST
23
+ class TokenIn(BaseModel):
24
+ accessToken: str
25
+
26
+ @app.post('/api/set_token')
27
+ async def set_token(payload: TokenIn, response: Response):
28
+ token_received["status"] = True
29
+ session_token = payload.accessToken
30
+ response.set_cookie(
31
+ key='session_token',
32
+ value=session_token,
33
+ httponly=True,
34
+ secure=True,
35
+ samesite='none',
36
+ domain='your-gradio-app.com', # Replace with your actual domain if needed
37
+ max_age=3600,
38
+ path='/'
39
+ )
40
+ return {'status': 'ok'}
41
+
42
+ # ---------- Gradio UI ----------
43
+ def check_status():
44
+ return "✅ Code received" if token_received["status"] else "❌ Code not received"
45
+
46
+ with gr.Blocks() as demo:
47
+ status_box = gr.Textbox(value=check_status(), label="Token Status", interactive=False)
48
+ refresh_btn = gr.Button("Refresh")
49
+
50
+ refresh_btn.click(fn=check_status, outputs=status_box)
51
+
52
+ # Mount Gradio inside FastAPI
53
+ import gradio as gr
54
+ from fastapi.middleware.wsgi import WSGIMiddleware
55
+ from gradio.routes import App as GradioApp
56
+
57
+ gradio_app = gr.mount_gradio_app(app, demo, path="/")
58
+
59
+ # This is what Hugging Face Spaces expects to run
60
+ if __name__ == "__main__":
61
+ import uvicorn
62
+ uvicorn.run(app, host="0.0.0.0", port=7860)