Spaces:
Sleeping
Sleeping
Commit ·
dc0e6de
1
Parent(s): daf4465
update commit with phi-3 mini 3
Browse files
app.py
CHANGED
|
@@ -5,44 +5,45 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
| 5 |
model_id = "microsoft/phi-3-mini-4k-instruct"
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 7 |
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
-
model_id,
|
| 9 |
-
torch_dtype="auto",
|
| 10 |
-
device_map="auto" # Will default to CPU if no GPU available
|
| 11 |
)
|
| 12 |
|
| 13 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
def chat_fn(message, history):
|
| 17 |
history_text = ""
|
| 18 |
-
for
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
prompt = f"{history_text}<|user|>\n{message}\n<|assistant|>\n"
|
| 21 |
-
response = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)[0]['generated_text']
|
| 22 |
-
reply = response.split("<|assistant|>")[-1].strip()
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
reply = f"```\n{reply}\n```"
|
| 27 |
|
| 28 |
return reply
|
| 29 |
|
| 30 |
-
# Launch the app with basic Blocks + authentication
|
| 31 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 32 |
gr.Markdown("## 💬 Chat with Phi-3 Mini")
|
| 33 |
gr.Markdown(
|
| 34 |
-
"Welcome
|
| 35 |
-
"Ask any question or request code examples. Use your credentials to access this demo."
|
| 36 |
)
|
| 37 |
|
| 38 |
gr.ChatInterface(
|
| 39 |
fn=chat_fn,
|
|
|
|
| 40 |
examples=[
|
| 41 |
-
"What is
|
| 42 |
-
"Write a
|
| 43 |
-
"Explain
|
| 44 |
],
|
| 45 |
-
|
| 46 |
)
|
| 47 |
|
| 48 |
-
demo.launch(
|
|
|
|
| 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 |
+
# OpenAI-style messages (new format)
|
| 14 |
def chat_fn(message, history):
|
| 15 |
history_text = ""
|
| 16 |
+
for item in history:
|
| 17 |
+
if item["role"] == "user":
|
| 18 |
+
history_text += f"<|user|>\n{item['content']}\n"
|
| 19 |
+
elif item["role"] == "assistant":
|
| 20 |
+
history_text += f"<|assistant|>\n{item['content']}\n"
|
| 21 |
prompt = f"{history_text}<|user|>\n{message}\n<|assistant|>\n"
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
result = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)[0]['generated_text']
|
| 24 |
+
reply = result.split("<|assistant|>")[-1].strip()
|
| 25 |
+
|
| 26 |
+
# Format code blocks
|
| 27 |
+
if "```" not in reply and any(word in reply for word in ["def ", "class ", "import "]):
|
| 28 |
reply = f"```\n{reply}\n```"
|
| 29 |
|
| 30 |
return reply
|
| 31 |
|
|
|
|
| 32 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 33 |
gr.Markdown("## 💬 Chat with Phi-3 Mini")
|
| 34 |
gr.Markdown(
|
| 35 |
+
"Welcome to your AI Assistant powered by Phi-3 Mini. Ask me anything or request code examples!"
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
gr.ChatInterface(
|
| 39 |
fn=chat_fn,
|
| 40 |
+
title="",
|
| 41 |
examples=[
|
| 42 |
+
"What is Python?",
|
| 43 |
+
"Write a JavaScript function to reverse a string.",
|
| 44 |
+
"Explain how transformers work.",
|
| 45 |
],
|
| 46 |
+
chatbot=gr.Chatbot(type="messages") # fixes the deprecated tuples warning
|
| 47 |
)
|
| 48 |
|
| 49 |
+
demo.launch()
|