Erik
Update app.py
2a58266 verified
import gradio as gr
from transformers import pipeline
# Model options dict
MODEL_OPTIONS = {
"Phi-4 Mini Instruct (recommended)": "microsoft/Phi-4-mini-instruct",
"Phi-3 Mini Instruct": "microsoft/Phi-3-mini-instruct"
}
model_pipelines = {}
PROMPT_SUGGESTIONS = [
"Explain quantum computing in simple terms.",
"Write a friendly email asking for feedback.",
"Give me a creative short story idea.",
"Summarize the benefits of meditation."
]
def get_model(model_key):
if model_key not in model_pipelines:
model_pipelines[model_key] = pipeline(
"text-generation",
model=MODEL_OPTIONS[model_key]
)
return model_pipelines[model_key]
def chat_fn(message, history, system_prompt, temperature, chosen_model):
prompt = f"SYSTEM: {system_prompt}\n"
for u, a in history:
prompt += f"User: {u}\nAI: {a}\n"
prompt += f"User: {message}\nAI:"
generator = get_model(chosen_model)
out = generator(
prompt,
max_new_tokens=150,
do_sample=True,
temperature=float(temperature)
)[0]["generated_text"]
reply = out.split("AI:")[-1].strip()
history.append((message, reply))
return history, history
with gr.Blocks() as demo:
gr.Markdown("# Microsoft’s Phi Playground Lab Chat Interface")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("**Prompt Suggestions**")
for s in PROMPT_SUGGESTIONS:
btn = gr.Button(s)
# use js= to run client-side JS that updates the textbox
btn.click(
None,
[],
[],
js=f"() => document.querySelector('textarea').value = `{s}`"
)
with gr.Column(scale=3):
sys_prompt = gr.Textbox(
label="System Prompt",
value="You are a helpful, concise AI assistant optimized for clarity."
)
model_selector = gr.Dropdown(
choices=list(MODEL_OPTIONS.keys()),
value="Phi-4 Mini Instruct (recommended)",
label="Select Model"
)
temp_slider = gr.Slider(
minimum=0.1,
maximum=1.2,
step=0.1,
value=0.7,
label="Temperature"
)
chat_interface = gr.ChatInterface(
fn=chat_fn,
additional_inputs=[sys_prompt, temp_slider, model_selector],
placeholder="Ask anything..."
)
demo.launch()