Spaces:
Sleeping
Sleeping
File size: 12,980 Bytes
3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 35ae9b2 3b9a7b5 e3a267d 3b9a7b5 |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
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> | π <a href="https://numind.ai/blog">Blog</a> | π£οΈ <a href="https://discord.gg/3tsEtJNCDe">Discord</a> | π οΈ <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)
|