File size: 2,006 Bytes
be196c4
3953725
cede142
 
095dbb9
7041fb3
bd336a0
 
 
 
 
 
 
 
 
 
 
26a1605
cede142
bd336a0
 
 
 
3953725
 
bd336a0
cede142
 
bd336a0
3953725
 
cede142
3953725
 
 
 
cede142
3953725
cede142
3953725
26a1605
7041fb3
3953725
7041fb3
 
 
 
 
 
 
 
 
 
 
 
 
 
23c0ee1
bd336a0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import json

# Initialize model with error handling
try:
    tokenizer = AutoTokenizer.from_pretrained("numind/NuExtract-1.5")
    model = AutoModelForCausalLM.from_pretrained(
        "numind/NuExtract-1.5",
        device_map="auto",
        torch_dtype=torch.float16
    )
    MODEL_LOADED = True
except Exception as e:
    MODEL_LOADED = False
    print(f"Model loading failed: {e}")

def extract_structure(template, text):
    if not MODEL_LOADED:
        return "❌ Model not loaded", {}, "<p style='color:red'>Model failed to initialize</p>"
    
    prompt = f"""Extract from text:
Template: {template}
Text: {text}
JSON Output:"""
    
    try:
        inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
        outputs = model.generate(**inputs, max_new_tokens=512)
        result = tokenizer.decode(outputs[0], skip_special_tokens=True)
        
        # Extract JSON portion
        json_start = result.find("{")
        json_end = result.rfind("}") + 1
        extracted = json.loads(result[json_start:json_end])
        
        return "βœ… Success", extracted, f"<pre>{json.dumps(extracted, indent=2)}</pre>"
    except Exception as e:
        return f"❌ Error: {str(e)}", {}, f"<p style='color:red'>{str(e)}</p>"

# Gradio interface (properly indented block)
with gr.Blocks() as demo:
    gr.Markdown("# NuExtract-1.5 Structured Data Extractor")
    
    with gr.Row():
        with gr.Column():
            template = gr.Textbox(label="Template (JSON)", value='{"fields": ["name", "email"]}')
            text = gr.TextArea(label="Input Text")
            btn = gr.Button("Extract")
        
        with gr.Column():
            status = gr.Textbox(label="Status")
            json_out = gr.JSON(label="Output")
            html_out = gr.HTML()
    
    btn.click(extract_structure, [template, text], [status, json_out, html_out])

if __name__ == "__main__":
    demo.launch()