diff --git a/kubernetes/fastapi-chart/Chart.yaml b/kubernetes/fastapi-chart/Chart.yaml index 4d3b922..c472b31 100644 --- a/kubernetes/fastapi-chart/Chart.yaml +++ b/kubernetes/fastapi-chart/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v2 -name: mark-word-fastapi +name: fastapi-k8s-app description: A Helm chart for deploying FastAPI application on Kubernetes version: 0.1.0 # Chart 版本 appVersion: "1.0.0" # 应用版本,通常与镜像版本关联 \ No newline at end of file diff --git a/kubernetes/fastapi-chart/templates/_helpers.tpl b/kubernetes/fastapi-chart/templates/_helpers.tpl index b9c6584..014339a 100644 --- a/kubernetes/fastapi-chart/templates/_helpers.tpl +++ b/kubernetes/fastapi-chart/templates/_helpers.tpl @@ -1,7 +1,7 @@ {{/* Expand the name of the chart. */}} -{{- define "mark-word-fastapi.name" -}} +{{- define "fastapi-k8s-app.name" -}} {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} {{- end }} @@ -10,7 +10,7 @@ Create a default fully qualified app name. We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). If release name contains chart name it will be used as a full name. */}} -{{- define "mark-word-fastapi.fullname" -}} +{{- define "fastapi-k8s-app.fullname" -}} {{- if .Values.fullnameOverride }} {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} {{- else }} @@ -26,16 +26,16 @@ If release name contains chart name it will be used as a full name. {{/* Create chart name and version as part of the label. */}} -{{- define "mark-word-fastapi.chart" -}} +{{- define "fastapi-k8s-app.chart" -}} {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} {{- end }} {{/* Common labels */}} -{{- define "mark-word-fastapi.labels" -}} -helm.sh/chart: {{ include "mark-word-fastapi.chart" . }} -{{ include "mark-word-fastapi.selectorLabels" . }} +{{- define "fastapi-k8s-app.labels" -}} +helm.sh/chart: {{ include "fastapi-k8s-app.chart" . }} +{{ include "fastapi-k8s-app.selectorLabels" . }} {{- if .Chart.AppVersion }} app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} {{- end }} @@ -45,7 +45,7 @@ app.kubernetes.io/managed-by: {{ .Release.Service }} {{/* Selector labels */}} -{{- define "mark-word-fastapi.selectorLabels" -}} -app.kubernetes.io/name: {{ include "mark-word-fastapi.name" . }} +{{- define "fastapi-k8s-app.selectorLabels" -}} +app.kubernetes.io/name: {{ include "fastapi-k8s-app.name" . }} app.kubernetes.io/instance: {{ .Release.Name }} {{- end }} \ No newline at end of file diff --git a/kubernetes/fastapi-chart/templates/deployment.yaml b/kubernetes/fastapi-chart/templates/deployment.yaml index e03da87..ef7bc4d 100644 --- a/kubernetes/fastapi-chart/templates/deployment.yaml +++ b/kubernetes/fastapi-chart/templates/deployment.yaml @@ -1,18 +1,18 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: {{ include "mark-word-fastapi.fullname" . }} + name: {{ include "fastapi-k8s-app.fullname" . }} labels: - {{- include "mark-word-fastapi.labels" . | nindent 4 }} + {{- include "fastapi-k8s-app.labels" . | nindent 4 }} spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: - {{- include "mark-word-fastapi.selectorLabels" . | nindent 6 }} + {{- include "fastapi-k8s-app.selectorLabels" . | nindent 6 }} template: metadata: labels: - {{- include "mark-word-fastapi.selectorLabels" . | nindent 8 }} + {{- include "fastapi-k8s-app.selectorLabels" . | nindent 8 }} spec: containers: - name: {{ .Chart.Name }} diff --git a/kubernetes/fastapi-chart/templates/ingress.yaml b/kubernetes/fastapi-chart/templates/ingress.yaml deleted file mode 100644 index ec5e179..0000000 --- a/kubernetes/fastapi-chart/templates/ingress.yaml +++ /dev/null @@ -1,31 +0,0 @@ -# kubernetes/fastapi-chart/templates/ingress.yaml -{{- if .Values.ingress.enabled -}} -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - name: {{ include "mark-word-fastapi.fullname" . }} - labels: - {{- include "mark-word-fastapi.labels" . | nindent 4 }} - annotations: - # 可以添加 Traefik 特定的 annotation,例如强制重定向到 HTTPS - # traefik.ingress.kubernetes.io/router.entrypoints: web, websecure - # traefik.ingress.kubernetes.io/router.tls: "true" - # traefik.ingress.kubernetes.io/router.middlewares: default-redirect-https@kubernetescrd -spec: - ingressClassName: {{ .Values.ingress.className }} - rules: - - host: {{ .Values.ingress.host | quote }} - http: - paths: - - path: / - pathType: Prefix - backend: - service: - name: {{ include "mark-word-fastapi.fullname" . }} - port: - number: {{ .Values.service.port }} - {{- if .Values.ingress.tls }} - tls: - {{- toYaml .Values.ingress.tls | nindent 4 }} - {{- end }} -{{- end }} \ No newline at end of file diff --git a/kubernetes/fastapi-chart/templates/service.yaml b/kubernetes/fastapi-chart/templates/service.yaml index 19e0601..3e0d6cb 100644 --- a/kubernetes/fastapi-chart/templates/service.yaml +++ b/kubernetes/fastapi-chart/templates/service.yaml @@ -1,9 +1,9 @@ apiVersion: v1 kind: Service metadata: - name: {{ include "mark-word-fastapi.fullname" . }} + name: {{ include "fastapi-k8s-app.fullname" . }} labels: - {{- include "mark-word-fastapi.labels" . | nindent 4 }} + {{- include "fastapi-k8s-app.labels" . | nindent 4 }} spec: type: {{ .Values.service.type }} ports: @@ -12,4 +12,4 @@ spec: protocol: TCP name: http selector: - {{- include "mark-word-fastapi.selectorLabels" . | nindent 4 }} \ No newline at end of file + {{- include "fastapi-k8s-app.selectorLabels" . | nindent 4 }} \ No newline at end of file diff --git a/kubernetes/fastapi-chart/values.yaml b/kubernetes/fastapi-chart/values.yaml index a8f8a7f..015d0cb 100644 --- a/kubernetes/fastapi-chart/values.yaml +++ b/kubernetes/fastapi-chart/values.yaml @@ -17,4 +17,4 @@ ingress: # tls: # - hosts: # - markword.simpla.dev - # secretName: fastapi-tls-secret # 你的TLS Secret名称 + # secretName: fastapi-tls-secret # 你的TLS Secret名称 \ No newline at end of file diff --git a/mark_word.py b/mark_word.py new file mode 100644 index 0000000..4e9616f --- /dev/null +++ b/mark_word.py @@ -0,0 +1,131 @@ +# from docx import Document +import os +from tempfile import NamedTemporaryFile + +import thulac +from fastapi import FastAPI, Form, UploadFile, File, Request +from fastapi.responses import FileResponse +from fastapi.responses import HTMLResponse +from fastapi.templating import Jinja2Templates +from pydantic import BaseModel +from reportlab.lib.pagesizes import letter +from reportlab.pdfgen import canvas + + +class ExportRequest(BaseModel): + format: str # 导出格式(html、pdf、rtf) + content: str # 要导出的内容 + +# 初始化 THULAC 分词工具 +thu = thulac.thulac() + +# 初始化 FastAPI 应用和模板 +app = FastAPI() +templates = Jinja2Templates(directory="templates") + + +@app.get("/", response_class=HTMLResponse) +async def home(request: Request): + return templates.TemplateResponse("index.html", {"request": request}) + + +@app.post("/process", response_class=HTMLResponse) +async def process(request: Request, file: UploadFile = File(None), text: str = Form(None)): + """ + 统一处理上传文件和粘贴文本,返回标注结果页面 + """ + if file: + # 读取上传文件内容 + content = await file.read() + text_content = content.decode("utf-8") + elif text: + # 读取粘贴文本内容 + text_content = text + else: + return templates.TemplateResponse( + "error.html", {"request": request, "message": "未提供文件或文本"} + ) + + # 处理文本,返回标注结果 + processed_text = process_text(text) + + return templates.TemplateResponse("result.html", {"request": request, "content": processed_text}) + + +def process_text(text: str) -> str: + """处理文本并为每种词性添加 CSS 类,同时保留段落换行""" + words = thu.cut(text) + colored_text = "" + + for word, tag in words: + if tag: # 确保 tag 不为空 + colored_text += f"{word}" + else: + colored_text += word # 没有词性时直接添加词语 + + # 替换换行符为
+ return colored_text.replace("\n", "
") + + + +@app.post("/export") +async def export_file(request: ExportRequest): + format = request.format + content = request.content + + if format == "html": + file_name = "exported_result.html" + file_content = f"{content}" + return create_file_response(file_name, file_content.encode("utf-8"), "text/html") + + elif format == "pdf": + with NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file: + generate_pdf(temp_file.name, content) + return FileResponse(temp_file.name, filename="exported_result.pdf", media_type="application/pdf") + + elif format == "rtf": + file_name = "exported_result.rtf" + file_content = generate_rtf(content) + return create_file_response(file_name, file_content.encode("utf-8"), "application/rtf") + + return {"error": "Unsupported format"} + + +def create_file_response(file_name, file_content, media_type): + """ + 创建文件响应 + """ + with NamedTemporaryFile(delete=False, suffix=os.path.splitext(file_name)[1]) as temp_file: + temp_file.write(file_content) + temp_file.flush() + return FileResponse(temp_file.name, filename=file_name, media_type=media_type) + + +def generate_pdf(file_path, content): + """ + 生成 PDF 文件 + """ + c = canvas.Canvas(file_path, pagesize=letter) + c.setFont("Helvetica", 12) + width, height = letter + y = height - 40 + + for line in content.split("
"): + c.drawString(40, y, line.strip()) + y -= 20 + if y < 40: # 换页条件 + c.showPage() + c.setFont("Helvetica", 12) + y = height - 40 + c.save() + + +def generate_rtf(content): + """ + 生成 RTF 文件 + """ + rtf_header = r"{\rtf1\ansi\deff0" + rtf_footer = r"}" + rtf_body = content.replace("
", r"\line ") + return rtf_header + rtf_body + rtf_footer +