Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,22 @@ api_token = os.environ.get("HF_TOKEN", "") # Get token from environment variabl
|
|
10 |
headers = {"Authorization": f"Bearer {api_token}"}
|
11 |
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def query_api(payload):
|
14 |
try:
|
15 |
response = requests.post(API_URL, headers=headers, json=payload)
|
@@ -23,6 +39,7 @@ def query_api(payload):
|
|
23 |
return {"error": f"Could not decode JSON: {str(e)}"}
|
24 |
|
25 |
|
|
|
26 |
def extract_structure(template, text):
|
27 |
try:
|
28 |
# Format the input for NuExtract
|
@@ -79,6 +96,9 @@ with gr.Blocks() as demo:
|
|
79 |
if not api_token:
|
80 |
gr.Markdown("## ⚠️ No API token found. Set `HF_TOKEN` in the Space secrets.")
|
81 |
|
|
|
|
|
|
|
82 |
with gr.Row():
|
83 |
with gr.Column():
|
84 |
template_input = gr.Textbox(
|
@@ -130,3 +150,4 @@ with gr.Blocks() as demo:
|
|
130 |
|
131 |
if __name__ == "__main__":
|
132 |
demo.launch()
|
|
|
|
10 |
headers = {"Authorization": f"Bearer {api_token}"}
|
11 |
|
12 |
|
13 |
+
# Test API Connection
|
14 |
+
def test_api_connection():
|
15 |
+
try:
|
16 |
+
# Test with a simple GET request
|
17 |
+
response = requests.get("https://api-inference.huggingface.co/models/numind/NuExtract-1.5")
|
18 |
+
|
19 |
+
# Check if the connection was successful
|
20 |
+
if response.status_code == 200:
|
21 |
+
print("✅ Connection to Hugging Face API successful!")
|
22 |
+
else:
|
23 |
+
print(f"⚠️ API returned status code {response.status_code}: {response.text}")
|
24 |
+
except requests.exceptions.RequestException as e:
|
25 |
+
print(f"❌ Connection failed: {str(e)}")
|
26 |
+
|
27 |
+
|
28 |
+
# Make the API request
|
29 |
def query_api(payload):
|
30 |
try:
|
31 |
response = requests.post(API_URL, headers=headers, json=payload)
|
|
|
39 |
return {"error": f"Could not decode JSON: {str(e)}"}
|
40 |
|
41 |
|
42 |
+
# Extract structure from the template and text
|
43 |
def extract_structure(template, text):
|
44 |
try:
|
45 |
# Format the input for NuExtract
|
|
|
96 |
if not api_token:
|
97 |
gr.Markdown("## ⚠️ No API token found. Set `HF_TOKEN` in the Space secrets.")
|
98 |
|
99 |
+
# Call test connection before launching the Gradio interface
|
100 |
+
test_api_connection()
|
101 |
+
|
102 |
with gr.Row():
|
103 |
with gr.Column():
|
104 |
template_input = gr.Textbox(
|
|
|
150 |
|
151 |
if __name__ == "__main__":
|
152 |
demo.launch()
|
153 |
+
|