NuExtract-2.0 / app.py
Alexandre-Numind's picture
Update app.py
85ab008 verified
import json, torch, gradio as gr
from transformers import AutoProcessor, AutoModelForVision2Seq
from utils import process_all_vision_info
model_name = "numind/NuExtract-2.0-4B"
model = AutoModelForVision2Seq.from_pretrained(
model_name,
trust_remote_code=True,
torch_dtype=torch.bfloat16,
device_map="auto",
)
processor = AutoProcessor.from_pretrained(
model_name,
trust_remote_code=True,
padding_side="left",
use_fast=True,
)
def run_model(image, text, template):
"""Return (extracted_json, template_update, warning_update)."""
try:
json.loads(template)
template_valid = True
except Exception:
template_valid = False
messages = (
[{"role": "user", "content": text}]
if text
else [{"role": "user", "content": [{"type": "image", "image": image}]}]
)
template_arg = template if template_valid else None
if not template_valid:
messages = [{"role": "user", "content": template}]
chat_txt = processor.tokenizer.apply_chat_template(
messages, template=template_arg, tokenize=False, add_generation_prompt=True
)
img_inputs = process_all_vision_info(messages)
inputs = processor(
text=[chat_txt],
images=img_inputs,
padding=True,
return_tensors="pt"
).to("cuda")
seq_len = inputs.input_ids.shape[1]
if seq_len > 10_000:
return (
"",
gr.update(),
gr.update(
value=(
f"❌ **Input too long**: {seq_len} tokens "
f"(limit = 10 000). Please shorten the text or image context."
),
visible=True,
),
)
ids = model.generate(**inputs, do_sample=False, num_beams=1, max_new_tokens=4000)
ids2 = [o[len(i):] for i, o in zip(inputs.input_ids, ids)]
out = processor.batch_decode(
ids2, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
if template_valid:
extract_out = json.dumps(json.loads(out), indent=4,ensure_ascii = False)
templ_upd = gr.update()
warn_upd = gr.update(visible=False)
else:
extract_out = ""
templ_upd = gr.update(value=out)
warn_upd = gr.update(
value="⚠️ Template wasn’t valid JSON. "
"I generated one for you β€” check it, then press **Run** again.",
visible=True,
)
return extract_out, templ_upd, warn_upd
with gr.Blocks(title="NuExtract – zero-shot structured extraction") as demo:
# πŸš€ Banner
gr.HTML("""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NuExtract-2 Overview</title>
<style>
img { display:block; margin-bottom:1rem; }
ul { margin:1rem 0; padding-left:1.5rem; }
a { color:#4187f6; text-decoration:none; }
a:hover { text-decoration:underline; }
h1,h2 { margin:0 0 .5rem 0; font-weight:600; }
pre { overflow-x:auto; border-radius:6px; padding:1rem; }
code { border-radius:1px; padding:.1em .1em; font-family:monospace; }
/* β€”β€”β€” Dark / light themes β€”β€”β€” */
html[data-theme="dark"],
@media (prefers-color-scheme: dark) {
body { background-color:#1e1e1e; }
code { background-color:#2d2d2d; }
pre { background-color:#2a2a2a; }
}
html[data-theme="light"],
@media (prefers-color-scheme: light) {
body { background-color:#ffffff; }
code { background-color:#f5f5f5; }
pre { background-color:#f5f5f5; }
}
/* β€”β€”β€” NEW: put the two articles side-by-side β€”β€”β€” */
.template-container {
display: flex;
flex-wrap: wrap; /* stacks on small screens */
gap: 2rem;
margin-top: 1rem;
}
.template-container article {
flex: 1 1 320px; /* grow / shrink with a sensible min width */
min-width: 280px;
}
</style>
</head>
<body>
<p align="center">
<a href="https://nuextract.ai/">
<img src="https://cdn.prod.website-files.com/638364a4e52e440048a9529c/64188f405afcf42d0b85b926_logo_numind_final.png"
alt="NuMind Logo" style="width:200px;height:50px;" />
</a>
</p>
<p align="center">
πŸ–₯️ <a href="https://nuextract.ai/">API / Platform</a>&nbsp;|&nbsp;πŸ“‘ <a href="https://numind.ai/blog">Blog</a>&nbsp;|&nbsp;πŸ—£οΈ <a href="https://discord.gg/3tsEtJNCDe">Discord</a>&nbsp;|&nbsp;πŸ› οΈ <a href="https://github.com/numindai/nuextract">Github</a>
</p>
<section>
<h3>This space is a demo for <a href="https://huggingface.co/numind/NuExtract-2.0-4B" target="_blank">NuExtract-2.0-4B</a></h3>
<h3>You can also check: <a href="https://huggingface.co/numind/NuExtract-2.0-2B" target="_blank">NuExtract-2.0-2B</a> and <a href="https://huggingface.co/numind/NuExtract-2.0-8B" target="_blank">NuExtract-2.0-8B</a> and our top-performing model via the <a href="https://nuextract.ai/">API / Platform</a></h3>
<h1>NuExtract-2.0</h1>
<p>NuExtract 2.0 is a family of models trained specifically for structured information extraction tasks. It supports both multimodal inputs and is multilingual.</p>
<p>To use the model, provide an input text/image and a JSON template describing the information you need to extract. The template should be a JSON object, specifying field names and their expected type.</p>
<!-- ------------- SIDE-BY-SIDE CONTAINER ------------- -->
<div class="template-container">
<!-- Supported Template Types -->
<article>
<h3>Supported Template Types</h3>
<ul>
<li><code>verbatim-string</code> β€” extract text exactly as it appears.</li>
<li><code>string</code> β€” generic text, with possible paraphrasing.</li>
<li><code>integer</code> β€” whole number.</li>
<li><code>number</code> β€” decimal or whole number.</li>
<li><code>date-time</code> β€” ISO 8601 date format.</li>
<li><code>boolean</code> β€” True or False.</li>
<li>Array of any type above (e.g. <code>["string"]</code>).</li>
<li><code>enum</code> β€” one value from a predefined list (e.g. <code>["yes", "no", "maybe"]</code>).</li>
<li><code>multi-label</code> β€” multiple values from a list (e.g. <code>[["A", "B", "C"]]</code>).</li>
</ul>
<p>You can specify any nested structure, such as an object inside an object or a list of objects. If no relevant information is found, the model returns <code>null</code> or <code>[]</code>.</p>
</article>
<!-- Example Template -->
<article>
<h3>Example Template</h3>
<pre><code>{
"first_name": "verbatim-string",
"last_name": "verbatim-string",
"description": "string",
"age": "integer",
"classes": [
{
"name": "verbatim-string",
"professors": ["verbatim-string"],
"gpa": "number"
}
],
"average_gpa": "number",
"birth_date": "date-time",
"nationality": ["France", "England", "Japan", "USA", "China"],
"languages_spoken": [["English", "French", "Japanese", "Mandarin", "Spanish"]]
}</code></pre>
</article>
</div><!-- /.template-container -->
<br>
<strong>You can also provide a description of what you want to extract, use a non-JSON format (e.g. YAML, Pydantic) or even an example of input text. The model will automatically update the template field and generate a compatible JSON template based on our typing system.</strong>
</section>
<br>
<section>
<i>⚠️ This demo restricts inputs to 10,000 tokens</i>
</section>
</body>
</html>
""")
templ = gr.Textbox(
lines=10,
label="Template (JSON or prompt)",
placeholder="JSON template or instruction/other format to generate a template",
)
with gr.Row(equal_height=False):
img = gr.Image(type="filepath", label="Input image (PNG)", scale=1)
txt = gr.Textbox(lines=10, label="Text", scale=1)
example_data = [
[
"data/affiche.jpg", # image file
"", # no text
"""{
"movie_name": "verbatim-string",
"tagline": "verbatim-string",
"language": "string",
"motion_picture_association_rating": [
"G - General Audiences",
"PG - Parental Guidance Suggested",
"PG-13 – Parents Strongly Cautioned",
"R – Restricted",
"NC-17 – Adults Only",
"not provided"
],
"movie_distribution_company": "verbatim-string",
"movie_production_company": ["verbatim-string"],
"theatre_release_date": "date-time",
"movie_director_name": "verbatim-string",
"actors_names": [
"verbatim-string"
],
"award" : "verbatim-string",
"reviews": [
{
"critic_name": "verbatim-string",
"review_comment": "verbatim-string"
}
],
"technologies": [
[
"Dolby Stereo",
"Dolby Digital",
"Dolby Stereo Digital",
"Dolby Atmos",
"Dolby Vision",
"Dolby Cinema",
"DTS",
"SDDS",
"IMAX",
"4DX"
]
]
}"""
],
[
None, # no image
"""Provectus is a Silicon Valley-based Artificial Intelligence consultancy and solutions provider.
At Provectus, we are obsessed with leveraging cloud, data, and AI to reimagine the way businesses operate, compete, and deliver customer value. With the wide range of AI solutions for various use cases and industry verticals, Provectus is recognized by technology analysts and top cloud vendors as a leading AI consultancy and solutions provider. We are transformational leaders for our clients and employees.
Currently, we are looking for a highly motivated and self-driven Front End Developer.
Join us!
Briefly about your first project in our company:
The customer is an online distributor of menswear and a leader in the US market that works with Dolce & Gabbana, Calvin Klein, Ralph Lauren. We are developing a system for automating the processes of renting, buying, selling, and ordering suits online.
Team:
Frontend, Backend, and Mobile Engineers.
Team Lead and PM are on the customer side in the United States.
Requirements
Software Engineering or related field with 3+ years of professional software development experience in Frontend technology such as ReactJs, AngularJs
Deep understanding of front end development fundamentals including JavaScript, CSS, HTML and strong skills in React.js and its core principles, React.js workflows (such as Flux or Redux), Typescript, webpack, Babel, npm.
Strong skills working with REST-based APIs and JSON data structures
Hands-on experience working experience with source control system Git
Experience working with micro-frontend using web components
Strong analytical and debugging skills
At least an Intermediate level of English.
Responsibilities
Implementing best practices and technical solutions in process of migration from Angular to React
Working on new functionality
Taking ownership of business requirements and design, implement, test solutions
Write a professional, performant, high-quality code that will support a scaling business
Ask the right questions and think deeply about building solutions that support both - short-term and long-term goals
Proactively participate in the agile development process sprints, providing effort estimates, commitments, and feedback to tasks.""", # text
"""{
"company": "verbatim-string",
"industry": "string",
"position": "string",
"contract_type": "string",
"location": "string",
"remote": [
"yes",
"no",
"hybrid"
],
"education": "string",
"years_of_experience": "string",
"required_skills": [
"string"
],
"responsibilities": [
"string"
],
"salary": "string",
"benefits": [
"string"
],
"language_skills": [
{
"language": "verbatim-string",
"level": "string"
}
]
}"""
],
]
warn = gr.Markdown(visible=False)
run_btn = gr.Button("Run", variant="primary")
out_json = gr.Textbox(
lines=14,
label="Extraction Output (JSON)",
show_copy_button=True,
)
run_btn.click(
fn=run_model,
inputs=[img, txt, templ],
outputs=[out_json, templ, warn],
)
gr.Examples(
examples=example_data,
inputs=[img, txt, templ],
label="πŸ” Click an example to pre-fill the inputs",
cache_examples=False,
)
demo.launch(debug=True, share=True)