Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,70 @@
|
|
| 1 |
-
import huggingface_hub as hf_hub
|
| 2 |
-
import time
|
| 3 |
-
import openvino_genai as ov_genai
|
| 4 |
-
import numpy as np
|
| 5 |
import gradio as gr
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
model_name_to_full_id = {model_id.split("/")[-1]: model_id for model_id in model_ids} # Create Dictionary
|
| 19 |
-
|
| 20 |
-
def download_model(model_id):
|
| 21 |
-
model_path = model_id.split("/")[-1] # Extract model name
|
| 22 |
-
try:
|
| 23 |
hf_hub.snapshot_download(model_id, local_dir=model_path, local_dir_use_symlinks=False)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
tokenizer = pipe.get_tokenizer()
|
| 68 |
-
tokenizer.set_chat_template(tokenizer.chat_template) # 確保 chat template 已設定
|
| 69 |
-
print(f"Model {model_name} loaded successfully.")
|
| 70 |
-
return True
|
| 71 |
-
except Exception as e:
|
| 72 |
-
print(f"Error loading model {model_name}: {e}")
|
| 73 |
-
return False
|
| 74 |
-
|
| 75 |
-
# 產生回應的函數
|
| 76 |
-
def generate_response(prompt, model_name):
|
| 77 |
-
global pipe, tokenizer, accumulated_text
|
| 78 |
-
|
| 79 |
-
# 如果模型尚未載入,或需要切換模型,則載入模型
|
| 80 |
-
if pipe is None or pipe.model_name != model_name:
|
| 81 |
-
if not load_model(model_name):
|
| 82 |
-
return "模型載入失敗", "模型載入失敗", "模型載入失敗"
|
| 83 |
-
|
| 84 |
-
accumulated_text = "" #重置累積文字
|
| 85 |
-
|
| 86 |
-
try:
|
| 87 |
-
generated = pipe.generate(prompt, streamer=streamer, max_new_tokens=100)
|
| 88 |
-
tokenpersec = f'{generated.perf_metrics.get_throughput().mean:.2f}'
|
| 89 |
-
return tokenpersec, accumulated_text
|
| 90 |
-
except Exception as e:
|
| 91 |
-
error_message = f"生成回應時發生錯誤:{e}"
|
| 92 |
-
print(error_message)
|
| 93 |
-
return "發生錯誤", "發生錯誤", error_message
|
| 94 |
|
| 95 |
with gr.Blocks() as demo:
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
def process_input(prompt, model_name):
|
| 104 |
-
tokens_sec, response = generate_response(prompt, model_name)
|
| 105 |
-
return tokens_sec, response
|
| 106 |
-
|
| 107 |
-
prompt_textbox.submit(
|
| 108 |
-
fn=process_input,
|
| 109 |
-
inputs=[prompt_textbox, model_dropdown],
|
| 110 |
-
outputs=[token_per_sec_textbox, markdown_component]
|
| 111 |
)
|
|
|
|
| 112 |
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openvino_genai as ov_genai
|
| 3 |
+
import huggingface_hub as hf_hub
|
| 4 |
+
|
| 5 |
+
# OpenVINO Setup
|
| 6 |
+
model_id = "OpenVINO/Qwen3-0.6B-int4-ov" # Or your chosen model
|
| 7 |
+
model_path = "Qwen3-0.6B-int4-ov" # Local directory for the model
|
| 8 |
+
|
| 9 |
+
# Download the model if it doesn't exist locally
|
| 10 |
+
try:
|
| 11 |
+
# Check if the model directory exists. A quick and dirty check. Adjust as needed.
|
| 12 |
+
import os
|
| 13 |
+
if not os.path.exists(model_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
hf_hub.snapshot_download(model_id, local_dir=model_path, local_dir_use_symlinks=False)
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error downloading model: {e}")
|
| 17 |
+
print("Please ensure you have huggingface_hub installed and are authenticated if required.")
|
| 18 |
+
exit() # Or handle the error more gracefully
|
| 19 |
+
|
| 20 |
+
pipe = ov_genai.LLMPipeline(model_path, "CPU")
|
| 21 |
+
tokenizer = pipe.get_tokenizer()
|
| 22 |
+
tokenizer.set_chat_template(tokenizer.chat_template)
|
| 23 |
+
pipe.start_chat() # moved pipe.start_chat() here to run after pipeline intialization
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Gradio Chatbot UI
|
| 27 |
+
def user(user_message, history: list):
|
| 28 |
+
return "", history + [{"role": "user", "content": user_message}]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def bot(history: list):
|
| 32 |
+
# Get the user's last message from the history
|
| 33 |
+
user_message = history[-1]["content"]
|
| 34 |
+
|
| 35 |
+
# Use OpenVINO to generate a response
|
| 36 |
+
full_response = "" # Store the complete response
|
| 37 |
+
|
| 38 |
+
def streamer(subword): # Local streamer function
|
| 39 |
+
nonlocal full_response # Allow modification of outer scope variable
|
| 40 |
+
full_response += subword # Accumulate the subword
|
| 41 |
+
history[-1]['content'] = full_response # Update chatbot content
|
| 42 |
+
yield history
|
| 43 |
+
return ov_genai.StreamingStatus.RUNNING
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Initialize the bot message in history
|
| 47 |
+
history.append({"role": "assistant", "content": ""})
|
| 48 |
+
|
| 49 |
+
# Generate the response using the streaming function
|
| 50 |
+
for updated_history in pipe.generate(user_message, streamer=streamer, max_new_tokens=100):
|
| 51 |
+
yield updated_history
|
| 52 |
+
|
| 53 |
+
# Alternatively, without the step-by-step updates, you can just do this:
|
| 54 |
+
# full_response = pipe.generate(user_message, max_new_tokens=100) # but this will skip the steaming
|
| 55 |
+
# history[-1]['content'] = full_response
|
| 56 |
+
# yield history
|
| 57 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
+
chatbot = gr.Chatbot(type="messages")
|
| 61 |
+
msg = gr.Textbox()
|
| 62 |
+
clear = gr.Button("Clear")
|
| 63 |
+
|
| 64 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 65 |
+
bot, chatbot, chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 68 |
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.queue().launch()
|