Appearance
入门
安装Gradio
bash
pip install gradio
示例程序
python
import gradio as gr
def greet(name, intensity):
return "Hello, " + name + "!" * int(intensity)
demo = gr.Interface(
fn=greet,
inputs=["text", "slider"],
outputs=["text"],
)
demo.launch(server_name='0.0.0.0', server_port=5040, show_error=True)
在Docker中使用Gradio
在一个文件夹下放入app.py, requirements.txt, Dockerfile三个文件
app.py 内容如下
python
import gradio as gr
def greet(name, intensity):
return "Hello, " + name + "!" * int(intensity)
demo = gr.Interface(
fn=greet,
inputs=["text", "slider"],
outputs=["text"],
)
demo.launch(server_name='0.0.0.0', server_port=5040, show_error=True)
直接生成requirements.txt文件
bash
pip freeze > requirements.txt
参考的requirements.txt 内容如下
txt
aiofiles==23.2.1
annotated-types==0.7.0
anyio==4.8.0
certifi==2024.12.14
charset-normalizer==3.4.1
click==8.1.8
fastapi==0.115.6
ffmpy==0.5.0
filelock==3.16.1
fsspec==2024.12.0
gradio==5.10.0
gradio_client==1.5.3
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
huggingface-hub==0.27.1
idna==3.10
Jinja2==3.1.5
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
numpy==2.2.1
orjson==3.10.14
packaging==24.2
pandas==2.2.3
pillow==11.1.0
pydantic==2.10.4
pydantic_core==2.27.2
pydub==0.25.1
Pygments==2.19.1
python-dateutil==2.9.0.post0
python-multipart==0.0.20
pytz==2024.2
PyYAML==6.0.2
requests==2.32.3
rich==13.9.4
ruff==0.8.6
safehttpx==0.1.6
semantic-version==2.10.0
shellingham==1.5.4
six==1.17.0
sniffio==1.3.1
starlette==0.41.3
tomlkit==0.13.2
tqdm==4.67.1
typer==0.15.1
typing_extensions==4.12.2
tzdata==2024.2
urllib3==2.3.0
uvicorn==0.34.0
websockets==14.1
Dockerfile内容如下
dockerfile
FROM python:3.11.11-bookworm
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 5040
CMD ["python", "app.py"]
构建镜像
bash
docker build -t gradio_app:latest .
运行镜像
bash
docker run -p 5040:5040 gradio_app:latest