|
import gradio as gr |
|
from fastapi import FastAPI |
|
from pydantic import BaseModel |
|
from transformers import AutoTokenizer, AutoModelForTokenClassification |
|
import torch |
|
from threading import Thread |
|
import uvicorn |
|
import requests |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
model_name = "mdarhri00/named-entity-recognition" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForTokenClassification.from_pretrained(model_name) |
|
|
|
class TextInput(BaseModel): |
|
text: str |
|
|
|
@app.post("/predict") |
|
async def predict(input: TextInput): |
|
text = input.text |
|
|
|
|
|
inputs = tokenizer(text, return_tensors="pt") |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
|
|
|
|
logits = outputs.logits |
|
predictions = torch.argmax(logits, dim=2) |
|
|
|
|
|
id2label = model.config.id2label |
|
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]) |
|
entities = [{"token": token, "label": id2label[prediction.item()]} for token, prediction in zip(tokens, predictions[0])] |
|
|
|
return {"entities": entities} |
|
|
|
|
|
def start_api(): |
|
uvicorn.run(app, host="0.0.0.0", port=8000) |
|
|
|
api_thread = Thread(target=start_api, daemon=True) |
|
api_thread.start() |
|
|
|
|
|
def predict_gradio(text): |
|
response = requests.post("https://asmalljob-docker01.hf.space/predict", json={"text": text}) |
|
entities = response.json().get("entities", []) |
|
return entities |
|
|
|
demo = gr.Interface(fn=predict_gradio, inputs="text", outputs="json") |
|
demo.launch(share=True) |
|
|