Spaces:
Sleeping
Sleeping
File size: 1,934 Bytes
f74f344 2b29a6a cab7f8a 1549b17 cab7f8a 1549b17 cab7f8a 6addef4 16ebb52 e70bcc1 cab7f8a b95e062 7b0bd94 b95e062 cab7f8a f74f344 dc0e6de f74f344 daf4465 6addef4 16ebb52 dc0e6de fab7f6a f74f344 09f59b6 6addef4 cab7f8a 4c41d4f daf4465 6addef4 daf4465 16ebb52 6addef4 1549b17 daf4465 f74f344 be61c57 | 1 2 3 4 5 6 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
# ✅ Safe import of the GPU decorator
try:
from spaces import GPU
except ImportError:
def GPU(func): return func # Fallback if not in a HF Space
# ✅ Load Phi-3 Mini model
model_id = "microsoft/phi-3-mini-4k-instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id, torch_dtype="auto", device_map="auto"
)
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
# ✅ Apply GPU decorator to ensure ZeroGPU allocates GPU
@GPU
def chat_fn(message, history):
history_text = ""
for item in history:
if item["role"] == "user":
history_text += f"<|user|>\n{item['content']}\n"
elif item["role"] == "assistant":
history_text += f"<|assistant|>\n{item['content']}\n"
prompt = f"{history_text}<|user|>\n{message}\n<|assistant|>\n"
result = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)[0]['generated_text']
reply = result.split("<|assistant|>")[-1].strip()
# ✅ Auto-format Python or general code
keywords = ["def ", "class ", "import ", "function ", "console.log", "public static void"]
if "```" not in reply and any(k in reply for k in keywords):
reply = f"```\n{reply.strip()}\n```" # Wrap in Markdown code block
return reply
# ✅ Gradio UI
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("## 💬 Chat with Phi-3 Mini")
gr.Markdown("Welcome to your AI Assistant powered by Phi-3 Mini. Ask me anything or request code examples!")
gr.ChatInterface(
fn=chat_fn,
title="",
examples=[
"What is Python?",
"Write a JavaScript function to reverse a string.",
"Explain how transformers work.",
],
chatbot=gr.Chatbot(type="messages")
)
demo.launch()
|