Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from fastapi.responses import JSONResponse, HTMLResponse
|
| 4 |
+
import logging
|
| 5 |
+
# 创建 FastAPI 实例
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
# 设置日志配置
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
# 请求体的模型
|
| 11 |
+
class UserInput(BaseModel):
|
| 12 |
+
user_input: str
|
| 13 |
+
|
| 14 |
+
@app.get("/", response_class=HTMLResponse)
|
| 15 |
+
async def read_root():
|
| 16 |
+
# 提供前端 HTML 文件
|
| 17 |
+
with open("index.html", "r") as file:
|
| 18 |
+
return file.read()
|
| 19 |
+
|
| 20 |
+
@app.post("/submit")
|
| 21 |
+
async def submit_input(input_data: UserInput):
|
| 22 |
+
# 处理用户输入并返回响应
|
| 23 |
+
logger.info("input coming")
|
| 24 |
+
|
| 25 |
+
return JSONResponse(content={"message": input_data.user_input})
|