File size: 1,144 Bytes
af77804
 
 
 
9d27c6c
 
4a40fb3
9d27c6c
af77804
9d27c6c
 
af77804
9d27c6c
 
c925507
 
 
 
af77804
9d27c6c
0c95c30
af77804
9d27c6c
af77804
9d27c6c
0c95c30
af77804
0c95c30
af77804
9d27c6c
0c95c30
 
9d27c6c
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
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# Use GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

st.title("Text Generation with Hugging Face Transformers")

# Input prompt from user
prompt = st.text_area("Enter a prompt:", "this news is real pyresearch given right computer vision videos?")

# Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", torch_dtype="auto", trust_remote_code=True)

# Move the model to the desired device
model.to(device)

# Generate text on button click
if st.button("Generate"):
    with torch.no_grad():
        token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(device)
        output_ids = model.generate(
            token_ids,
            max_new_tokens=512,
            do_sample=True,
            temperature=0.1
        )

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