marcmaxmeister commited on
Commit
5051fc4
·
verified ·
1 Parent(s): ec07fa1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -8
app.py CHANGED
@@ -1,3 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import requests
3
  import json
@@ -33,12 +78,4 @@ with gr.Blocks() as demo:
33
 
34
  demo.launch()
35
 
36
- """
37
- response = requests.post(api_url, headers=headers, json=payload, timeout=60)
38
-
39
- if response.status_code == 200:
40
- result = response.json()
41
- print(result)
42
- else:
43
- print(f"Error accessing private space: {response.status_code}, {response.text}")
44
  """
 
1
+ import os
2
+ from fastapi import FastAPI, Request, HTTPException
3
+ import httpx
4
+ from urllib.parse import urlencode
5
+
6
+ app = FastAPI()
7
+
8
+ # Access the secret environment variable
9
+ private_space_token = os.environ.get('api_key')
10
+ api_url = "https://givingtuesday-annotated-990-data.hf.space/"
11
+ headers = {
12
+ "Authorization": f"Bearer {private_space_token}",
13
+ "Content-Type": "application/json"
14
+ }
15
+
16
+ @app.get("/eins/")
17
+ async def proxy_request(request: Request):
18
+ # Capture all query parameters from the incoming request
19
+ query_params = request.query_params
20
+
21
+ # Encode the parameters for the new URL
22
+ encoded_params = urlencode(query_params)
23
+
24
+ # Construct the full URL for the private space endpoint
25
+ private_api_url = f"{api_url}/?{encoded_params}"
26
+
27
+ # Use httpx to make an asynchronous GET request to the private API
28
+ try:
29
+ async with httpx.AsyncClient(timeout=60.0) as client:
30
+ response = await client.get(private_api_url, headers=headers)
31
+ response.raise_for_status() # Raise an exception for 4xx/5xx responses
32
+ return response.json()
33
+
34
+ except httpx.HTTPStatusError:
35
+ raise HTTPException(status_code=response.status_code, detail="Error from private space API")
36
+ except httpx.RequestError as e:
37
+ raise HTTPException(status_code=500, detail=f"Network error: {str(e)}")
38
+
39
+
40
+
41
+
42
+
43
+ """
44
+
45
+
46
  import os
47
  import requests
48
  import json
 
78
 
79
  demo.launch()
80
 
 
 
 
 
 
 
 
 
81
  """