Spaces:
Runtime error
Runtime error
File size: 2,611 Bytes
d5e9814 8c040d4 d5e9814 8c040d4 d5e9814 8c040d4 d5e9814 8c040d4 d5e9814 |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
from fastapi import FastAPI
import json
import difflib
from flair.models import SequenceTagger
from flair.data import Sentence
app = FastAPI()
# Ladda Flair flerspråkig NER-modell
try:
tagger = SequenceTagger.load("flair/ner-multi")
except Exception as e:
print(f"Error loading model: {str(e)}")
raise e
# Ladda entiteter från entities.json
with open("entities.json") as f:
entities = json.load(f)
ITEMS = set(entities["items"])
COLORS = set(entities["colors"])
PRICES = set(entities["prices"])
def correct_spelling(word, valid_words, threshold=0.7):
"""Korrigera stavfel."""
normalized = word.rstrip("etn")
matches = difflib.get_close_matches(normalized, valid_words, n=1, cutoff=threshold)
return matches[0] if matches else word
@app.post("/parse")
async def parse_user_request(request: str):
if not request or len(request) > 200:
return {"error": "Ogiltig eller för lång begäran"}
try:
# Skapa Flair Sentence
sentence = Sentence(request)
# Prediktera NER-taggar
tagger.predict(sentence)
# Extrahera entiteter
result_entities = {}
# Kolla färger och priser i hela meningen
words = request.lower().split()
for word in words:
if word in COLORS:
result_entities["FÄRG"] = word
elif word in PRICES:
result_entities["PRIS"] = word
# Extrahera varor från NER
for entity in sentence.get_spans("ner"):
if entity.tag in ["MISC", "ORG", "LOC"]: # Diverse, organisationer, platser som potentiella objekt
corrected = correct_spelling(entity.text.lower(), ITEMS)
if corrected in ITEMS:
result_entities["VARA"] = corrected
elif not result_entities.get("VARA"):
result_entities["VARA"] = entity.text.lower()
# Om ingen vara hittades
if "VARA" not in result_entities:
return {"result": "error:ingen vara"}
# Skapa strukturerad sträng
result_parts = [f"vara:{result_entities['VARA']}"]
if "FÄRG" in result_entities:
result_parts.append(f"färg:{result_entities['FÄRG']}")
if "PRIS" in result_entities:
result_parts.append(f"pris:{result_entities['PRIS']}")
return {"result": ",".join(result_parts)}
except Exception as e:
return {"error": f"Fel vid parsning: {str(e)}"}
@app.get("/")
async def root():
return {"message": "Request Parser API is running!"} |