Spaces:
Runtime error
Runtime error
File size: 936 Bytes
54921ec 66691b2 54921ec 66691b2 85ed553 66691b2 85ed553 54921ec 66691b2 54921ec |
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 |
import gradio as gr
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from transformers import pipeline, AutoTokenizer, AutoModelForTokenClassification
# 正確載入 safetensors 模型
model = pipeline(
"ner",
model=AutoModelForTokenClassification.from_pretrained("ckiplab/bert-base-chinese-ner", use_safetensors=True),
tokenizer=AutoTokenizer.from_pretrained("ckiplab/bert-base-chinese-ner"),
aggregation_strategy="simple"
)
def analyze(sentence: str):
result = model(sentence)
return " ".join([f"{r['word']}/{r['entity_group']}" for r in result])
demo = gr.Interface(fn=analyze, inputs="text", outputs="text", title="實體辨識")
app = FastAPI()
app = gr.mount_gradio_app(app, demo, path="/")
@app.post("/analyze")
async def api_analyze(request: Request):
payload = await request.json()
return JSONResponse(content={"result": analyze(payload.get("sentence", ""))}) |