Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,15 @@
|
|
1 |
-
import os
|
2 |
-
import json
|
3 |
-
import re
|
4 |
import gradio as gr
|
5 |
-
import requests
|
6 |
-
|
7 |
-
# Hugging Face API details
|
8 |
-
API_URL = "https://api-inference.huggingface.co/models/numind/NuExtract-1.5"
|
9 |
-
api_token = os.environ.get("HF_TOKEN", "") # Get token from environment variable
|
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)
|
16 |
-
print("API STATUS CODE:", response.status_code)
|
17 |
-
print("RAW RESPONSE:", response.text)
|
18 |
-
return response.json()
|
19 |
-
except Exception as e:
|
20 |
-
return {"error": f"Request failed: {str(e)}"}
|
21 |
-
|
22 |
|
23 |
def extract_structure(template, text):
|
24 |
-
|
25 |
-
prompt = f"<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>"
|
26 |
-
|
27 |
-
payload = {
|
28 |
-
"inputs": prompt,
|
29 |
-
"parameters": {
|
30 |
-
"max_new_tokens": 2000,
|
31 |
-
"temperature": 0.01,
|
32 |
-
"return_full_text": True
|
33 |
-
}
|
34 |
-
}
|
35 |
-
|
36 |
-
response = query_api(payload)
|
37 |
-
|
38 |
-
if isinstance(response, dict) and "error" in response:
|
39 |
-
return f"API Error: {response['error']}", "{}", f"<p>Error: {response['error']}</p>"
|
40 |
-
|
41 |
-
if isinstance(response, list) and len(response) > 0:
|
42 |
-
output = response[0].get("generated_text", "")
|
43 |
-
print("Generated Text:", output)
|
44 |
-
|
45 |
-
if "<|output|>" in output:
|
46 |
-
result = output.split("<|output|>")[-1].strip()
|
47 |
-
else:
|
48 |
-
match = re.search(r'({[\s\S]+})', output)
|
49 |
-
result = match.group(1) if match else output.strip()
|
50 |
-
|
51 |
-
try:
|
52 |
-
parsed = json.loads(result)
|
53 |
-
result = json.dumps(parsed, indent=2)
|
54 |
-
except Exception:
|
55 |
-
pass
|
56 |
|
57 |
-
highlighted = f"<p>β
Successfully processed input of length {len(text)} characters.</p>"
|
58 |
-
return "β
Extraction Complete", result, highlighted
|
59 |
-
|
60 |
-
return "β οΈ Unexpected API Response", json.dumps(response, indent=2), "<p>Unexpected format.</p>"
|
61 |
-
|
62 |
-
except Exception as e:
|
63 |
-
return f"β Error: {str(e)}", "{}", f"<p>Processing failed: {str(e)}</p>"
|
64 |
-
|
65 |
-
|
66 |
-
# Gradio App
|
67 |
with gr.Blocks() as demo:
|
68 |
gr.Markdown("# π§ NuExtract-1.5 Information Extractor")
|
69 |
|
70 |
-
if not api_token:
|
71 |
-
gr.Markdown("## β οΈ No API token found. Please set `HF_TOKEN` in environment variables.")
|
72 |
-
|
73 |
with gr.Row():
|
74 |
with gr.Column():
|
75 |
-
template_input = gr.Textbox(
|
76 |
-
|
77 |
-
value='{"name": "", "email": ""}',
|
78 |
-
lines=5
|
79 |
-
)
|
80 |
-
text_input = gr.Textbox(
|
81 |
-
label="Input Text",
|
82 |
-
value="Contact: John Smith ([email protected])",
|
83 |
-
lines=10
|
84 |
-
)
|
85 |
submit_btn = gr.Button("Extract Information")
|
86 |
|
87 |
with gr.Column():
|
@@ -94,51 +22,7 @@ with gr.Blocks() as demo:
|
|
94 |
inputs=[template_input, text_input],
|
95 |
outputs=[progress_output, result_output, html_output]
|
96 |
)
|
97 |
-
|
98 |
-
gr.Examples(
|
99 |
-
[
|
100 |
-
[
|
101 |
-
'{"name": "", "email": ""}',
|
102 |
-
'Contact: John Smith ([email protected])'
|
103 |
-
],
|
104 |
-
[
|
105 |
-
'''{
|
106 |
-
"Model": {
|
107 |
-
"Name": "",
|
108 |
-
"Number of parameters": "",
|
109 |
-
"Architecture": []
|
110 |
-
},
|
111 |
-
"Usage": {
|
112 |
-
"Use case": [],
|
113 |
-
"License": ""
|
114 |
-
}
|
115 |
-
}''',
|
116 |
-
'''We introduce Mistral 7B, a 7-billion-parameter language model engineered for superior performance and efficiency. Mistral 7B outperforms the best open 13B model (Llama 2) across all evaluated benchmarks, and the best released 34B model (Llama 1) in reasoning, mathematics, and code generation. Our model is released under the Apache 2.0 license.'''
|
117 |
-
]
|
118 |
-
],
|
119 |
-
[template_input, text_input]
|
120 |
-
)
|
121 |
-
|
122 |
-
|
123 |
-
def test_api_connection():
|
124 |
-
print("===== Application Startup =====")
|
125 |
-
if not api_token:
|
126 |
-
print("β HF_TOKEN not set. Please set your API token.")
|
127 |
-
else:
|
128 |
-
test_payload = {
|
129 |
-
"inputs": "<|input|>\n### Template:\n{\"test\": \"\"}\n### Text:\nHello world\n\n<|output|>",
|
130 |
-
"parameters": {
|
131 |
-
"max_new_tokens": 100,
|
132 |
-
"temperature": 0.01
|
133 |
-
}
|
134 |
-
}
|
135 |
-
response = query_api(test_payload)
|
136 |
-
if isinstance(response, list):
|
137 |
-
print("β
Connection to Hugging Face API successful!")
|
138 |
-
else:
|
139 |
-
print("β οΈ API may not be returning expected format:", response)
|
140 |
-
|
141 |
|
142 |
if __name__ == "__main__":
|
143 |
-
|
144 |
-
demo.launch(debug=True) # You can add share=True or server_name/port if needed
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
def extract_structure(template, text):
|
4 |
+
return "β
Test worked", '{"name": "John Smith", "email": "[email protected]"}', "<p>Success</p>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
with gr.Blocks() as demo:
|
7 |
gr.Markdown("# π§ NuExtract-1.5 Information Extractor")
|
8 |
|
|
|
|
|
|
|
9 |
with gr.Row():
|
10 |
with gr.Column():
|
11 |
+
template_input = gr.Textbox(label="Template (JSON)", lines=5)
|
12 |
+
text_input = gr.Textbox(label="Input Text", lines=10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
submit_btn = gr.Button("Extract Information")
|
14 |
|
15 |
with gr.Column():
|
|
|
22 |
inputs=[template_input, text_input],
|
23 |
outputs=[progress_output, result_output, html_output]
|
24 |
)
|
25 |
+
print("β
Button click event bound!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
if __name__ == "__main__":
|
28 |
+
demo.launch(ssr=False)
|
|