Update app.py
Browse files
app.py
CHANGED
@@ -3,29 +3,28 @@ from huggingface_hub import InferenceClient
|
|
3 |
from optimum.intel import OVModelForCausalLM
|
4 |
from transformers import AutoTokenizer, pipeline
|
5 |
|
6 |
-
#
|
7 |
model_id = "hsuwill000/Qwen2.5-1.5B-Instruct-openvino-8bit"
|
8 |
model = OVModelForCausalLM.from_pretrained(model_id, device_map="auto")
|
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]["
|
17 |
-
|
18 |
-
# 獲取模型的回應
|
19 |
response = pipe(input_text, max_length=512, truncation=True, num_return_sequences=1)
|
20 |
reply = response[0]['generated_text']
|
21 |
|
22 |
-
#
|
23 |
print(f"Message: {message}")
|
24 |
print(f"Reply: {reply}")
|
25 |
-
return reply
|
26 |
|
27 |
-
#
|
28 |
-
demo = gr.ChatInterface(fn=respond, title="Qwen2.5-3B-Instruct-openvino", description="Qwen2.5-3B-Instruct-openvino", type='
|
29 |
|
30 |
if __name__ == "__main__":
|
31 |
demo.launch()
|
|
|
3 |
from optimum.intel import OVModelForCausalLM
|
4 |
from transformers import AutoTokenizer, pipeline
|
5 |
|
6 |
+
# Load the model and tokenizer
|
7 |
model_id = "hsuwill000/Qwen2.5-1.5B-Instruct-openvino-8bit"
|
8 |
model = OVModelForCausalLM.from_pretrained(model_id, device_map="auto")
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
10 |
|
11 |
+
# Create generation pipeline
|
12 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
13 |
|
14 |
def respond(message, history):
|
15 |
+
# Combine current message with previous history
|
16 |
+
input_text = message if not history else history[-1]["value"] + " " + message
|
17 |
+
# Get model's response
|
|
|
18 |
response = pipe(input_text, max_length=512, truncation=True, num_return_sequences=1)
|
19 |
reply = response[0]['generated_text']
|
20 |
|
21 |
+
# Return new message format
|
22 |
print(f"Message: {message}")
|
23 |
print(f"Reply: {reply}")
|
24 |
+
return [{"role": "bot", "value": reply}]
|
25 |
|
26 |
+
# Set up Gradio chat interface
|
27 |
+
demo = gr.ChatInterface(fn=respond, title="Qwen2.5-3B-Instruct-openvino", description="Qwen2.5-3B-Instruct-openvino", type='chatbot')
|
28 |
|
29 |
if __name__ == "__main__":
|
30 |
demo.launch()
|