HelloSun commited on
Commit
3b22ee7
·
verified ·
1 Parent(s): bf1f14f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -21
app.py CHANGED
@@ -1,35 +1,25 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
  from optimum.intel import OVModelForCausalLM
8
  from transformers import AutoTokenizer, pipeline
9
 
 
10
  model_id = "HelloSun/Qwen2.5-0.5B-Instruct-openvino"
11
  model = OVModelForCausalLM.from_pretrained(model_id)
12
-
13
  tokenizer = AutoTokenizer.from_pretrained(model_id)
14
- pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
15
-
16
-
17
- def respond(
18
- message,
19
- history,
20
- ):
21
- print(message,history)
22
- return pipe(message)
23
-
24
 
 
 
25
 
26
- """
27
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
28
- """
29
- demo = gr.ChatInterface(
30
- respond,
31
- )
32
 
 
 
33
 
34
  if __name__ == "__main__":
35
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
3
  from optimum.intel import OVModelForCausalLM
4
  from transformers import AutoTokenizer, pipeline
5
 
6
+ # 載入模型和標記器
7
  model_id = "HelloSun/Qwen2.5-0.5B-Instruct-openvino"
8
  model = OVModelForCausalLM.from_pretrained(model_id)
 
9
  tokenizer = AutoTokenizer.from_pretrained(model_id)
 
 
 
 
 
 
 
 
 
 
10
 
11
+ # 建立生成管道
12
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
13
 
14
+ def respond(message, history):
15
+ # 將當前訊息與歷史訊息合併
16
+ input_text = message if not history else history[-1][1] + " " + message
17
+ # 獲取模型的回應
18
+ response = pipe(input_text, max_length=100, num_return_sequences=1)
19
+ return response[0]['generated_text'], history + [(message, response[0]['generated_text'])]
20
 
21
+ # 設定 Gradio 的聊天界面
22
+ demo = gr.ChatInterface(fn=respond)
23
 
24
  if __name__ == "__main__":
25
  demo.launch()