GuglielmoTor commited on
Commit
751e28f
·
verified ·
1 Parent(s): 85c8b0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -50
app.py CHANGED
@@ -1,62 +1,26 @@
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://app.ingaze.ai'], # 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='GuglielmoTor--LinkedinMonitor.hf.space', # 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)
 
1
  import gradio as gr
 
 
 
 
2
 
3
+ # shared state
 
 
 
 
 
 
 
 
 
 
 
 
4
  token_received = {"status": False}
5
 
6
+ # 1) this will be called by POST
7
+ def receive_token(accessToken: str):
 
 
 
 
8
  token_received["status"] = True
9
+ return {"status": "ok"}
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ # 2) this just drives your UI
12
  def check_status():
13
  return "✅ Code received" if token_received["status"] else "❌ Code not received"
14
 
15
  with gr.Blocks() as demo:
16
+ # we don’t actually show these widgets in the UI:
17
+ hidden_token = gr.Textbox(visible=False)
18
+ hidden_btn = gr.Button(visible=False)
19
+ # wire up the hidden POST→function
20
+ hidden_btn.click(fn=receive_token, inputs=hidden_token, outputs=[])
21
 
22
+ # your visible UI
23
+ status_box = gr.Textbox(value=check_status(), label="Token Status", interactive=False)
24
+ refresh = gr.Button("Refresh").click(fn=check_status, outputs=status_box)
 
 
 
 
 
25
 
26
+ demo.launch(server_name="0.0.0.0", server_port=7860)