tosei0000 commited on
Commit
b68b228
·
verified ·
1 Parent(s): 78f4d36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -78
app.py CHANGED
@@ -1,86 +1,23 @@
1
- import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
- # 模型路径(本地或 huggingface repo)
6
- model_path = "tosei0000/code-AI" # ← 修改为你的模型目录或 huggingface repo_id
7
 
8
- # 加载 tokenizer 和模型
9
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
10
- model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, torch_dtype=torch.float32)
11
- model.eval()
12
 
13
- # 系统人格设定,可修改为你喜欢的角色
14
- SYSTEM_PROMPT = (
15
- "你是一个善良、聪明、幽默的 AI 编程助手,能写各种 Python/HTML/JavaScript 代码,"
16
- "也能像朋友一样聊天。请保持耐心、有趣,尽可能详细地回答问题。\n"
17
- )
18
 
19
- # 对话历史记录(最多保存 5 轮)
20
- chat_history = []
 
 
 
21
 
22
- # 生成回复函数
23
- def generate_reply(user_input, chat_mode, max_tokens=512, temperature=0.7):
24
- global chat_history
25
-
26
- # 更新历史上下文
27
- chat_history.append(f"用户: {user_input}")
28
- if len(chat_history) > 5:
29
- chat_history = chat_history[-5:] # 只保留最近 5 条
30
-
31
- # 拼接 prompt
32
- if chat_mode == "代码生成":
33
- prompt = f"{SYSTEM_PROMPT}\n请根据以下需求生成代码:\n{user_input}\n"
34
- else: # 聊天模式
35
- prompt = SYSTEM_PROMPT + "\n" + "\n".join(chat_history) + "\n助手:"
36
-
37
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=4096)
38
- with torch.no_grad():
39
- outputs = model.generate(
40
- **inputs,
41
- max_new_tokens=max_tokens,
42
- temperature=temperature,
43
- top_p=0.95,
44
- do_sample=True,
45
- eos_token_id=tokenizer.eos_token_id,
46
- )
47
-
48
- decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
49
-
50
- # 提取最后一句助手的回复
51
- reply = decoded_output.split("助手:")[-1].strip()
52
-
53
- # 保存助手回复到历史中
54
- chat_history.append(f"助手: {reply}")
55
-
56
- return reply
57
-
58
- # 重置历史按钮
59
- def reset_memory():
60
- global chat_history
61
- chat_history = []
62
- return "记忆已重置。"
63
-
64
- # Gradio 界面
65
- with gr.Blocks() as demo:
66
- gr.Markdown("# 🤖 智能代码助理 + 聊天机器人")
67
- gr.Markdown("支持代码生成与聊天功能,可记忆上下文,具备人格设定!")
68
-
69
- with gr.Row():
70
- chat_mode = gr.Radio(["聊天", "代码生成"], value="代码生成", label="对话模式")
71
- reset_btn = gr.Button("🧹 重置记忆")
72
-
73
- user_input = gr.Textbox(label="你的输入", lines=6, placeholder="输入代码需求或聊天内容...")
74
- max_tokens = gr.Slider(50, 1024, value=512, step=10, label="最大生成长度")
75
- temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="生成多样性(temperature)")
76
-
77
- output = gr.Textbox(label="AI 回复", lines=10)
78
-
79
- submit_btn = gr.Button("✨ 生成")
80
-
81
- submit_btn.click(fn=generate_reply, inputs=[user_input, chat_mode, max_tokens, temperature], outputs=output)
82
- reset_btn.click(fn=reset_memory, outputs=output)
83
-
84
- # 启动服务
85
- if __name__ == "__main__":
86
- demo.launch()
 
 
1
  from transformers import AutoTokenizer, AutoModelForCausalLM
2
  import torch
3
 
4
+ # 设置模型路径(替换为你上传后显示的路径名)
5
+ model_path = "tosei0000/code-AI" # 修改为你上传的文件夹名
6
 
7
+ # 加载模型和分词器
8
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
9
+ model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
 
10
 
11
+ # 如果你有 GPU(Kaggle 支持 GPU),把模型放到 GPU
12
+ device = "cuda" if torch.cuda.is_available() else "cpu"
13
+ model.to(device)
 
 
14
 
15
+ # 推理函数
16
+ def chat(prompt, max_new_tokens=100):
17
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
18
+ outputs = model.generate(**inputs, max_new_tokens=max_new_tokens)
19
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
20
 
21
+ # 测试
22
+ response = chat("你好,请介绍一下你自己。")
23
+ print(response)