Jenny991 commited on
Commit
f987894
·
1 Parent(s): 8e9dfe3

新增聊天機器人 app 與依賴

Browse files
Files changed (2) hide show
  1. app.py +27 -14
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,20 +1,33 @@
1
- from transformers import pipeline
2
  import gradio as gr
 
3
 
4
- generator = pipeline(
5
- "text-generation",
6
- model="Qwen/Qwen1.5-0.5B-Chat",
7
- trust_remote_code=True
8
- )
9
 
10
- chat_history = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- def chat(user_input):
13
- chat_history.append(f"使用者: {user_input}")
14
- prompt = "\n".join(chat_history[-6:]) + "\nAI:"
15
- response = generator(prompt, max_new_tokens=100)[0]["generated_text"]
16
- reply = response[len(prompt):].strip()
17
  chat_history.append(f"AI: {reply}")
18
- return reply
 
 
 
 
 
 
 
 
19
 
20
- gr.ChatInterface(chat).launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
3
 
4
+ model_id = "MediaTek-Research/Breeze-7B-Instruct-v1_0" # Breeze7 模型名稱(請確認 Huggingface 上正確名稱)
 
 
 
 
5
 
6
+ # 載入 tokenizer 和模型
7
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
8
+ model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
9
+
10
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer, trust_remote_code=True)
11
+
12
+ def chat_fn(user_input, chat_history):
13
+ chat_history = chat_history or []
14
+ chat_history.append(f"你: {user_input}")
15
+
16
+ # 取最近幾句聊天作為 prompt,避免太長
17
+ prompt = "\n".join(chat_history) + "\nAI:"
18
+
19
+ response = generator(prompt, max_new_tokens=100, temperature=0.7)
20
+ reply = response[0]['generated_text'][len(prompt):].strip()
21
 
 
 
 
 
 
22
  chat_history.append(f"AI: {reply}")
23
+ return reply, chat_history
24
+
25
+ with gr.Blocks() as demo:
26
+ chat_history = gr.State([])
27
+
28
+ chatbot = gr.Chatbot()
29
+ user_input = gr.Textbox(show_label=False, placeholder="說點什麼...")
30
+
31
+ user_input.submit(chat_fn, inputs=[user_input, chat_history], outputs=[chatbot, chat_history])
32
 
33
+ demo.launch()
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  transformers
2
- torch
 
 
1
  transformers
2
+ torch
3
+ gradio