Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +74 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from docx import Document
|
| 6 |
+
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
| 7 |
+
from docx.shared import Pt
|
| 8 |
+
|
| 9 |
+
SECRET_KEY = os.getenv("API_KEY", "default_secret_key") # 从环境变量获取apikey
|
| 10 |
+
|
| 11 |
+
def create_docx(text: str) -> BytesIO:
|
| 12 |
+
"""创建Word文档并返回内存字节流"""
|
| 13 |
+
doc = Document()
|
| 14 |
+
for paragraph_text in text.split('\n'):
|
| 15 |
+
if paragraph_text.strip(): # 跳过空行
|
| 16 |
+
p = doc.add_paragraph()
|
| 17 |
+
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
| 18 |
+
run = p.add_run(paragraph_text.strip())
|
| 19 |
+
run.font.name = '楷体'
|
| 20 |
+
run.font.size = Pt(12)
|
| 21 |
+
output = BytesIO()
|
| 22 |
+
doc.save(output)
|
| 23 |
+
output.seek(0)
|
| 24 |
+
return output
|
| 25 |
+
|
| 26 |
+
def upload_to_tempfiles(doc_stream: BytesIO) -> str:
|
| 27 |
+
"""上传文档到临时文件托管平台"""
|
| 28 |
+
filename = "document.docx"
|
| 29 |
+
api_url = "https://tmpfiles.org/api/v1/upload"
|
| 30 |
+
files = {'file': (filename, doc_stream, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')}
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
response = requests.post(api_url, files=files)
|
| 34 |
+
if response.status_code == 200:
|
| 35 |
+
response_json = response.json()
|
| 36 |
+
if response_json.get('status') == 'success' and 'data' in response_json and 'url' in response_json['data']:
|
| 37 |
+
return response_json['data']['url']
|
| 38 |
+
return "上传失败"
|
| 39 |
+
except Exception:
|
| 40 |
+
return "上传失败"
|
| 41 |
+
|
| 42 |
+
def process_text(request: gr.Request, text: str) -> dict:
|
| 43 |
+
"""处理API请求的核心函数"""
|
| 44 |
+
# 验证apikey
|
| 45 |
+
api_key = request.headers.get("apikey", "")
|
| 46 |
+
if api_key != SECRET_KEY:
|
| 47 |
+
return {"output": "无效的API密钥"}
|
| 48 |
+
|
| 49 |
+
# 处理空文本
|
| 50 |
+
if not text.strip():
|
| 51 |
+
return {"output": "输入文本不能为空"}
|
| 52 |
+
|
| 53 |
+
# 创建并上传文档
|
| 54 |
+
try:
|
| 55 |
+
doc_stream = create_docx(text)
|
| 56 |
+
file_url = upload_to_tempfiles(doc_stream)
|
| 57 |
+
doc_stream.close()
|
| 58 |
+
return {"output": file_url}
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return {"output": f"处理失败: {str(e)}"}
|
| 61 |
+
|
| 62 |
+
# 创建Gradio接口
|
| 63 |
+
app = gr.Interface(
|
| 64 |
+
fn=process_text,
|
| 65 |
+
inputs=gr.Textbox(label="输入文本", lines=5),
|
| 66 |
+
outputs=gr.JSON(label="处理结果"),
|
| 67 |
+
title="文本转DOCX API",
|
| 68 |
+
description="输入文本将转为楷体居中的DOCX文档,返回文件托管URL",
|
| 69 |
+
allow_flagging="never"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# 启动应用
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
app.launch(api_open=False, show_api=True)
|
requirements.txt
ADDED
|
File without changes
|