Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,24 +2,31 @@ import gradio as gr
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
import json
|
5 |
-
import time
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
def extract_structure(template, text):
|
16 |
-
|
|
|
|
|
|
|
17 |
Template: {template}
|
18 |
Text: {text}
|
19 |
-
|
20 |
|
21 |
try:
|
22 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(
|
23 |
outputs = model.generate(**inputs, max_new_tokens=512)
|
24 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
25 |
|
@@ -33,21 +40,10 @@ Extracted JSON:"""
|
|
33 |
except Exception as e:
|
34 |
return f"❌ Error: {str(e)}", {}, f"<p style='color:red'>{str(e)}</p>"
|
35 |
|
36 |
-
# Gradio
|
37 |
with gr.Blocks() as demo:
|
38 |
-
|
39 |
-
|
40 |
-
with gr.Row():
|
41 |
-
with gr.Column():
|
42 |
-
template = gr.Textbox(label="Template (JSON)", value='{"fields": ["name", "email"]}')
|
43 |
-
text = gr.TextArea(label="Input Text")
|
44 |
-
btn = gr.Button("Extract")
|
45 |
-
|
46 |
-
with gr.Column():
|
47 |
-
status = gr.Textbox(label="Status")
|
48 |
-
json_out = gr.JSON(label="Output")
|
49 |
-
html_out = gr.HTML()
|
50 |
-
|
51 |
-
btn.click(extract_structure, [template, text], [status, json_out, html_out])
|
52 |
|
53 |
-
|
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
import json
|
|
|
5 |
|
6 |
+
# Initialize with error handling
|
7 |
+
try:
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("numind/NuExtract-1.5")
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
"numind/NuExtract-1.5",
|
11 |
+
device_map="auto",
|
12 |
+
torch_dtype=torch.float16
|
13 |
+
)
|
14 |
+
MODEL_LOADED = True
|
15 |
+
except Exception as e:
|
16 |
+
MODEL_LOADED = False
|
17 |
+
print(f"Model loading failed: {e}")
|
18 |
|
19 |
def extract_structure(template, text):
|
20 |
+
if not MODEL_LOADED:
|
21 |
+
return "❌ Model not loaded", {}, "<p style='color:red'>Model failed to initialize</p>"
|
22 |
+
|
23 |
+
prompt = f"""Extract from text:
|
24 |
Template: {template}
|
25 |
Text: {text}
|
26 |
+
JSON Output:"""
|
27 |
|
28 |
try:
|
29 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
30 |
outputs = model.generate(**inputs, max_new_tokens=512)
|
31 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
32 |
|
|
|
40 |
except Exception as e:
|
41 |
return f"❌ Error: {str(e)}", {}, f"<p style='color:red'>{str(e)}</p>"
|
42 |
|
43 |
+
# Gradio interface
|
44 |
with gr.Blocks() as demo:
|
45 |
+
# [Keep your existing UI code here]
|
46 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
if __name__ == "__main__":
|
49 |
+
demo.launch()
|