shrimantasatpati commited on
Commit
f78469f
1 Parent(s): 2bfc948

Uploaded files

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("Microsoft Phi 2 Streamlit App")
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
+ with torch.no_grad():
26
+ token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
27
+ output_ids = model.generate(
28
+ token_ids.to(model.device),
29
+ max_new_tokens=512,
30
+ do_sample=True,
31
+ temperature=0.3
32
+ )
33
+
34
+ output = tokenizer.decode(output_ids[0][token_ids.size(1):])
35
+ st.text("Generated Output:")
36
+ st.write(output)