1
0

3 Commits a4524882ef ... b6f7bdd350

Autor SHA1 Nachricht Datum
  chenhaiyang b6f7bdd350 Updated Python environment vor 1 Jahr
  chenhaiyang a20f7dc11e delete nlp router vor 1 Jahr
  chenhaiyang ffddf73053 delete some unused files vor 1 Jahr

+ 0 - 29
app/api/routers/nlp.py

@@ -1,29 +0,0 @@
-from fastapi import APIRouter, Query
-
-from app.controllers.nlp.meeting import get_caught_result
-from app.models.domain.nlp import MeetingInfoResponse
-
-router = APIRouter()
-
-
-@router.get("/meeting/info", response_model=MeetingInfoResponse)
-async def catch_meeting_info(sentence: str = Query(..., max_length=100)):
-    (
-        start_time,
-        end_time,
-        duration,
-        room_size,
-        topic,
-        name_list,
-    ) = await get_caught_result(sentence)
-    response = {
-        "Message": "success",
-        "AcceptableStartTime": start_time,
-        "AcceptableEndTime": end_time,
-        "MeetingDurationSeconds": duration,
-        "MeetingRoomSize": room_size,
-        "Topic": topic,
-        "Participants": name_list,
-    }
-
-    return response

+ 0 - 0
app/controllers/nlp/__init__.py


+ 0 - 90
app/controllers/nlp/meeting.py

@@ -1,90 +0,0 @@
-from httpx import AsyncClient
-from loguru import logger
-
-from app.services.duckling import Duckling
-from app.services.tencent_nlp import TencentNLP
-
-
-class MeetingInfoCatcher:
-    def __init__(self, nlp_service: TencentNLP, duckling: Duckling):
-        super(MeetingInfoCatcher, self).__init__()
-        self.nlp_service = nlp_service
-        self.duckling = duckling
-
-    async def extract_time(self, sentence: str) -> tuple[str, str, int]:
-        start_time, end_time, duration = "", "", -1
-        parsed = await self.duckling.parse(sentence)
-        for dim in parsed:
-            if dim["dim"] == "time":
-                start_time = dim["value"]["from"]["value"]
-                end_time = dim["value"]["to"]["value"]
-            if dim["dim"] == "duration":
-                duration = dim["value"]["normalized"]["value"]
-
-        return start_time, end_time, duration
-
-    async def extract_room_size(self, sentence: str) -> str:
-        dp_tokens = await self.nlp_service.get_dependency(sentence)
-        size = ""
-        for token in dp_tokens:
-            if await self.nlp_service.get_word_similarity(token.Word, "会议室") > 0.8:
-                index = token.Id
-                for item in dp_tokens:
-                    if item.HeadId == index:
-                        if (
-                                await self.nlp_service.get_word_similarity(item.Word, "小")
-                                > 0.9
-                        ):
-                            size = "small"
-                        if (
-                                await self.nlp_service.get_word_similarity(item.Word, "中")
-                                > 0.9
-                        ):
-                            size = "medium"
-                        if (
-                                await self.nlp_service.get_word_similarity(item.Word, "大")
-                                > 0.9
-                        ):
-                            size = "large"
-                break
-
-        return size
-
-    async def extract_topic(self, sentence: str) -> str:
-        summarization = await self.nlp_service.get_auto_summarization_result(sentence)
-
-        return summarization
-
-    async def extract_name(self, sentence: str) -> list[str]:
-        _, ner_tokens = await self.nlp_service.get_lexical_analysis_result(sentence)
-        name_list = []
-        if ner_tokens:
-            for token in ner_tokens:
-                if token.Type == "PER":
-                    name_list.append(token.Word)
-
-        return name_list
-
-    async def run(self, sentence: str) -> tuple[str, str, int, str, str, list[str]]:
-        similarity = await self.nlp_service.get_text_similarity_result(
-            "我要开会", [sentence]
-        )
-        if similarity[-1].Score < 0.5:
-            return "", "", -1, "", "", []
-        else:
-            start_time, end_time, interval = await self.extract_time(sentence)
-            topic = await self.extract_topic(sentence)
-            name_list = await self.extract_name(sentence)
-            room_size = await self.extract_room_size(sentence)
-
-            return start_time, end_time, interval, room_size, topic, name_list
-
-
-@logger.catch()
-async def get_caught_result(sentence: str) -> tuple[str, str, int, str, str, list[str]]:
-    async with AsyncClient() as client:
-        duckling = Duckling(client)
-        service = TencentNLP()
-
-        catcher = MeetingInfoCatcher(service, duckling)
-        return await catcher.run(sentence)

+ 0 - 2
app/main.py

@@ -12,7 +12,6 @@ from app.api.routers import (
     space,
     bluetooth,
     devices,
-    nlp,
     positioning,
 )
 from app.api.routers.model_path import early_start
@@ -35,7 +34,6 @@ def get_application() -> FastAPI:
     application.include_router(bluetooth.router, prefix="/bluetooth", tags=["BLE"])
     application.include_router(devices.router, prefix="/devices", tags=["Devices"])
     application.include_router(early_start.router, prefix="/model-path", tags=["Model Path"])
-    application.include_router(nlp.router, prefix="/nlp-service", tags=["NLP"])
     application.include_router(positioning.router, prefix="/positioning-service", tags=["Positioning Service"])
     application.include_router(space.router, prefix="/room", tags=["Spaces"])
     application.include_router(targets.router, prefix="/target", tags=["Targets"])

+ 0 - 23
app/models/domain/nlp.py

@@ -1,23 +0,0 @@
-from enum import Enum
-
-from pydantic import BaseModel
-
-
-class RoomSize(str, Enum):
-    small = "small"
-    medium = "medium"
-    large = "large"
-    unknown = ""
-
-
-class NLPResponseBase(BaseModel):
-    Message: str
-
-
-class MeetingInfoResponse(NLPResponseBase):
-    AcceptableStartTime: str | None
-    AcceptableEndTime: str | None
-    MeetingDurationSeconds: int | None
-    MeetingRoomSize: RoomSize | None
-    Topic: str | None
-    Participants: list[str]

+ 0 - 25
app/services/duckling.py

@@ -1,25 +0,0 @@
-import os
-
-from httpx import AsyncClient, URL
-
-from app.core.config import settings
-from app.services.service import api_exception
-
-
-class Duckling:
-    """
-    Duckling is a Haskell library that parses text into structured data.
-    """
-
-    def __init__(self, client: AsyncClient, server_settings=settings):
-        super(Duckling, self).__init__()
-        self._client = client
-        self._host = URL(os.getenv("DUCKLING_HOST", settings.DUCKLING_HOST))
-
-    @api_exception
-    async def parse(self, text: str, locale: str = "zh_CN") -> dict:
-        url = self._host.join("parse")
-        data = {"locale": locale, "text": text}
-        raw_response = await self._client.post(url, data=data)
-
-        return raw_response.json()

+ 0 - 90
app/services/tencent_nlp.py

@@ -1,90 +0,0 @@
-import json
-
-from loguru import logger
-from tencentcloud.common import credential
-from tencentcloud.common.exception.tencent_cloud_sdk_exception import (
-    TencentCloudSDKException,
-)
-from tencentcloud.common.profile.client_profile import ClientProfile
-from tencentcloud.common.profile.http_profile import HttpProfile
-from tencentcloud.nlp.v20190408 import models, nlp_client
-
-from app.core.config import settings
-
-
-def get_tencent_nlp_client() -> nlp_client.NlpClient:
-    try:
-        cred = credential.Credential(
-            settings.TENCENT_SECRET_ID_V1, settings.TENCENT_SECRET_KEY_V1
-        )
-        http_profile = HttpProfile()
-        http_profile.reqMethod = "GET"
-        http_profile.endpoint = settings.TENCENT_NLP_ENDPOINT
-
-        client_profile = ClientProfile()
-        client_profile.httpProfile = http_profile
-        client = nlp_client.NlpClient(cred, "ap-guangzhou", client_profile)
-
-        return client
-    except TencentCloudSDKException as e:
-        logger.error(e)
-
-
-class TencentNLP:
-    def __init__(self):
-        cred = credential.Credential(
-            settings.TENCENT_SECRET_ID_V1, settings.TENCENT_SECRET_KEY_V1
-        )
-        http_profile = HttpProfile()
-        http_profile.reqMethod = "GET"
-        http_profile.endpoint = settings.TENCENT_NLP_ENDPOINT
-
-        client_profile = ClientProfile()
-        client_profile.httpProfile = http_profile
-        client = nlp_client.NlpClient(cred, "ap-guangzhou", client_profile)
-        self.client = client
-
-    async def get_lexical_analysis_result(self, text: str) -> tuple[list, list]:
-        req = models.LexicalAnalysisRequest()
-        params = {"Text": text}
-        req.from_json_string(json.dumps(params))
-        resp = self.client.LexicalAnalysis(req)
-
-        return resp.PosTokens, resp.NerTokens
-
-    async def get_auto_summarization_result(self, text: str) -> str:
-        req = models.AutoSummarizationRequest()
-        params = {"Text": text}
-        req.from_json_string(json.dumps(params))
-        resp = self.client.AutoSummarization(req)
-
-        return resp.Summary
-
-    async def get_text_similarity_result(
-            self, src_text: str, target_text: list[str]
-    ) -> list:
-        req = models.TextSimilarityRequest()
-        params = {"SrcText": src_text, "TargetText": target_text}
-        req.from_json_string(json.dumps(params))
-        resp = self.client.TextSimilarity(req)
-
-        return resp.Similarity
-
-    async def get_dependency(self, text: str) -> list:
-        req = models.DependencyParsingRequest()
-        params = {"Text": text}
-        req.from_json_string(json.dumps(params))
-        resp = self.client.DependencyParsing(req)
-
-        return resp.DpTokens
-
-    async def get_word_similarity(self, src_word: str, target: str) -> float:
-        try:
-            req = models.WordSimilarityRequest()
-            params = {"SrcWord": src_word, "TargetWord": target}
-            req.from_json_string(json.dumps(params))
-            resp = self.client.WordSimilarity(req)
-        except TencentCloudSDKException:
-            return 0
-
-        return resp.Similarity

+ 8 - 4
requirements.txt

@@ -1,13 +1,18 @@
 anyio==3.6.2
 arrow==1.2.3
 certifi==2023.5.7
-charset-normalizer==2.0.12
+charset-normalizer==3.1.0
 click==8.1.3
 environs==9.5.0
 fastapi==0.95.2
 greenlet==2.0.2
 grpcio==1.53.0
 h11==0.14.0
+h2==4.1.0
+hpack==4.0.0
+httpcore==0.17.2
+httpx==0.24.1
+hyperframe==6.0.1
 idna==3.4
 joblib==1.2.0
 loguru==0.7.0
@@ -26,7 +31,7 @@ python-dateutil==2.8.2
 python-dotenv==1.0.0
 pytz==2023.3
 redis==4.5.5
-requests==2.28.0
+requests==2.31.0
 scikit-learn==1.2.2
 scipy==1.10.1
 setuptools==67.7.2
@@ -34,11 +39,10 @@ six==1.16.0
 sniffio==1.3.0
 SQLAlchemy==2.0.15
 starlette==0.27.0
-tencentcloud-sdk-python==3.0.899
 threadpoolctl==3.1.0
 typing_extensions==4.6.1
 tzdata==2023.3
 ujson==5.7.0
-urllib3==1.26.16
+urllib3==2.0.2
 uvicorn==0.22.0
 wheel==0.40.0