Spaces:
Sleeping
Sleeping
Commit ·
e70bcc1
1
Parent(s): f74f344
update commit with phi-3 mini
Browse files
app.py
CHANGED
|
@@ -4,12 +4,13 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
| 4 |
# Load Phi-3 Mini model
|
| 5 |
model_id = "microsoft/phi-3-mini-4k-instruct"
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 7 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Pipeline for text generation
|
| 10 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 11 |
|
| 12 |
-
# Chat
|
| 13 |
def chat_fn(message, history):
|
| 14 |
history_text = ""
|
| 15 |
for user, bot in history:
|
|
@@ -19,19 +20,18 @@ def chat_fn(message, history):
|
|
| 19 |
reply = response.split("<|assistant|>")[-1].strip()
|
| 20 |
return reply
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
clear_btn="🧹 Clear Chat"
|
| 35 |
-
)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
| 4 |
# Load Phi-3 Mini model
|
| 5 |
model_id = "microsoft/phi-3-mini-4k-instruct"
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
model_id, torch_dtype="auto", device_map="auto"
|
| 9 |
+
)
|
| 10 |
|
|
|
|
| 11 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 12 |
|
| 13 |
+
# Chat logic with history
|
| 14 |
def chat_fn(message, history):
|
| 15 |
history_text = ""
|
| 16 |
for user, bot in history:
|
|
|
|
| 20 |
reply = response.split("<|assistant|>")[-1].strip()
|
| 21 |
return reply
|
| 22 |
|
| 23 |
+
# Wrap with Blocks for custom layout and auth
|
| 24 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 25 |
+
gr.Markdown("## 💬 Chat with Phi-3 Mini")
|
| 26 |
+
gr.Markdown("Welcome! Lightweight, fast, and privacy-focused AI assistant powered by Microsoft's Phi-3. You can ask questions, request code, or chat naturally.")
|
| 27 |
+
|
| 28 |
+
gr.ChatInterface(fn=chat_fn,
|
| 29 |
+
examples=["What is a large language model?", "Write a Python function to reverse a list.", "Explain the concept of recursion."],
|
| 30 |
+
title="",
|
| 31 |
+
retry_btn="↻ Retry",
|
| 32 |
+
undo_btn="⤺ Undo",
|
| 33 |
+
clear_btn="🧹 Clear"
|
| 34 |
+
)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Add authentication here
|
| 37 |
+
demo.launch(auth=("user", "pass"))
|