File size: 1,174 Bytes
c8446f9
04462aa
c8446f9
04462aa
c8446f9
84c2214
04462aa
 
 
84c2214
04462aa
c8446f9
84c2214
04462aa
 
 
 
 
 
 
 
c8446f9
 
84c2214
04462aa
 
 
 
 
84c2214
 
04462aa
84c2214
04462aa
 
 
 
 
84c2214
04462aa
c8446f9
04462aa
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

model_id = "microsoft/phi-3-mini-4k-instruct"

# Load tokenizer and model without device_map
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    trust_remote_code=True
)

# Define text generation pipeline
pipe = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    max_new_tokens=512,
    temperature=0.7,
    do_sample=True,
    top_p=0.9,
)

# Format input for the Phi-3 instruction format
def generate_response(prompt):
    messages = [
        {"role": "user", "content": prompt}
    ]
    prompt_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    output = pipe(prompt_text)[0]["generated_text"]
    return output[len(prompt_text):]

# Gradio UI
demo = gr.Interface(
    fn=generate_response,
    inputs=gr.Textbox(label="Your Prompt", placeholder="Ask something..."),
    outputs=gr.Textbox(label="Phi-3 Mini Response"),
    title="🧠 Phi-3 Mini (Microsoft)",
    description="Running Phi-3 Mini in Hugging Face CPU Space."
)

demo.launch()