fexeak
commited on
Commit
·
cb41d64
1
Parent(s):
5861199
feat: 添加AI提示词生成器和聊天助手界面
Browse files- 新增app01.py实现基于UnfilteredAI/Promt-generator的提示词生成器
- 重构app.py为基于NSFW-Flash模型的聊天助手界面
- 更新requirements.txt指定gradio版本要求
- app.py +157 -33
- app01.py +168 -0
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,41 +1,165 @@
|
|
1 |
import torch
|
2 |
-
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
model =
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
#
|
20 |
-
|
|
|
21 |
|
22 |
-
#
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
use_cache=False,
|
33 |
-
eos_token_id=tokenizer.eos_token_id,
|
34 |
-
streamer=streamer
|
35 |
-
)
|
36 |
-
|
37 |
-
# # Decode the generated text
|
38 |
-
# output_text = tokenizer.decode(generated_text[0], skip_special_tokens=True)
|
39 |
-
|
40 |
-
# # Print the generated text
|
41 |
-
# print(output_text)
|
|
|
1 |
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import threading
|
5 |
+
import time
|
6 |
|
7 |
+
# Global variables for model and tokenizer
|
8 |
+
model = None
|
9 |
+
tokenizer = None
|
10 |
+
model_loaded = False
|
11 |
|
12 |
+
def load_model():
|
13 |
+
"""Load the model and tokenizer"""
|
14 |
+
global model, tokenizer, model_loaded
|
15 |
+
try:
|
16 |
+
print("Loading model...")
|
17 |
+
model = AutoModelForCausalLM.from_pretrained(
|
18 |
+
"UnfilteredAI/NSFW-Flash",
|
19 |
+
trust_remote_code=True,
|
20 |
+
torch_dtype=torch.bfloat16
|
21 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
22 |
+
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
24 |
+
"UnfilteredAI/NSFW-Flash",
|
25 |
+
trust_remote_code=True
|
26 |
+
)
|
27 |
+
|
28 |
+
model_loaded = True
|
29 |
+
print("Model loaded successfully!")
|
30 |
+
except Exception as e:
|
31 |
+
print(f"Error loading model: {e}")
|
32 |
+
model_loaded = False
|
33 |
|
34 |
+
def generate_response(message, history, temperature, max_length, top_p):
|
35 |
+
"""Generate response from the model"""
|
36 |
+
global model, tokenizer, model_loaded
|
37 |
+
|
38 |
+
if not model_loaded:
|
39 |
+
return "模型尚未加载完成,请稍等..."
|
40 |
+
|
41 |
+
try:
|
42 |
+
# Build conversation history
|
43 |
+
chat = [
|
44 |
+
{"role": "system", "content": "You are NSFW-Flash, an AI assistant. Respond helpfully and appropriately."}
|
45 |
+
]
|
46 |
+
|
47 |
+
# Add conversation history
|
48 |
+
for user_msg, bot_msg in history:
|
49 |
+
chat.append({"role": "user", "content": user_msg})
|
50 |
+
if bot_msg:
|
51 |
+
chat.append({"role": "assistant", "content": bot_msg})
|
52 |
+
|
53 |
+
# Add current message
|
54 |
+
chat.append({"role": "user", "content": message})
|
55 |
+
|
56 |
+
# Apply chat template
|
57 |
+
chat_text = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
|
58 |
+
|
59 |
+
# Tokenize
|
60 |
+
inputs = tokenizer(chat_text, return_tensors="pt", return_attention_mask=False)
|
61 |
+
if torch.cuda.is_available():
|
62 |
+
inputs = inputs.to("cuda")
|
63 |
+
|
64 |
+
# Generate
|
65 |
+
with torch.no_grad():
|
66 |
+
generated = model.generate(
|
67 |
+
**inputs,
|
68 |
+
max_length=max_length,
|
69 |
+
temperature=temperature,
|
70 |
+
top_p=top_p,
|
71 |
+
do_sample=True,
|
72 |
+
use_cache=False,
|
73 |
+
eos_token_id=tokenizer.eos_token_id,
|
74 |
+
pad_token_id=tokenizer.eos_token_id
|
75 |
+
)
|
76 |
+
|
77 |
+
# Decode response
|
78 |
+
response = tokenizer.decode(generated[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
79 |
+
return response.strip()
|
80 |
+
|
81 |
+
except Exception as e:
|
82 |
+
return f"生成回复时出错: {str(e)}"
|
83 |
|
84 |
+
def chat_interface(message, history, temperature, max_length, top_p):
|
85 |
+
"""Chat interface for Gradio"""
|
86 |
+
response = generate_response(message, history, temperature, max_length, top_p)
|
87 |
+
history.append([message, response])
|
88 |
+
return "", history
|
89 |
|
90 |
+
# Load model in background
|
91 |
+
loading_thread = threading.Thread(target=load_model)
|
92 |
+
loading_thread.start()
|
93 |
|
94 |
+
# Create Gradio interface
|
95 |
+
with gr.Blocks(title="AI Chat Assistant") as demo:
|
96 |
+
gr.Markdown("# 🤖 AI Chat Assistant")
|
97 |
+
gr.Markdown("基于 NSFW-Flash 模型的聊天助手")
|
98 |
+
|
99 |
+
with gr.Row():
|
100 |
+
with gr.Column(scale=3):
|
101 |
+
chatbot = gr.Chatbot(
|
102 |
+
value=[],
|
103 |
+
height=500,
|
104 |
+
show_label=False
|
105 |
+
)
|
106 |
+
|
107 |
+
with gr.Row():
|
108 |
+
msg = gr.Textbox(
|
109 |
+
placeholder="输入您的消息...",
|
110 |
+
show_label=False,
|
111 |
+
scale=4
|
112 |
+
)
|
113 |
+
send_btn = gr.Button("发送", scale=1)
|
114 |
+
|
115 |
+
clear_btn = gr.Button("清空对话")
|
116 |
+
|
117 |
+
with gr.Column(scale=1):
|
118 |
+
gr.Markdown("### 参数设置")
|
119 |
+
temperature = gr.Slider(
|
120 |
+
minimum=0.1,
|
121 |
+
maximum=2.0,
|
122 |
+
value=0.7,
|
123 |
+
step=0.1,
|
124 |
+
label="Temperature"
|
125 |
+
)
|
126 |
+
max_length = gr.Slider(
|
127 |
+
minimum=100,
|
128 |
+
maximum=2000,
|
129 |
+
value=1000,
|
130 |
+
step=100,
|
131 |
+
label="最大长度"
|
132 |
+
)
|
133 |
+
top_p = gr.Slider(
|
134 |
+
minimum=0.1,
|
135 |
+
maximum=1.0,
|
136 |
+
value=0.95,
|
137 |
+
step=0.05,
|
138 |
+
label="Top-p"
|
139 |
+
)
|
140 |
+
|
141 |
+
# Event handlers
|
142 |
+
send_btn.click(
|
143 |
+
chat_interface,
|
144 |
+
inputs=[msg, chatbot, temperature, max_length, top_p],
|
145 |
+
outputs=[msg, chatbot]
|
146 |
+
)
|
147 |
+
|
148 |
+
msg.submit(
|
149 |
+
chat_interface,
|
150 |
+
inputs=[msg, chatbot, temperature, max_length, top_p],
|
151 |
+
outputs=[msg, chatbot]
|
152 |
+
)
|
153 |
+
|
154 |
+
clear_btn.click(
|
155 |
+
lambda: ([], ""),
|
156 |
+
outputs=[chatbot, msg]
|
157 |
+
)
|
158 |
|
159 |
+
if __name__ == "__main__":
|
160 |
+
demo.launch(
|
161 |
+
server_name="0.0.0.0",
|
162 |
+
server_port=7860,
|
163 |
+
share=True,
|
164 |
+
show_error=True
|
165 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app01.py
ADDED
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import threading
|
5 |
+
import time
|
6 |
+
|
7 |
+
# Global variables for model and tokenizer
|
8 |
+
model = None
|
9 |
+
tokenizer = None
|
10 |
+
model_loaded = False
|
11 |
+
|
12 |
+
def load_model():
|
13 |
+
"""Load the model and tokenizer"""
|
14 |
+
global model, tokenizer, model_loaded
|
15 |
+
try:
|
16 |
+
print("Loading Prompt Generator model...")
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained("UnfilteredAI/Promt-generator")
|
18 |
+
model = AutoModelForCausalLM.from_pretrained(
|
19 |
+
"UnfilteredAI/Promt-generator",
|
20 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
21 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
22 |
+
|
23 |
+
model_loaded = True
|
24 |
+
print("Prompt Generator model loaded successfully!")
|
25 |
+
except Exception as e:
|
26 |
+
print(f"Error loading model: {e}")
|
27 |
+
model_loaded = False
|
28 |
+
|
29 |
+
def generate_prompt(input_text, max_length, temperature, top_p, num_return_sequences):
|
30 |
+
"""Generate enhanced prompts from input text"""
|
31 |
+
global model, tokenizer, model_loaded
|
32 |
+
|
33 |
+
if not model_loaded:
|
34 |
+
return "模型尚未加载完成,请稍等..."
|
35 |
+
|
36 |
+
if not input_text.strip():
|
37 |
+
return "请输入一些文本作为提示词的起始内容。"
|
38 |
+
|
39 |
+
try:
|
40 |
+
# Tokenize input
|
41 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
42 |
+
if torch.cuda.is_available():
|
43 |
+
inputs = inputs.to("cuda")
|
44 |
+
|
45 |
+
# Generate
|
46 |
+
with torch.no_grad():
|
47 |
+
outputs = model.generate(
|
48 |
+
**inputs,
|
49 |
+
max_length=max_length,
|
50 |
+
temperature=temperature,
|
51 |
+
top_p=top_p,
|
52 |
+
do_sample=True,
|
53 |
+
num_return_sequences=num_return_sequences,
|
54 |
+
pad_token_id=tokenizer.eos_token_id,
|
55 |
+
eos_token_id=tokenizer.eos_token_id
|
56 |
+
)
|
57 |
+
|
58 |
+
# Decode generated prompts
|
59 |
+
generated_prompts = []
|
60 |
+
for output in outputs:
|
61 |
+
generated_text = tokenizer.decode(output, skip_special_tokens=True)
|
62 |
+
generated_prompts.append(generated_text)
|
63 |
+
|
64 |
+
return "\n\n---\n\n".join(generated_prompts)
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
return f"生成提示词时出错: {str(e)}"
|
68 |
+
|
69 |
+
def clear_output():
|
70 |
+
"""Clear the output"""
|
71 |
+
return ""
|
72 |
+
|
73 |
+
# Load model in background
|
74 |
+
loading_thread = threading.Thread(target=load_model)
|
75 |
+
loading_thread.start()
|
76 |
+
|
77 |
+
# Create Gradio interface
|
78 |
+
with gr.Blocks(title="AI Prompt Generator") as demo:
|
79 |
+
gr.Markdown("# 🎨 AI Prompt Generator")
|
80 |
+
gr.Markdown("基于 UnfilteredAI/Promt-generator 模型的智能提示词生成器")
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
with gr.Column(scale=2):
|
84 |
+
input_text = gr.Textbox(
|
85 |
+
label="输入起始文本",
|
86 |
+
placeholder="例如: a red car, beautiful landscape, futuristic city...",
|
87 |
+
lines=3
|
88 |
+
)
|
89 |
+
|
90 |
+
with gr.Row():
|
91 |
+
generate_btn = gr.Button("生成提示词", variant="primary", scale=2)
|
92 |
+
clear_btn = gr.Button("清空", scale=1)
|
93 |
+
|
94 |
+
output_text = gr.Textbox(
|
95 |
+
label="生成的提示词",
|
96 |
+
lines=10,
|
97 |
+
max_lines=20,
|
98 |
+
show_copy_button=True
|
99 |
+
)
|
100 |
+
|
101 |
+
with gr.Column(scale=1):
|
102 |
+
gr.Markdown("### 生成参数")
|
103 |
+
|
104 |
+
max_length = gr.Slider(
|
105 |
+
minimum=50,
|
106 |
+
maximum=500,
|
107 |
+
value=150,
|
108 |
+
step=10,
|
109 |
+
label="最大长度"
|
110 |
+
)
|
111 |
+
|
112 |
+
temperature = gr.Slider(
|
113 |
+
minimum=0.1,
|
114 |
+
maximum=2.0,
|
115 |
+
value=0.8,
|
116 |
+
step=0.1,
|
117 |
+
label="Temperature (创造性)"
|
118 |
+
)
|
119 |
+
|
120 |
+
top_p = gr.Slider(
|
121 |
+
minimum=0.1,
|
122 |
+
maximum=1.0,
|
123 |
+
value=0.9,
|
124 |
+
step=0.05,
|
125 |
+
label="Top-p (多样性)"
|
126 |
+
)
|
127 |
+
|
128 |
+
num_return_sequences = gr.Slider(
|
129 |
+
minimum=1,
|
130 |
+
maximum=5,
|
131 |
+
value=3,
|
132 |
+
step=1,
|
133 |
+
label="生成数量"
|
134 |
+
)
|
135 |
+
|
136 |
+
gr.Markdown("### 使用说明")
|
137 |
+
gr.Markdown(
|
138 |
+
"""- **输入起始文本**: 描述你想要的内容主题
|
139 |
+
- **Temperature**: 控制生成的随机性,越高越有创意
|
140 |
+
- **Top-p**: 控制词汇选择的多样性
|
141 |
+
- **生成数量**: 一次生成多个不同的提示词"""
|
142 |
+
)
|
143 |
+
|
144 |
+
# Event handlers
|
145 |
+
generate_btn.click(
|
146 |
+
generate_prompt,
|
147 |
+
inputs=[input_text, max_length, temperature, top_p, num_return_sequences],
|
148 |
+
outputs=output_text
|
149 |
+
)
|
150 |
+
|
151 |
+
input_text.submit(
|
152 |
+
generate_prompt,
|
153 |
+
inputs=[input_text, max_length, temperature, top_p, num_return_sequences],
|
154 |
+
outputs=output_text
|
155 |
+
)
|
156 |
+
|
157 |
+
clear_btn.click(
|
158 |
+
clear_output,
|
159 |
+
outputs=output_text
|
160 |
+
)
|
161 |
+
|
162 |
+
if __name__ == "__main__":
|
163 |
+
demo.launch(
|
164 |
+
server_name="0.0.0.0",
|
165 |
+
server_port=7861,
|
166 |
+
share=False,
|
167 |
+
show_error=True
|
168 |
+
)
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
gradio
|
2 |
transformers @ git+https://github.com/huggingface/transformers.git@main
|
3 |
torch
|
4 |
accelerate
|
|
|
1 |
+
gradio>=4.0.0
|
2 |
transformers @ git+https://github.com/huggingface/transformers.git@main
|
3 |
torch
|
4 |
accelerate
|