Spaces:
Sleeping
Sleeping
Change to Gradio
Browse files- Dockerfile +15 -2
- app.py +31 -6
- requirements.txt +6 -3
Dockerfile
CHANGED
|
@@ -13,5 +13,18 @@ RUN pip install -r requirements.txt
|
|
| 13 |
# 公开 Gradio 默认的端口 7860
|
| 14 |
EXPOSE 7860
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# 公开 Gradio 默认的端口 7860
|
| 14 |
EXPOSE 7860
|
| 15 |
|
| 16 |
+
# Set up a new user named "user" with user ID 1000
|
| 17 |
+
RUN useradd -m -u 1000 user
|
| 18 |
+
# Switch to the "user" user
|
| 19 |
+
USER user
|
| 20 |
+
# Set home to the user's home directory
|
| 21 |
+
ENV HOME=/home/user \
|
| 22 |
+
PATH=/home/user/.local/bin:$PATH
|
| 23 |
+
|
| 24 |
+
# Set the working directory to the user's home directory
|
| 25 |
+
WORKDIR $HOME/app
|
| 26 |
+
|
| 27 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
| 28 |
+
COPY --chown=user . $HOME/app
|
| 29 |
+
|
| 30 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
|
@@ -3,6 +3,9 @@ from fastapi import FastAPI
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
app = FastAPI() # 创建 FastAPI 应用
|
| 7 |
|
| 8 |
# 定义请求模型
|
|
@@ -11,7 +14,20 @@ class TextRequest(BaseModel):
|
|
| 11 |
|
| 12 |
# 定义两个 API 路由处理函数
|
| 13 |
@app.post("/api/aaa")
|
| 14 |
-
async def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
result = request.text + 'aaa'
|
| 16 |
return {"result": result}
|
| 17 |
|
|
@@ -20,12 +36,21 @@ async def api_bbb(request: TextRequest):
|
|
| 20 |
result = request.text + 'bbb'
|
| 21 |
return {"result": result}
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
@app.get("/")
|
| 24 |
async def root():
|
| 25 |
return {"message": "Welcome to the API. Use /api/aaa or /api/bbb for processing."}
|
| 26 |
|
| 27 |
-
# 启动应用,使用环境变量指定的端口
|
| 28 |
-
if __name__ == "__main__":
|
| 29 |
-
import uvicorn
|
| 30 |
-
port = int(os.getenv("PORT", 7860)) # 获取 PORT 环境变量
|
| 31 |
-
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 5 |
|
| 6 |
+
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
|
| 9 |
app = FastAPI() # 创建 FastAPI 应用
|
| 10 |
|
| 11 |
# 定义请求模型
|
|
|
|
| 14 |
|
| 15 |
# 定义两个 API 路由处理函数
|
| 16 |
@app.post("/api/aaa")
|
| 17 |
+
async def api_aaa_post(request: TextRequest):
|
| 18 |
+
result = request.text + 'aaa'
|
| 19 |
+
return {"result": result}
|
| 20 |
+
|
| 21 |
+
# 定义两个 API 路由处理函数
|
| 22 |
+
@app.post("/aaa")
|
| 23 |
+
async def aaa(request: TextRequest):
|
| 24 |
+
result = request.text + 'aaa'
|
| 25 |
+
return {"result": result}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# 定义两个 API 路由处理函数
|
| 29 |
+
@app.get("/aaa")
|
| 30 |
+
async def api_aaa_get(request: TextRequest):
|
| 31 |
result = request.text + 'aaa'
|
| 32 |
return {"result": result}
|
| 33 |
|
|
|
|
| 36 |
result = request.text + 'bbb'
|
| 37 |
return {"result": result}
|
| 38 |
|
| 39 |
+
|
| 40 |
+
pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 41 |
+
|
| 42 |
+
@app.get("/infer_t5")
|
| 43 |
+
def t5_get(input):
|
| 44 |
+
output = pipe_flan(input)
|
| 45 |
+
return {"output": output[0]["generated_text"]}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@app.post("/infer_t5")
|
| 49 |
+
def t5_post(input):
|
| 50 |
+
output = pipe_flan(input)
|
| 51 |
+
return {"output": output[0]["generated_text"]}
|
| 52 |
+
|
| 53 |
@app.get("/")
|
| 54 |
async def root():
|
| 55 |
return {"message": "Welcome to the API. Use /api/aaa or /api/bbb for processing."}
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
gradio
|
| 2 |
transformers
|
| 3 |
akshare
|
| 4 |
blis==0.7.11
|
|
@@ -6,5 +5,9 @@ spacy==3.7.5
|
|
| 6 |
gensim
|
| 7 |
numpy
|
| 8 |
gensim
|
| 9 |
-
fastapi
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
transformers
|
| 2 |
akshare
|
| 3 |
blis==0.7.11
|
|
|
|
| 5 |
gensim
|
| 6 |
numpy
|
| 7 |
gensim
|
| 8 |
+
fastapi==0.74.*
|
| 9 |
+
requests==2.27.*
|
| 10 |
+
sentencepiece==0.1.*
|
| 11 |
+
torch==1.11.*
|
| 12 |
+
transformers==4.*
|
| 13 |
+
uvicorn[standard]==0.17.*
|