Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,26 +3,44 @@ import torch
|
|
| 3 |
import json
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def test_function(template, text):
|
| 8 |
-
print(f"
|
| 9 |
return "Button clicked successfully", "Function was called"
|
| 10 |
|
| 11 |
-
# Real extraction function
|
| 12 |
def extract_info(template, text):
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
# Format prompt according to NuExtract-1.5 requirements
|
| 15 |
prompt = f"<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>"
|
|
|
|
| 16 |
|
| 17 |
# Tokenize
|
| 18 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 19 |
|
| 20 |
-
# Generate
|
| 21 |
print("Generating output...")
|
| 22 |
outputs = model.generate(
|
| 23 |
**inputs,
|
| 24 |
max_new_tokens=1000,
|
| 25 |
-
do_sample=False
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
# Decode and extract result
|
|
@@ -37,33 +55,19 @@ def extract_info(template, text):
|
|
| 37 |
|
| 38 |
# Try to parse as JSON
|
| 39 |
print("Parsing JSON...")
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return "✅ Success", formatted
|
| 44 |
except Exception as e:
|
| 45 |
-
print(f"Error: {str(e)}")
|
| 46 |
return f"❌ Error: {str(e)}", "{}"
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
try:
|
| 50 |
-
print("Loading model...")
|
| 51 |
-
model_name = "numind/NuExtract-1.5"
|
| 52 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 53 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 54 |
-
model_name,
|
| 55 |
-
torch_dtype=torch.float16,
|
| 56 |
-
device_map="auto",
|
| 57 |
-
trust_remote_code=True
|
| 58 |
-
)
|
| 59 |
-
print("Model loaded successfully")
|
| 60 |
-
except Exception as e:
|
| 61 |
-
print(f"Model loading error: {e}")
|
| 62 |
-
# Create dummy function for testing UI
|
| 63 |
-
def extract_info(template, text):
|
| 64 |
-
return "Model failed to load", "Cannot process request"
|
| 65 |
-
|
| 66 |
-
# Create a very simple interface
|
| 67 |
with gr.Blocks() as demo:
|
| 68 |
gr.Markdown("# NuExtract-1.5 Extraction Tool")
|
| 69 |
|
|
@@ -88,7 +92,7 @@ with gr.Blocks() as demo:
|
|
| 88 |
status = gr.Textbox(label="Status")
|
| 89 |
output = gr.Textbox(label="Output", lines=10)
|
| 90 |
|
| 91 |
-
# Connect both buttons
|
| 92 |
test_btn.click(
|
| 93 |
fn=test_function,
|
| 94 |
inputs=[template, text],
|
|
@@ -101,6 +105,5 @@ with gr.Blocks() as demo:
|
|
| 101 |
outputs=[status, output]
|
| 102 |
)
|
| 103 |
|
| 104 |
-
# Launch the app
|
| 105 |
if __name__ == "__main__":
|
| 106 |
demo.launch()
|
|
|
|
| 3 |
import json
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
| 6 |
+
# Initialize model 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 |
+
trust_remote_code=True
|
| 14 |
+
)
|
| 15 |
+
MODEL_LOADED = True
|
| 16 |
+
print("Model loaded successfully!")
|
| 17 |
+
except Exception as e:
|
| 18 |
+
MODEL_LOADED = False
|
| 19 |
+
print(f"Model loading failed: {e}")
|
| 20 |
+
|
| 21 |
def test_function(template, text):
|
| 22 |
+
print(f"Test function called with template: {template[:30]} and text: {text[:30]}")
|
| 23 |
return "Button clicked successfully", "Function was called"
|
| 24 |
|
|
|
|
| 25 |
def extract_info(template, text):
|
| 26 |
+
if not MODEL_LOADED:
|
| 27 |
+
return "❌ Model not loaded", "{}"
|
| 28 |
+
|
| 29 |
try:
|
| 30 |
# Format prompt according to NuExtract-1.5 requirements
|
| 31 |
prompt = f"<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>"
|
| 32 |
+
print(f"Processing with prompt: {prompt[:100]}...")
|
| 33 |
|
| 34 |
# Tokenize
|
| 35 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 36 |
|
| 37 |
+
# Generate with cache disabled
|
| 38 |
print("Generating output...")
|
| 39 |
outputs = model.generate(
|
| 40 |
**inputs,
|
| 41 |
max_new_tokens=1000,
|
| 42 |
+
do_sample=False,
|
| 43 |
+
use_cache=False # This disables the problematic cache
|
| 44 |
)
|
| 45 |
|
| 46 |
# Decode and extract result
|
|
|
|
| 55 |
|
| 56 |
# Try to parse as JSON
|
| 57 |
print("Parsing JSON...")
|
| 58 |
+
try:
|
| 59 |
+
extracted = json.loads(json_text)
|
| 60 |
+
formatted = json.dumps(extracted, indent=2)
|
| 61 |
+
except json.JSONDecodeError:
|
| 62 |
+
print(f"JSON parsing failed. Raw output: {json_text[:100]}...")
|
| 63 |
+
return "❌ JSON parsing error", json_text
|
| 64 |
+
|
| 65 |
return "✅ Success", formatted
|
| 66 |
except Exception as e:
|
| 67 |
+
print(f"Error in extraction: {str(e)}")
|
| 68 |
return f"❌ Error: {str(e)}", "{}"
|
| 69 |
|
| 70 |
+
# Create a simple interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
with gr.Blocks() as demo:
|
| 72 |
gr.Markdown("# NuExtract-1.5 Extraction Tool")
|
| 73 |
|
|
|
|
| 92 |
status = gr.Textbox(label="Status")
|
| 93 |
output = gr.Textbox(label="Output", lines=10)
|
| 94 |
|
| 95 |
+
# Connect both buttons
|
| 96 |
test_btn.click(
|
| 97 |
fn=test_function,
|
| 98 |
inputs=[template, text],
|
|
|
|
| 105 |
outputs=[status, output]
|
| 106 |
)
|
| 107 |
|
|
|
|
| 108 |
if __name__ == "__main__":
|
| 109 |
demo.launch()
|