File size: 1,315 Bytes
6232477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List, Dict

# Initialize the NER pipeline
ner_model = pipeline("ner", grouped_entities=True)

# Define the FastAPI app
app = FastAPI()

# Define the request and response models for the API
class NERRequest(BaseModel):
    text: str

class Entity(BaseModel):
    entity_group: str
    start: int
    end: int
    score: float
    word: str

class NERResponse(BaseModel):
    entities: List[Entity]

@app.post("/ner", response_model=NERResponse)
def get_entities(request: NERRequest):
    # Use the NER model to detect entities
    entities = ner_model(request.text)
    # Convert entities to the response model
    response_entities = [Entity(**entity) for entity in entities]
    return NERResponse(entities=response_entities)

# Define the Gradio interface function
def ner_demo(text):
    entities = ner_model(text)
    return {"entities": entities}

# Create the Gradio interface
iface = gr.Interface(
    fn=ner_demo,
    inputs=gr.Textbox(lines=10, placeholder="Enter text here..."),
    outputs=gr.JSON(),
    title="Named Entity Recognition",
    description="Enter text to extract named entities using a NER model."
)

# Launch the Gradio interface
iface.launch(share=True)