Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
from pathlib import Path
|
| 3 |
import torch
|
| 4 |
-
from threading import Event, Thread
|
| 5 |
from transformers import AutoConfig, AutoTokenizer
|
| 6 |
from optimum.intel.openvino import OVModelForCausalLM
|
| 7 |
import openvino as ov
|
|
@@ -15,6 +14,66 @@ from llm_config import SUPPORTED_LLM_MODELS
|
|
| 15 |
# Initialize model language options
|
| 16 |
model_languages = list(SUPPORTED_LLM_MODELS)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Define Gradio interface within a Blocks context
|
| 19 |
with gr.Blocks() as iface:
|
| 20 |
# Dropdown for model language selection
|
|
@@ -31,12 +90,11 @@ with gr.Blocks() as iface:
|
|
| 31 |
value=None
|
| 32 |
)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
def update_model_id(model_language_value):
|
| 36 |
model_ids = list(SUPPORTED_LLM_MODELS[model_language_value])
|
| 37 |
return gr.Dropdown.update(value=model_ids[0], choices=model_ids)
|
| 38 |
|
| 39 |
-
# Update model_id choices when model_language changes
|
| 40 |
model_language.change(update_model_id, inputs=model_language, outputs=model_id)
|
| 41 |
|
| 42 |
# Checkbox for INT4 model preparation
|
|
@@ -59,42 +117,7 @@ with gr.Blocks() as iface:
|
|
| 59 |
label="Device"
|
| 60 |
)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
def get_model_path(model_language_value, model_id_value):
|
| 64 |
-
model_configuration = SUPPORTED_LLM_MODELS[model_language_value][model_id_value]
|
| 65 |
-
pt_model_name = model_id_value.split("-")[0]
|
| 66 |
-
int4_model_dir = Path(model_id_value) / "INT4_compressed_weights"
|
| 67 |
-
return model_configuration, int4_model_dir, pt_model_name
|
| 68 |
-
|
| 69 |
-
# Function to download the model if not already present
|
| 70 |
-
def download_model_if_needed(model_language_value, model_id_value):
|
| 71 |
-
model_configuration, int4_model_dir, pt_model_name = get_model_path(model_language_value, model_id_value)
|
| 72 |
-
int4_weights = int4_model_dir / "openvino_model.bin"
|
| 73 |
-
if not int4_weights.exists():
|
| 74 |
-
print(f"Downloading model {model_id_value}...")
|
| 75 |
-
# Download logic (e.g., requests.get(model_configuration["model_url"])) can go here
|
| 76 |
-
return int4_model_dir
|
| 77 |
-
|
| 78 |
-
# Load the model based on selected options
|
| 79 |
-
def load_model(model_language_value, model_id_value):
|
| 80 |
-
int4_model_dir = download_model_if_needed(model_language_value, model_id_value)
|
| 81 |
-
ov_config = {
|
| 82 |
-
hints.performance_mode(): hints.PerformanceMode.LATENCY,
|
| 83 |
-
streams.num(): "1",
|
| 84 |
-
props.cache_dir(): ""
|
| 85 |
-
}
|
| 86 |
-
core = ov.Core()
|
| 87 |
-
tok = AutoTokenizer.from_pretrained(int4_model_dir, trust_remote_code=True)
|
| 88 |
-
ov_model = OVModelForCausalLM.from_pretrained(
|
| 89 |
-
int4_model_dir,
|
| 90 |
-
device=device.value,
|
| 91 |
-
ov_config=ov_config,
|
| 92 |
-
config=AutoConfig.from_pretrained(int4_model_dir, trust_remote_code=True),
|
| 93 |
-
trust_remote_code=True
|
| 94 |
-
)
|
| 95 |
-
return tok, ov_model
|
| 96 |
-
|
| 97 |
-
# Gradio sliders for model generation parameters
|
| 98 |
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.7, label="Temperature")
|
| 99 |
top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.9, label="Top P")
|
| 100 |
top_k = gr.Slider(minimum=0, maximum=50, value=50, label="Top K")
|
|
@@ -103,41 +126,19 @@ with gr.Blocks() as iface:
|
|
| 103 |
# Conversation history state
|
| 104 |
history = gr.State([])
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
input_ids=input_ids,
|
| 118 |
-
max_new_tokens=256,
|
| 119 |
-
temperature=temperature,
|
| 120 |
-
top_p=top_p,
|
| 121 |
-
top_k=top_k,
|
| 122 |
-
repetition_penalty=repetition_penalty
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
# Stream response to textbox
|
| 126 |
-
response = ""
|
| 127 |
-
for new_text in ov_model.generate(**generate_kwargs):
|
| 128 |
-
response += new_text
|
| 129 |
-
history[-1][1] = response
|
| 130 |
-
yield history
|
| 131 |
-
|
| 132 |
-
# Set up the interface with inputs and outputs
|
| 133 |
-
iface = gr.Interface(
|
| 134 |
-
fn=generate_response,
|
| 135 |
-
inputs=[history, temperature, top_p, top_k, repetition_penalty, model_language, model_id],
|
| 136 |
-
outputs=[gr.Textbox(label="Conversation History"), history],
|
| 137 |
-
live=True,
|
| 138 |
-
title="OpenVINO Chatbot"
|
| 139 |
)
|
| 140 |
|
| 141 |
# Launch the Gradio app
|
| 142 |
if __name__ == "__main__":
|
| 143 |
-
iface.launch(debug=True,
|
|
|
|
| 1 |
import os
|
| 2 |
from pathlib import Path
|
| 3 |
import torch
|
|
|
|
| 4 |
from transformers import AutoConfig, AutoTokenizer
|
| 5 |
from optimum.intel.openvino import OVModelForCausalLM
|
| 6 |
import openvino as ov
|
|
|
|
| 14 |
# Initialize model language options
|
| 15 |
model_languages = list(SUPPORTED_LLM_MODELS)
|
| 16 |
|
| 17 |
+
# Helper function to retrieve model configuration and path
|
| 18 |
+
def get_model_path(model_language_value, model_id_value):
|
| 19 |
+
model_configuration = SUPPORTED_LLM_MODELS[model_language_value][model_id_value]
|
| 20 |
+
pt_model_name = model_id_value.split("-")[0]
|
| 21 |
+
int4_model_dir = Path(model_id_value) / "INT4_compressed_weights"
|
| 22 |
+
return model_configuration, int4_model_dir, pt_model_name
|
| 23 |
+
|
| 24 |
+
# Download the model if not already present
|
| 25 |
+
def download_model_if_needed(model_language_value, model_id_value):
|
| 26 |
+
model_configuration, int4_model_dir, pt_model_name = get_model_path(model_language_value, model_id_value)
|
| 27 |
+
int4_weights = int4_model_dir / "openvino_model.bin"
|
| 28 |
+
if not int4_weights.exists():
|
| 29 |
+
print(f"Downloading model {model_id_value}...")
|
| 30 |
+
# Download logic (e.g., requests.get(model_configuration["model_url"])) can go here
|
| 31 |
+
return int4_model_dir
|
| 32 |
+
|
| 33 |
+
# Load the model based on selected options
|
| 34 |
+
def load_model(model_language_value, model_id_value, device):
|
| 35 |
+
int4_model_dir = download_model_if_needed(model_language_value, model_id_value)
|
| 36 |
+
ov_config = {
|
| 37 |
+
hints.performance_mode(): hints.PerformanceMode.LATENCY,
|
| 38 |
+
streams.num(): "1",
|
| 39 |
+
props.cache_dir(): ""
|
| 40 |
+
}
|
| 41 |
+
core = ov.Core()
|
| 42 |
+
tok = AutoTokenizer.from_pretrained(int4_model_dir, trust_remote_code=True)
|
| 43 |
+
ov_model = OVModelForCausalLM.from_pretrained(
|
| 44 |
+
int4_model_dir,
|
| 45 |
+
device=device,
|
| 46 |
+
ov_config=ov_config,
|
| 47 |
+
config=AutoConfig.from_pretrained(int4_model_dir, trust_remote_code=True),
|
| 48 |
+
trust_remote_code=True
|
| 49 |
+
)
|
| 50 |
+
return tok, ov_model
|
| 51 |
+
|
| 52 |
+
# Define the function to generate responses
|
| 53 |
+
def generate_response(history, temperature, top_p, top_k, repetition_penalty, model_language_value, model_id_value, device):
|
| 54 |
+
tok, ov_model = load_model(model_language_value, model_id_value, device)
|
| 55 |
+
|
| 56 |
+
def convert_history_to_token(history):
|
| 57 |
+
input_tokens = tok(" ".join([msg[0] for msg in history]), return_tensors="pt").input_ids
|
| 58 |
+
return input_tokens
|
| 59 |
+
|
| 60 |
+
input_ids = convert_history_to_token(history)
|
| 61 |
+
generate_kwargs = dict(
|
| 62 |
+
input_ids=input_ids,
|
| 63 |
+
max_new_tokens=256,
|
| 64 |
+
temperature=temperature,
|
| 65 |
+
top_p=top_p,
|
| 66 |
+
top_k=top_k,
|
| 67 |
+
repetition_penalty=repetition_penalty
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Stream response to textbox
|
| 71 |
+
response = ""
|
| 72 |
+
for new_text in ov_model.generate(**generate_kwargs):
|
| 73 |
+
response += new_text
|
| 74 |
+
history[-1][1] = response
|
| 75 |
+
yield history
|
| 76 |
+
|
| 77 |
# Define Gradio interface within a Blocks context
|
| 78 |
with gr.Blocks() as iface:
|
| 79 |
# Dropdown for model language selection
|
|
|
|
| 90 |
value=None
|
| 91 |
)
|
| 92 |
|
| 93 |
+
# Update model_id choices when model_language changes
|
| 94 |
def update_model_id(model_language_value):
|
| 95 |
model_ids = list(SUPPORTED_LLM_MODELS[model_language_value])
|
| 96 |
return gr.Dropdown.update(value=model_ids[0], choices=model_ids)
|
| 97 |
|
|
|
|
| 98 |
model_language.change(update_model_id, inputs=model_language, outputs=model_id)
|
| 99 |
|
| 100 |
# Checkbox for INT4 model preparation
|
|
|
|
| 117 |
label="Device"
|
| 118 |
)
|
| 119 |
|
| 120 |
+
# Sliders for model generation parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.7, label="Temperature")
|
| 122 |
top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.9, label="Top P")
|
| 123 |
top_k = gr.Slider(minimum=0, maximum=50, value=50, label="Top K")
|
|
|
|
| 126 |
# Conversation history state
|
| 127 |
history = gr.State([])
|
| 128 |
|
| 129 |
+
# Textbox for conversation history
|
| 130 |
+
conversation_output = gr.Textbox(label="Conversation History")
|
| 131 |
+
|
| 132 |
+
# Button to trigger response generation
|
| 133 |
+
generate_button = gr.Button("Generate Response")
|
| 134 |
+
|
| 135 |
+
# Define action when button is clicked
|
| 136 |
+
generate_button.click(
|
| 137 |
+
generate_response,
|
| 138 |
+
inputs=[history, temperature, top_p, top_k, repetition_penalty, model_language, model_id, device],
|
| 139 |
+
outputs=[conversation_output, history]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
)
|
| 141 |
|
| 142 |
# Launch the Gradio app
|
| 143 |
if __name__ == "__main__":
|
| 144 |
+
iface.launch(debug=True, server_name="0.0.0.0", server_port=7860)
|