Rename app to app.py
Browse files
app
DELETED
File without changes
|
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from onnxruntime_genai import ChatSession, GenerationConfig, ORTModel
|
3 |
+
|
4 |
+
# Path to the downloaded ONNX model
|
5 |
+
MODEL_DIR = "model/cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4"
|
6 |
+
|
7 |
+
# Load the model
|
8 |
+
model = ORTModel(MODEL_DIR, execution_provider="cpu")
|
9 |
+
session = ChatSession(model)
|
10 |
+
|
11 |
+
# Chat function
|
12 |
+
def chat_with_phi4(message, history):
|
13 |
+
history = history or []
|
14 |
+
for past_msg in history:
|
15 |
+
session.history.append((past_msg[0], past_msg[1]))
|
16 |
+
|
17 |
+
reply = session.chat(message, config=GenerationConfig(max_new_tokens=300))
|
18 |
+
history.append((message, reply))
|
19 |
+
return history, history
|
20 |
+
|
21 |
+
# Hugging Face logo
|
22 |
+
HF_LOGO = "https://huggingface.co/front/assets/huggingface_logo-noborder.svg"
|
23 |
+
|
24 |
+
# Gradio interface
|
25 |
+
with gr.Blocks() as demo:
|
26 |
+
gr.Image(value=HF_LOGO, width=120, show_label=False, show_download_button=False)
|
27 |
+
gr.Markdown("### Chat with Microsoft Phi-4 Mini Instruct (ONNX)")
|
28 |
+
|
29 |
+
chatbot = gr.Chatbot()
|
30 |
+
user_input = gr.Textbox(label="Your message")
|
31 |
+
state = gr.State([])
|
32 |
+
|
33 |
+
send_btn = gr.Button("Send")
|
34 |
+
clear_btn = gr.Button("Clear")
|
35 |
+
|
36 |
+
send_btn.click(chat_with_phi4, [user_input, state], [chatbot, state])
|
37 |
+
user_input.submit(chat_with_phi4, [user_input, state], [chatbot, state])
|
38 |
+
clear_btn.click(lambda: ([], []), outputs=[chatbot, state])
|
39 |
+
|
40 |
+
demo.launch()
|