fexeak
commited on
Commit
·
faf3f39
1
Parent(s):
382017b
feat: 添加异步启动和端口暴露功能
Browse files- 引入PortLinkClient用于端口暴露
- 使用asyncio实现Gradio的异步启动
- 添加全局端口配置变量
- 使用nest_asyncio解决异步事件循环问题
app.py
CHANGED
@@ -1,8 +1,26 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
def greet(name):
|
4 |
return "Hello " + name + "!"
|
5 |
|
6 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from yanghao.portlink import PortLinkClient as Client
|
3 |
+
import psutil
|
4 |
+
import asyncio
|
5 |
+
|
6 |
+
# 全局端口配置
|
7 |
+
PORT = 7860
|
8 |
|
9 |
def greet(name):
|
10 |
return "Hello " + name + "!"
|
11 |
|
12 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
13 |
+
|
14 |
+
async def launchGradio():
|
15 |
+
demo.launch(server_name="0.0.0.0", server_port=PORT)
|
16 |
+
|
17 |
+
async def main():
|
18 |
+
gr_task = asyncio.create_task(launchGradio())
|
19 |
+
# Expose port to the public internet. Check the debug output for the specific public URL.
|
20 |
+
async with Client(PORT) as c:
|
21 |
+
await c.link(PORT)
|
22 |
+
|
23 |
+
if __name__ == "__main__":
|
24 |
+
import nest_asyncio
|
25 |
+
nest_asyncio.apply()
|
26 |
+
await main()
|