Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +42 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import transformers
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load the Phi 2 model and tokenizer
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
8 |
+
"microsoft/phi-2",
|
9 |
+
trust_remote_code=True
|
10 |
+
)
|
11 |
+
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
13 |
+
# "kroonen/phi-2-GGUF",
|
14 |
+
"microsoft/phi-2",
|
15 |
+
device_map="auto",
|
16 |
+
trust_remote_code=True,
|
17 |
+
torch_dtype=torch.float32
|
18 |
+
)
|
19 |
+
|
20 |
+
# Streamlit UI
|
21 |
+
st.title("Microsoft Phi 2 Streamlit App")
|
22 |
+
|
23 |
+
# User input prompt
|
24 |
+
prompt = st.text_area("Enter your prompt:", """Write a short summary about how to create a healthy lifestyle.""")
|
25 |
+
|
26 |
+
# Generate output based on user input
|
27 |
+
if st.button("Generate Output"):
|
28 |
+
with torch.no_grad():
|
29 |
+
token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt",
|
30 |
+
return_attention_mask=False
|
31 |
+
)
|
32 |
+
output_ids = model.generate(
|
33 |
+
token_ids.to(model.device),
|
34 |
+
# max_new_tokens=512,
|
35 |
+
do_sample=True,
|
36 |
+
temperature=0.3,
|
37 |
+
max_length=200
|
38 |
+
)
|
39 |
+
|
40 |
+
output = tokenizer.decode(output_ids[0][token_ids.size(1):])
|
41 |
+
st.text("Generated Output:")
|
42 |
+
st.write(output)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
sentencepiece
|
3 |
+
accelerate
|
4 |
+
bitsandbytes
|
5 |
+
einops
|
6 |
+
streamlit
|