lightmate commited on
Commit
53c700b
·
verified ·
1 Parent(s): 031d3f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -16
app.py CHANGED
@@ -16,9 +16,14 @@ from starlette.config import Config
16
  from authlib.integrations.starlette_client import OAuth, OAuthError
17
 
18
  # Import our modular components
19
- from config.settings import settings
20
- from api.client import api_client
21
- from components.verification_result import format_verification_results
 
 
 
 
 
22
 
23
  # Create FastAPI app
24
  app = FastAPI(title="DGaze - News Verification")
@@ -143,8 +148,17 @@ def public(user: str = Depends(get_user), request: Request = None):
143
 
144
  @app.route('/login')
145
  async def login(request: Request):
146
- # Use root URL as redirect_uri to match React app pattern
147
- redirect_uri = f"http://localhost:{settings.GRADIO_SERVER_PORT}/"
 
 
 
 
 
 
 
 
 
148
  return await oauth.auth0.authorize_redirect(request, redirect_uri)
149
 
150
  @app.route('/logout')
@@ -158,13 +172,26 @@ async def logout(request: Request):
158
 
159
  request.session.pop('user', None)
160
  request.session.pop('session_id', None)
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  return RedirectResponse(url=
162
  "https://"
163
  + settings.AUTH0_DOMAIN
164
  + "/v2/logout?"
165
  + urlencode(
166
  {
167
- "returnTo": f"http://localhost:{settings.GRADIO_SERVER_PORT}/",
168
  "client_id": settings.AUTH0_CLIENT_ID,
169
  },
170
  quote_via=quote_plus,
@@ -893,17 +920,17 @@ def create_main_interface():
893
 
894
  return demo
895
 
 
 
 
 
 
 
 
 
 
 
896
  if __name__ == "__main__":
897
- # Create interfaces
898
- trial_demo = create_trial_interface()
899
- login_demo = create_login_interface()
900
- main_demo = create_main_interface()
901
-
902
- # Mount Gradio apps
903
- gr.mount_gradio_app(app, trial_demo, path="/trial")
904
- gr.mount_gradio_app(app, login_demo, path="/login-page")
905
- gr.mount_gradio_app(app, main_demo, path="/demo")
906
-
907
  # Run the application
908
  uvicorn.run(
909
  app,
 
16
  from authlib.integrations.starlette_client import OAuth, OAuthError
17
 
18
  # Import our modular components
19
+ try:
20
+ from config.settings import settings
21
+ from api.client import api_client
22
+ from components.verification_result import format_verification_results
23
+ except Exception as e:
24
+ print(f"ERROR: Failed to import required modules: {e}")
25
+ print("Please ensure all environment variables are set correctly.")
26
+ raise
27
 
28
  # Create FastAPI app
29
  app = FastAPI(title="DGaze - News Verification")
 
148
 
149
  @app.route('/login')
150
  async def login(request: Request):
151
+ # Use root URL as redirect_uri - adapt for Spaces or local
152
+ if settings.GRADIO_SERVER_NAME == "0.0.0.0" and settings.GRADIO_SERVER_PORT == 7860:
153
+ # Running on Hugging Face Spaces - use the current request's URL
154
+ redirect_uri = str(request.base_url).rstrip('/')
155
+ if not redirect_uri.startswith('https://'):
156
+ redirect_uri = redirect_uri.replace('http://', 'https://')
157
+ else:
158
+ # Running locally
159
+ redirect_uri = f"http://localhost:{settings.GRADIO_SERVER_PORT}/"
160
+
161
+ print(f"DEBUG: Login redirect_uri: {redirect_uri}")
162
  return await oauth.auth0.authorize_redirect(request, redirect_uri)
163
 
164
  @app.route('/logout')
 
172
 
173
  request.session.pop('user', None)
174
  request.session.pop('session_id', None)
175
+
176
+ # Determine return URL for Spaces or local
177
+ if settings.GRADIO_SERVER_NAME == "0.0.0.0" and settings.GRADIO_SERVER_PORT == 7860:
178
+ # Running on Hugging Face Spaces - use the current request's URL
179
+ return_url = str(request.base_url).rstrip('/')
180
+ if not return_url.startswith('https://'):
181
+ return_url = return_url.replace('http://', 'https://')
182
+ else:
183
+ # Running locally
184
+ return_url = f"http://localhost:{settings.GRADIO_SERVER_PORT}/"
185
+
186
+ print(f"DEBUG: Logout return_url: {return_url}")
187
+
188
  return RedirectResponse(url=
189
  "https://"
190
  + settings.AUTH0_DOMAIN
191
  + "/v2/logout?"
192
  + urlencode(
193
  {
194
+ "returnTo": return_url,
195
  "client_id": settings.AUTH0_CLIENT_ID,
196
  },
197
  quote_via=quote_plus,
 
920
 
921
  return demo
922
 
923
+ # Create interfaces
924
+ trial_demo = create_trial_interface()
925
+ login_demo = create_login_interface()
926
+ main_demo = create_main_interface()
927
+
928
+ # Mount Gradio apps
929
+ gr.mount_gradio_app(app, trial_demo, path="/trial")
930
+ gr.mount_gradio_app(app, login_demo, path="/login-page")
931
+ gr.mount_gradio_app(app, main_demo, path="/demo")
932
+
933
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
934
  # Run the application
935
  uvicorn.run(
936
  app,