Spaces:
Sleeping
Sleeping
arieridwans
commited on
Commit
•
b168e68
1
Parent(s):
ad3979e
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,39 @@
|
|
1 |
import streamlit as st
|
2 |
-
import subprocess
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
import torch
|
5 |
-
import re
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
13 |
|
14 |
-
|
|
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
instruct_prompt = "Instruct:You are a song writer and your main reference is The Beatles. Write a song lyrics by completing these words:"
|
18 |
output_prompt = "Output:"
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
|
|
4 |
|
5 |
+
# Load the Phi 2 model and tokenizer
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
7 |
+
"microsoft/phi-2",
|
8 |
+
trust_remote_code=True
|
9 |
+
)
|
10 |
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
12 |
+
"microsoft/phi-2",
|
13 |
+
device_map="auto",
|
14 |
+
trust_remote_code=True
|
15 |
+
)
|
16 |
|
17 |
+
# Streamlit UI
|
18 |
+
st.title("Eleanor Rigby")
|
19 |
|
20 |
+
# User input prompt
|
21 |
+
prompt = st.text_area("Enter your prompt:", """Write a story about Nasa""")
|
22 |
+
|
23 |
+
# Generate output based on user input
|
24 |
+
if st.button("Generate Output"):
|
25 |
instruct_prompt = "Instruct:You are a song writer and your main reference is The Beatles. Write a song lyrics by completing these words:"
|
26 |
output_prompt = "Output:"
|
27 |
+
prompt = """ {0}{1}\n{2} """.format(instruct_prompt, user_prompt, output_prompt)
|
28 |
+
with torch.no_grad():
|
29 |
+
token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
|
30 |
+
output_ids = model.generate(
|
31 |
+
token_ids.to(model.device),
|
32 |
+
max_new_tokens=512,
|
33 |
+
do_sample=True,
|
34 |
+
temperature=0.3
|
35 |
+
)
|
36 |
+
|
37 |
+
output = tokenizer.decode(output_ids[0][token_ids.size(1):])
|
38 |
+
st.text("Generated Output:")
|
39 |
+
st.write(output)
|