Spaces:
Runtime error
Runtime error
nyAru
commited on
Commit
·
2a8f7ef
1
Parent(s):
f32b48a
Add application file
Browse files- app.py +109 -3
- requirements.txt +5 -0
app.py
CHANGED
|
@@ -1,7 +1,113 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 4 |
+
from transformers import pipeline
|
| 5 |
|
| 6 |
+
# Model names (keeping it programmatic)
|
| 7 |
+
model_names = [
|
| 8 |
+
"hadiaskari98/Software_NER_prod",
|
| 9 |
+
"hadiaskari98/Software_UniNER",
|
| 10 |
+
"hadiaskari98/Hardware_UniNER",
|
| 11 |
+
"hadiaskari98/Vulnerability_UniNER",
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
example_sent = (
|
| 15 |
+
"The Ingress concept lets you map traffic to different backends based on rules you define via the Kubernetes API."
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Programmatically build the model info dict
|
| 19 |
+
model_info = {
|
| 20 |
+
model_name: {
|
| 21 |
+
"link": f"https://huggingface.co/{model_name}",
|
| 22 |
+
"usage": f"""from transformers import pipeline
|
| 23 |
+
ner = pipeline("ner", model=AutoModelForTokenClassification.from_pretrained("{model_name}"), tokenizer=AutoTokenizer.from_pretrained("{model_name}"))
|
| 24 |
+
result = ner("{example_sent}")
|
| 25 |
+
print(result)""",
|
| 26 |
+
}
|
| 27 |
+
for model_name in model_names
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
# Load models into a dictionary programmatically for the analyze function
|
| 31 |
+
models = {
|
| 32 |
+
model_names[0]: pipeline("ner", model=AutoModelForTokenClassification.from_pretrained(model_names[0]),
|
| 33 |
+
tokenizer=AutoTokenizer.from_pretrained(model_names[0]), device=0)
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# Function to display model info (link and usage code)
|
| 38 |
+
def display_model_info(model_name):
|
| 39 |
+
info = model_info[model_name]
|
| 40 |
+
usage_code = info["usage"]
|
| 41 |
+
link_button = f'[Open model page for {model_name} ]({info["link"]})'
|
| 42 |
+
return usage_code, link_button
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# Function to run NER on input text
|
| 46 |
+
@spaces.GPU
|
| 47 |
+
def analyze_text(text, model_name):
|
| 48 |
+
if model_name not in models:
|
| 49 |
+
models.clear()
|
| 50 |
+
models[model_name] = pipeline("ner", model=AutoModelForTokenClassification.from_pretrained(model_name),
|
| 51 |
+
tokenizer=AutoTokenizer.from_pretrained(model_name), device=0)
|
| 52 |
+
ner = models[model_name]
|
| 53 |
+
ner_results = ner(text)
|
| 54 |
+
highlighted_text = []
|
| 55 |
+
last_idx = 0
|
| 56 |
+
for entity in ner_results:
|
| 57 |
+
start = entity["start"]
|
| 58 |
+
end = entity["end"]
|
| 59 |
+
label = entity["entity"]
|
| 60 |
+
# Add non-entity text
|
| 61 |
+
if start > last_idx:
|
| 62 |
+
highlighted_text.append((text[last_idx:start], None))
|
| 63 |
+
# Add entity text
|
| 64 |
+
highlighted_text.append((text[start:end], label))
|
| 65 |
+
last_idx = end
|
| 66 |
+
# Add any remaining text after the last entity
|
| 67 |
+
if last_idx < len(text):
|
| 68 |
+
highlighted_text.append((text[last_idx:], None))
|
| 69 |
+
return highlighted_text
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
with gr.Blocks() as demo:
|
| 73 |
+
gr.Markdown("# Secure Chain Named Entity Recognition (NER)")
|
| 74 |
+
|
| 75 |
+
# Dropdown for model selection
|
| 76 |
+
model_selector = gr.Dropdown(
|
| 77 |
+
choices=list(model_info.keys()),
|
| 78 |
+
value=list(model_info.keys())[0],
|
| 79 |
+
label="Select Model",
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Textbox for input text
|
| 83 |
+
text_input = gr.Textbox(
|
| 84 |
+
label="Enter Text",
|
| 85 |
+
lines=5,
|
| 86 |
+
value=example_sent,
|
| 87 |
+
)
|
| 88 |
+
analyze_button = gr.Button("Run NER Model")
|
| 89 |
+
output = gr.HighlightedText(label="NER Result", combine_adjacent=True)
|
| 90 |
+
|
| 91 |
+
# Outputs: usage code, model page link, and analyze button
|
| 92 |
+
code_output = gr.Code(label="Use this model", visible=True)
|
| 93 |
+
link_output = gr.Markdown(
|
| 94 |
+
f"[Open model page for {model_selector} ]({model_selector})"
|
| 95 |
+
)
|
| 96 |
+
# Button for analyzing the input text
|
| 97 |
+
analyze_button.click(
|
| 98 |
+
analyze_text, inputs=[text_input, model_selector], outputs=output
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# Trigger the code output and model link when model is changed
|
| 102 |
+
model_selector.change(
|
| 103 |
+
display_model_info, inputs=[model_selector], outputs=[code_output, link_output]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Call the display_model_info function on load to set initial values
|
| 107 |
+
demo.load(
|
| 108 |
+
fn=display_model_info,
|
| 109 |
+
inputs=[model_selector],
|
| 110 |
+
outputs=[code_output, link_output],
|
| 111 |
+
)
|
| 112 |
|
|
|
|
| 113 |
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
gradio
|
| 3 |
+
torch
|
| 4 |
+
sentencepiece
|
| 5 |
+
spaces
|