fexeak
commited on
Commit
·
382017b
1
Parent(s):
ad71468
refactor: 重构应用代码结构并简化gradio启动逻辑
Browse files将原本的模型加载和推理代码移至单独的app.03.py文件
简化gradio_app.py中的网络检查和不必要的服务器状态打印
将app.py改为简单的gradio示例接口
- app.03.py +35 -0
- app.py +6 -33
- gradio_app.py +1 -18
app.03.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
from huggingface_hub import login
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 设置 Hugging Face token
|
| 6 |
+
HF_TOKEN = os.getenv('HF_TOKEN') # 从环境变量获取 token
|
| 7 |
+
if HF_TOKEN:
|
| 8 |
+
login(token=HF_TOKEN) # 使用 token 登录
|
| 9 |
+
else:
|
| 10 |
+
print("请设置环境变量 HF_TOKEN 为你的 Hugging Face token")
|
| 11 |
+
print("Windows 设置方法: $env:HF_TOKEN = '你的token'")
|
| 12 |
+
print("或者直接在代码中设置: login(token='你的token')")
|
| 13 |
+
exit(1)
|
| 14 |
+
|
| 15 |
+
safe_pipe = pipeline(
|
| 16 |
+
"text-generation",
|
| 17 |
+
model="meta-llama/Meta-Llama-3-8B",
|
| 18 |
+
torch_dtype="auto",
|
| 19 |
+
device_map="auto"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
naive_pipe = pipeline(
|
| 23 |
+
"text-generation",
|
| 24 |
+
model="microsoft/DialoGPT-medium",
|
| 25 |
+
torch_dtype="auto",
|
| 26 |
+
device_map="auto"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
safe_out = safe_pipe(prompt, max_new_tokens=100, do_sample=False)[0]["generated_text"]
|
| 30 |
+
naive_out = naive_pipe(prompt, max_new_tokens=100, do_sample=False)[0]["generated_text"]
|
| 31 |
+
|
| 32 |
+
print("=== 安全对齐模型回答 ===")
|
| 33 |
+
print(safe_out)
|
| 34 |
+
print("\n=== 无对齐模型回答 ===")
|
| 35 |
+
print(naive_out)
|
app.py
CHANGED
|
@@ -1,35 +1,8 @@
|
|
| 1 |
-
|
| 2 |
-
from huggingface_hub import login
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
if HF_TOKEN:
|
| 8 |
-
login(token=HF_TOKEN) # 使用 token 登录
|
| 9 |
-
else:
|
| 10 |
-
print("请设置环境变量 HF_TOKEN 为你的 Hugging Face token")
|
| 11 |
-
print("Windows 设置方法: $env:HF_TOKEN = '你的token'")
|
| 12 |
-
print("或者直接在代码中设置: login(token='你的token')")
|
| 13 |
-
exit(1)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
torch_dtype="auto",
|
| 19 |
-
device_map="auto"
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
naive_pipe = pipeline(
|
| 23 |
-
"text-generation",
|
| 24 |
-
model="microsoft/DialoGPT-medium",
|
| 25 |
-
torch_dtype="auto",
|
| 26 |
-
device_map="auto"
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
safe_out = safe_pipe(prompt, max_new_tokens=100, do_sample=False)[0]["generated_text"]
|
| 30 |
-
naive_out = naive_pipe(prompt, max_new_tokens=100, do_sample=False)[0]["generated_text"]
|
| 31 |
-
|
| 32 |
-
print("=== 安全对齐模型回答 ===")
|
| 33 |
-
print(safe_out)
|
| 34 |
-
print("\n=== 无对齐模型回答 ===")
|
| 35 |
-
print(naive_out)
|
|
|
|
| 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 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gradio_app.py
CHANGED
|
@@ -68,26 +68,10 @@ with gr.Blocks() as demo:
|
|
| 68 |
inputs=text_input
|
| 69 |
)
|
| 70 |
|
| 71 |
-
def check_network():
|
| 72 |
-
import socket
|
| 73 |
-
try:
|
| 74 |
-
# 测试与google dns的连接
|
| 75 |
-
socket.create_connection(("8.8.8.8", 53), timeout=3)
|
| 76 |
-
return True
|
| 77 |
-
except OSError:
|
| 78 |
-
return False
|
| 79 |
-
|
| 80 |
if __name__ == "__main__":
|
| 81 |
try:
|
| 82 |
-
print("[INFO] 检查网络连接...")
|
| 83 |
-
if check_network():
|
| 84 |
-
print("[INFO] 网络连接正常")
|
| 85 |
-
else:
|
| 86 |
-
print("[WARNING] 网络连接可能不稳定,这可能会影响模型加载和公共URL访问")
|
| 87 |
-
|
| 88 |
print("[INFO] 启动Gradio界面...")
|
| 89 |
-
demo.
|
| 90 |
-
server = demo.launch(
|
| 91 |
server_name="0.0.0.0", # 允许外部访问
|
| 92 |
server_port=7860, # 指定端口
|
| 93 |
share=True, # 创建公共URL
|
|
@@ -95,7 +79,6 @@ if __name__ == "__main__":
|
|
| 95 |
show_api=False, # 不显示API文档
|
| 96 |
favicon_path=None # 禁用favicon请求
|
| 97 |
)
|
| 98 |
-
print(f"[INFO] Gradio服务器状态: {server}")
|
| 99 |
except Exception as e:
|
| 100 |
print(f"[ERROR] Gradio启动失败: {str(e)}")
|
| 101 |
raise e
|
|
|
|
| 68 |
inputs=text_input
|
| 69 |
)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
if __name__ == "__main__":
|
| 72 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
print("[INFO] 启动Gradio界面...")
|
| 74 |
+
demo.launch(
|
|
|
|
| 75 |
server_name="0.0.0.0", # 允许外部访问
|
| 76 |
server_port=7860, # 指定端口
|
| 77 |
share=True, # 创建公共URL
|
|
|
|
| 79 |
show_api=False, # 不显示API文档
|
| 80 |
favicon_path=None # 禁用favicon请求
|
| 81 |
)
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
print(f"[ERROR] Gradio启动失败: {str(e)}")
|
| 84 |
raise e
|