BuckLakeAI / app.py
parkerjj's picture
Change to Gradio
fe0aec3
raw
history blame
1.26 kB
from fastapi import FastAPI
from pydantic import BaseModel
#from preprocess import preprocess_text # 假设您已经在 preprocess.py 中定义了此函数
import gradio as gr
app = FastAPI()
# 定义请求模型
class TextRequest(BaseModel):
text: str
# 定义 API 路由
@app.post("/api/preprocess")
async def preprocess_endpoint(request: TextRequest):
"""
接收文本并返回预处理后的结果
"""
if not request.text.strip():
return {"error": "Text cannot be empty."}
result = request.text + " (preprocessed)"
return result
# 使用 Gradio 创建 Web 界面
def gradio_interface(text):
return text + " (preprocessed)"
# Gradio Interface 配置
iface = gr.Interface(
fn=gradio_interface,
inputs="textbox", # 使用新版 Gradio 直接定义输入类型
outputs="json", # 使用新版 Gradio 直接定义输出类型
title="文本预处理 API",
description="发送文本到 /api/preprocess 进行预处理并获取 JSON 格式的响应"
)
# 将 Gradio 的 Web 界面挂载到 FastAPI 应用的 /gradio 路径下
app = gr.mount_gradio_app(app, iface, path="/gradio")
# 启动应用
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=63468)