Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import os
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
from io import BytesIO
|
@@ -6,8 +5,6 @@ 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()
|
@@ -39,13 +36,8 @@ def upload_to_tempfiles(doc_stream: BytesIO) -> str:
|
|
39 |
except Exception:
|
40 |
return "上传失败"
|
41 |
|
42 |
-
def process_text(
|
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": "输入文本不能为空"}
|
@@ -59,16 +51,16 @@ def process_text(request: gr.Request, text: str) -> dict:
|
|
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 |
-
flagging_mode="never"
|
70 |
)
|
71 |
|
72 |
-
# 启动应用
|
73 |
if __name__ == "__main__":
|
74 |
-
app.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
1 |
import requests
|
2 |
import gradio as gr
|
3 |
from io import BytesIO
|
|
|
5 |
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
6 |
from docx.shared import Pt
|
7 |
|
|
|
|
|
8 |
def create_docx(text: str) -> BytesIO:
|
9 |
"""创建Word文档并返回内存字节流"""
|
10 |
doc = Document()
|
|
|
36 |
except Exception:
|
37 |
return "上传失败"
|
38 |
|
39 |
+
def process_text(text: str) -> dict:
|
40 |
+
"""处理API请求的核心函数(无需API Key)"""
|
|
|
|
|
|
|
|
|
|
|
41 |
# 处理空文本
|
42 |
if not text.strip():
|
43 |
return {"output": "输入文本不能为空"}
|
|
|
51 |
except Exception as e:
|
52 |
return {"output": f"处理失败: {str(e)}"}
|
53 |
|
54 |
+
# 创建Gradio接口
|
55 |
app = gr.Interface(
|
56 |
fn=process_text,
|
57 |
inputs=gr.Textbox(label="输入文本", lines=5),
|
58 |
outputs=gr.JSON(label="处理结果"),
|
59 |
title="文本转DOCX API",
|
60 |
description="输入文本将转为楷体居中的DOCX文档,返回文件托管URL",
|
61 |
+
flagging_mode="never"
|
62 |
)
|
63 |
|
64 |
+
# 启动应用
|
65 |
if __name__ == "__main__":
|
66 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|