File size: 1,230 Bytes
f78469f
 
 
0b252a0
0901572
f78469f
 
 
 
 
 
 
 
c4be01c
3b4796e
0b252a0
f78469f
 
 
 
 
 
0b252a0
f78469f
 
 
 
c4be01c
92df928
c4be01c
f78469f
 
92df928
f78469f
0b252a0
92df928
f78469f
 
 
 
 
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
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# torch.set_default_device("cuda")
# Load the Phi 2 model and tokenizer
tokenizer = AutoTokenizer.from_pretrained(
    "microsoft/phi-2",
    trust_remote_code=True
)

model = AutoModelForCausalLM.from_pretrained(
    "microsoft/phi-2",
    device_map="cpu",
    trust_remote_code=True,
    torch_dtype=torch.float32
)

# Streamlit UI
st.title("Microsoft Phi 2 Streamlit App")

# User input prompt
prompt = st.text_area("Enter your prompt:", """Write a short summary about how to create a healthy lifestyle.""")

# Generate output based on user input
if st.button("Generate Output"):
    with torch.no_grad():
        token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt",  
                                     return_attention_mask=False
                                    )
        output_ids = model.generate(
            token_ids.to(model.device),
            # max_new_tokens=512,
            do_sample=True,
            temperature=0.3,
            max_length=200
        )

    output = tokenizer.decode(output_ids[0][token_ids.size(1):])
    st.text("Generated Output:")
    st.write(output)