Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Access the secret environment variable
|
| 5 |
+
space_token = os.environ.get('api_key')
|
| 6 |
+
api_url = "https://givingtuesday-annotated-990-data.hf.space"
|
| 7 |
+
headers = {
|
| 8 |
+
"Authorization": f"Bearer {space_token}",
|
| 9 |
+
"Content-Type": "application/json"
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def call_private_api(input_data):
|
| 15 |
+
# The payload structure depends on the private API's expected input
|
| 16 |
+
# 'data' should be a list matching the input components of the target space
|
| 17 |
+
payload = {
|
| 18 |
+
"data": [input_data],
|
| 19 |
+
# You can add other kwargs to the payload here if needed by the specific API
|
| 20 |
+
"kwargs": {"some_param": "some_value"}
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
response = requests.post(API_URL, headers=headers, data=json.dumps(payload))
|
| 24 |
+
return response.json()
|
| 25 |
+
|
| 26 |
+
import gradio as gr
|
| 27 |
+
# Example usage within a Gradio interface in the public space
|
| 28 |
+
with gr.Blocks() as demo:
|
| 29 |
+
text_input = gr.Textbox(label="Enter input for private API")
|
| 30 |
+
text_output = gr.Textbox(label="Response from private API")
|
| 31 |
+
submit_btn = gr.Button("Call API")
|
| 32 |
+
submit_btn.click(fn=call_private_api, inputs=text_input, outputs=text_output)
|
| 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 |
+
"""
|