1Noura commited on
Commit
5c763a2
·
verified ·
1 Parent(s): 0f497d7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+
4
+ # Load NER model for English and Arabic
5
+ ner_pipeline_en = pipeline('ner', grouped_entities=True) # English model
6
+ ner_pipeline_ar = pipeline('ner', model='CAMeL-Lab/bert-base-arabic-camelbert-msa-ner', grouped_entities=True) # Arabic model
7
+
8
+ def get_ner_pipeline(language='English'): #Return the NER model based on the specified language.
9
+ if language == 'Arabic':
10
+ return ner_pipeline_ar # Return Arabic model
11
+ return ner_pipeline_en # Return English model
12
+
13
+ def highlight_entities(text, language='English'): #Extract entities and return the text with highlighted entities.
14
+ ner_pipeline = get_ner_pipeline(language) # Get the appropriate NER model
15
+ entities = ner_pipeline(text) # Process the input text
16
+
17
+ # Create a list to store the highlighted text
18
+ highlighted_text_data = []
19
+ last_index = 0
20
+
21
+ for entity in entities:
22
+ entity_name = entity['word'] # Get the entity name
23
+ entity_type = entity['entity_group'] # Get the entity type
24
+ # Add text before the entity
25
+ highlighted_text_data.append((text[last_index: text.index(entity_name, last_index)], None))
26
+ # Add the entity with its type
27
+ highlighted_text_data.append((f"{entity_name}", entity_type))
28
+ last_index = text.index(entity_name, last_index) + len(entity_name)
29
+
30
+ # Add any remaining text after the last entity
31
+ highlighted_text_data.append((text[last_index:], None))
32
+
33
+ return highlighted_text_data # Return the highlighted entities
34
+
35
+ # Custom CSS for right-to-left (RTL) text alignment
36
+ custom_css = """
37
+ #output {
38
+ direction: rtl; /* Right-to-left for Arabic */
39
+ text-align: right; /* Align right for Arabic */
40
+ }
41
+ """
42
+
43
+ # Gradio interface setup
44
+ interface = gr.Interface(
45
+ fn=highlight_entities, # Function to call
46
+ inputs=[
47
+ gr.Textbox(label="Input Text", lines=5, placeholder="Enter your text here..."), # Text input
48
+ gr.Radio(label="Select Language", choices=["English", "Arabic"], value="English") # Language selection
49
+ ],
50
+ outputs=gr.HighlightedText(label="Highlighted NER Results", elem_id="output"), # Output as highlighted text
51
+ title="Named Entity Recognition", # Interface title
52
+ description="Select a language and enter text to extract and highlight named entities.", # Description
53
+ examples=[
54
+ ["Hugging Face Inc. is a company based in New York City.", "English"],
55
+ ["أحمد هو عالم في مجال الذكاء الاصطناعي", "Arabic"] ], # Add example inputs
56
+ css=custom_css # Apply custom CSS for RTL
57
+ )
58
+
59
+ # Launch the interface
60
+ interface.launch() # Start the Gradio interface