Files

58 lines
1.7 KiB
Python

"""
Text rewriting functions for style transformation.
"""
from fastapi import Body
from sse_starlette.sse import EventSourceResponse
from typing import AsyncIterable
import json
async def formal_style_iterator(text: str) -> AsyncIterable[str]:
"""正式风格改写"""
yield json.dumps({"text": text}, ensure_ascii=False)
async def formal_style(text: str = Body(..., description="输入文本")):
"""正式风格改写"""
return EventSourceResponse(formal_style_iterator(text))
async def party_style_iterator(text: str) -> AsyncIterable[str]:
"""党政风格改写"""
yield json.dumps({"text": text}, ensure_ascii=False)
async def party_style(text: str = Body(..., description="输入文本")):
"""党政风格改写"""
return EventSourceResponse(party_style_iterator(text))
async def col_style_iterator(text: str) -> AsyncIterable[str]:
"""口语风格改写"""
yield json.dumps({"text": text}, ensure_ascii=False)
async def col_style(text: str = Body(..., description="输入文本")):
"""口语风格改写"""
return EventSourceResponse(col_style_iterator(text))
async def chi_to_ens_iterator(text: str) -> AsyncIterable[str]:
"""中文转英文"""
yield json.dumps({"text": text}, ensure_ascii=False)
async def chi_to_ens(text: str = Body(..., description="输入文本")):
"""中文转英文"""
return EventSourceResponse(chi_to_ens_iterator(text))
async def ens_to_chi_iterator(text: str) -> AsyncIterable[str]:
"""英文转中文"""
yield json.dumps({"text": text}, ensure_ascii=False)
async def ens_to_chi(text: str = Body(..., description="输入文本")):
"""英文转中文"""
return EventSourceResponse(ens_to_chi_iterator(text))