Spaces:
Build error
Build error
Commit
·
f5ecfb4
1
Parent(s):
eb7dd0e
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("valurank/bert-base-NER")
|
| 6 |
+
model = AutoModelForTokenClassification.from_pretrained("valurank/bert-base-NER")
|
| 7 |
+
nlp = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
|
| 8 |
+
|
| 9 |
+
def rename_group(output_list):
|
| 10 |
+
final_output = []
|
| 11 |
+
|
| 12 |
+
for output in output_list:
|
| 13 |
+
output["entity"] = output["entity_group"]
|
| 14 |
+
del output["entity_group"]
|
| 15 |
+
|
| 16 |
+
final_output.append(output)
|
| 17 |
+
|
| 18 |
+
return final_output
|
| 19 |
+
|
| 20 |
+
def remove_prefix(word, prefix):
|
| 21 |
+
if prefix in word:
|
| 22 |
+
return word.split(prefix, 1)[1]
|
| 23 |
+
return " " + word
|
| 24 |
+
|
| 25 |
+
def join_results(results):
|
| 26 |
+
joined_results = []
|
| 27 |
+
|
| 28 |
+
for result in results:
|
| 29 |
+
if "##" in result["word"] and joined_results:
|
| 30 |
+
joined_results[-1]["end"] = result["end"]
|
| 31 |
+
joined_results[-1]["word"] += remove_prefix(result["word"], "##")
|
| 32 |
+
joined_results[-1]["score"] = min(joined_results[-1]["score"], result["score"])
|
| 33 |
+
else:
|
| 34 |
+
joined_results.append(result)
|
| 35 |
+
|
| 36 |
+
return joined_results
|
| 37 |
+
|
| 38 |
+
examples = [
|
| 39 |
+
""" Texas A&M professor used chatbot chatbot to assess students' grades.
|
| 40 |
+
The OpenAI chatbot is actually called ChatGPT and claims to have written every paper written by the bot.
|
| 41 |
+
The bot isn’t made to detect material composed by AI, or even material produced by itself.
|
| 42 |
+
Texas A&M University-Commerce said they are investigating the incident and developing policies related to AI in the classroom.
|
| 43 |
+
The university denied that anyone had received a failing grade.
|
| 44 |
+
The school also confirmed that several students had been cleared of any academic dishonesty.
|
| 45 |
+
The use of AI in coursework is a rapidly changing issue that confronts all learning institutions."""
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
def ner(text):
|
| 49 |
+
output = nlp(text)
|
| 50 |
+
output = join_results(output)
|
| 51 |
+
output = rename_group(output)
|
| 52 |
+
|
| 53 |
+
return {"text": text, "entities": output}
|
| 54 |
+
|
| 55 |
+
demo = gr.Interface(ner,
|
| 56 |
+
gr.Textbox(placeholder="Enter sentence here..."),
|
| 57 |
+
gr.HighlightedText(),
|
| 58 |
+
examples=examples)
|
| 59 |
+
|
| 60 |
+
if __name__ == '__main__':
|
| 61 |
+
demo.launch(debug=True)
|