Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,67 @@
|
|
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 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
def
|
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 |
|
68 |
if __name__ == "__main__":
|
69 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
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 |
+
hf_hub.snapshot_download(model_id, local_dir=model_path, local_dir_use_symlinks=False)
|
11 |
+
|
12 |
+
|
13 |
+
pipe = ov_genai.LLMPipeline(model_path, "CPU")
|
14 |
+
tokenizer = pipe.get_tokenizer()
|
15 |
+
tokenizer.set_chat_template(tokenizer.chat_template)
|
16 |
+
pipe.start_chat() # moved pipe.start_chat() here to run after pipeline intialization
|
17 |
+
|
18 |
+
|
19 |
+
# Gradio Chatbot UI
|
20 |
+
def user(user_message, history: list):
|
21 |
+
return "", history + [{"role": "user", "content": user_message}]
|
22 |
+
|
23 |
+
|
24 |
+
def bot(history: list, user_message):
|
25 |
+
# Use OpenVINO to generate a response
|
26 |
+
full_response = "" # Store the complete response
|
27 |
+
|
28 |
+
def streamer(subword): # Local streamer function
|
29 |
+
nonlocal full_response # Allow modification of outer scope variable
|
30 |
+
full_response += subword # Accumulate the subword
|
31 |
+
history[-1]['content'] = full_response # Update chatbot content
|
32 |
+
yield history
|
33 |
+
return ov_genai.StreamingStatus.RUNNING
|
34 |
+
|
35 |
+
|
36 |
+
# Initialize the bot message in history
|
37 |
+
history.append({"role": "assistant", "content": ""})
|
38 |
+
|
39 |
+
# Generate the response using the streaming function
|
40 |
+
for updated_history in pipe.generate(user_message, streamer=streamer, max_new_tokens=100):
|
41 |
+
yield updated_history
|
42 |
+
|
43 |
+
# Alternatively, without the step-by-step updates, you can just do this:
|
44 |
+
# full_response = pipe.generate(user_message, max_new_tokens=100) # but this will skip the steaming
|
45 |
+
# history[-1]['content'] = full_response
|
46 |
+
# yield history
|
47 |
+
|
48 |
+
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
chatbot = gr.Chatbot(type="messages")
|
51 |
+
msg = gr.Textbox()
|
52 |
+
submit_button = gr.Button("Submit") # Added submit button
|
53 |
+
clear = gr.Button("Clear")
|
54 |
+
|
55 |
+
def respond(message, chat_history): # Combined user and bot functions
|
56 |
+
user_message, chat_history = user(message, chat_history)
|
57 |
+
for bot_response in bot(chat_history, message):
|
58 |
+
chat_history = bot_response
|
59 |
+
yield "", chat_history
|
60 |
+
|
61 |
+
|
62 |
+
submit_button.click(respond, [msg, chatbot], [msg, chatbot])
|
63 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot]) # Optional: allow Enter key submission
|
64 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
+
demo.queue().launch()
|