Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
from transformers import AutoTokenizer
|
| 4 |
-
import os
|
| 5 |
import torch
|
| 6 |
|
| 7 |
-
# ---
|
| 8 |
model_id = "xingyu1996/tiger-gpt2"
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# --- 关键变化: 直接加载与训练时相同的分词器 ---
|
| 12 |
-
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 13 |
|
| 14 |
def respond(
|
| 15 |
message,
|
|
@@ -18,42 +14,30 @@ def respond(
|
|
| 18 |
temperature,
|
| 19 |
top_p,
|
| 20 |
):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
"stream": True,
|
| 29 |
-
"details": True, # 让 API 返回 token ID (重要变化)
|
| 30 |
}
|
| 31 |
-
|
| 32 |
-
if temperature
|
| 33 |
-
|
| 34 |
-
if top_p
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
response_text = current_text
|
| 48 |
-
|
| 49 |
-
yield response_text
|
| 50 |
-
elif hasattr(output, 'generated_text'): # 非流式生成时的最终输出
|
| 51 |
-
# 如果直接返回了完整文本,就用它
|
| 52 |
-
response_text = output.generated_text
|
| 53 |
-
yield response_text
|
| 54 |
-
except Exception as e:
|
| 55 |
-
print(f"推理时发生错误: {type(e).__name__} - {e}")
|
| 56 |
-
yield f"抱歉,推理时遇到错误: {type(e).__name__} - {str(e)}"
|
| 57 |
|
| 58 |
|
| 59 |
# 其他 Gradio 界面代码不变
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# --- 直接加载模型和分词器 ---
|
| 6 |
model_id = "xingyu1996/tiger-gpt2"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("gpt2") # 使用原始的 GPT-2 分词器
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
|
|
| 14 |
temperature,
|
| 15 |
top_p,
|
| 16 |
):
|
| 17 |
+
# 将输入文本转换为 token ID
|
| 18 |
+
input_ids = tokenizer.encode(message, return_tensors="pt")
|
| 19 |
+
|
| 20 |
+
# 准备生成参数
|
| 21 |
+
gen_kwargs = {
|
| 22 |
+
"max_length": input_ids.shape[1] + max_tokens,
|
| 23 |
+
"do_sample": True if temperature > 0 else False,
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
+
|
| 26 |
+
if temperature > 0:
|
| 27 |
+
gen_kwargs["temperature"] = temperature
|
| 28 |
+
if top_p < 1.0:
|
| 29 |
+
gen_kwargs["top_p"] = top_p
|
| 30 |
+
|
| 31 |
+
# 生成文本
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
output_ids = model.generate(input_ids, **gen_kwargs)
|
| 34 |
+
|
| 35 |
+
# 只保留新生成的部分
|
| 36 |
+
new_tokens = output_ids[0, input_ids.shape[1]:]
|
| 37 |
+
|
| 38 |
+
# 解码生成的 token ID
|
| 39 |
+
response = tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 40 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
# 其他 Gradio 界面代码不变
|