@@ -1,5 +1,5 @@
|
|||||||
apiVersion: v2
|
apiVersion: v2
|
||||||
name: mark-word-fastapi
|
name: fastapi-k8s-app
|
||||||
description: A Helm chart for deploying FastAPI application on Kubernetes
|
description: A Helm chart for deploying FastAPI application on Kubernetes
|
||||||
version: 0.1.0 # Chart 版本
|
version: 0.1.0 # Chart 版本
|
||||||
appVersion: "1.0.0" # 应用版本,通常与镜像版本关联
|
appVersion: "1.0.0" # 应用版本,通常与镜像版本关联
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{{/*
|
{{/*
|
||||||
Expand the name of the chart.
|
Expand the name of the chart.
|
||||||
*/}}
|
*/}}
|
||||||
{{- define "mark-word-fastapi.name" -}}
|
{{- define "fastapi-k8s-app.name" -}}
|
||||||
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
|
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
|
||||||
{{- end }}
|
{{- 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).
|
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.
|
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 }}
|
{{- if .Values.fullnameOverride }}
|
||||||
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
|
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
|
||||||
{{- else }}
|
{{- 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.
|
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 "-" }}
|
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
{{/*
|
{{/*
|
||||||
Common labels
|
Common labels
|
||||||
*/}}
|
*/}}
|
||||||
{{- define "mark-word-fastapi.labels" -}}
|
{{- define "fastapi-k8s-app.labels" -}}
|
||||||
helm.sh/chart: {{ include "mark-word-fastapi.chart" . }}
|
helm.sh/chart: {{ include "fastapi-k8s-app.chart" . }}
|
||||||
{{ include "mark-word-fastapi.selectorLabels" . }}
|
{{ include "fastapi-k8s-app.selectorLabels" . }}
|
||||||
{{- if .Chart.AppVersion }}
|
{{- if .Chart.AppVersion }}
|
||||||
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
|
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
@@ -45,7 +45,7 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
|
|||||||
{{/*
|
{{/*
|
||||||
Selector labels
|
Selector labels
|
||||||
*/}}
|
*/}}
|
||||||
{{- define "mark-word-fastapi.selectorLabels" -}}
|
{{- define "fastapi-k8s-app.selectorLabels" -}}
|
||||||
app.kubernetes.io/name: {{ include "mark-word-fastapi.name" . }}
|
app.kubernetes.io/name: {{ include "fastapi-k8s-app.name" . }}
|
||||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: {{ include "mark-word-fastapi.fullname" . }}
|
name: {{ include "fastapi-k8s-app.fullname" . }}
|
||||||
labels:
|
labels:
|
||||||
{{- include "mark-word-fastapi.labels" . | nindent 4 }}
|
{{- include "fastapi-k8s-app.labels" . | nindent 4 }}
|
||||||
spec:
|
spec:
|
||||||
replicas: {{ .Values.replicaCount }}
|
replicas: {{ .Values.replicaCount }}
|
||||||
selector:
|
selector:
|
||||||
matchLabels:
|
matchLabels:
|
||||||
{{- include "mark-word-fastapi.selectorLabels" . | nindent 6 }}
|
{{- include "fastapi-k8s-app.selectorLabels" . | nindent 6 }}
|
||||||
template:
|
template:
|
||||||
metadata:
|
metadata:
|
||||||
labels:
|
labels:
|
||||||
{{- include "mark-word-fastapi.selectorLabels" . | nindent 8 }}
|
{{- include "fastapi-k8s-app.selectorLabels" . | nindent 8 }}
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: {{ .Chart.Name }}
|
- name: {{ .Chart.Name }}
|
||||||
|
|||||||
@@ -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 }}
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
metadata:
|
metadata:
|
||||||
name: {{ include "mark-word-fastapi.fullname" . }}
|
name: {{ include "fastapi-k8s-app.fullname" . }}
|
||||||
labels:
|
labels:
|
||||||
{{- include "mark-word-fastapi.labels" . | nindent 4 }}
|
{{- include "fastapi-k8s-app.labels" . | nindent 4 }}
|
||||||
spec:
|
spec:
|
||||||
type: {{ .Values.service.type }}
|
type: {{ .Values.service.type }}
|
||||||
ports:
|
ports:
|
||||||
@@ -12,4 +12,4 @@ spec:
|
|||||||
protocol: TCP
|
protocol: TCP
|
||||||
name: http
|
name: http
|
||||||
selector:
|
selector:
|
||||||
{{- include "mark-word-fastapi.selectorLabels" . | nindent 4 }}
|
{{- include "fastapi-k8s-app.selectorLabels" . | nindent 4 }}
|
||||||
@@ -17,4 +17,4 @@ ingress:
|
|||||||
# tls:
|
# tls:
|
||||||
# - hosts:
|
# - hosts:
|
||||||
# - markword.simpla.dev
|
# - markword.simpla.dev
|
||||||
# secretName: fastapi-tls-secret # 你的TLS Secret名称
|
# secretName: fastapi-tls-secret # 你的TLS Secret名称
|
||||||
131
mark_word.py
Normal file
131
mark_word.py
Normal file
@@ -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"<span class='word {tag[0]}'>{word}</span>"
|
||||||
|
else:
|
||||||
|
colored_text += word # 没有词性时直接添加词语
|
||||||
|
|
||||||
|
# 替换换行符为 <br>
|
||||||
|
return colored_text.replace("\n", "<br>")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@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"<html><body>{content}</body></html>"
|
||||||
|
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("<br>"):
|
||||||
|
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("<br>", r"\line ")
|
||||||
|
return rtf_header + rtf_body + rtf_footer
|
||||||
|
|
||||||
Reference in New Issue
Block a user