Spaces:
Sleeping
Sleeping
File size: 1,769 Bytes
fb38431 c778ae5 5315eed c778ae5 4d07925 c778ae5 b461977 839fca3 b461977 c778ae5 5315eed 4d07925 5315eed b461977 4d07925 b461977 839fca3 ff04433 839fca3 ff04433 839fca3 ff04433 839fca3 4d07925 839fca3 4d07925 839fca3 b461977 5315eed b461977 |
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr
if torch.cuda.is_available():
torch.set_default_device("cuda")
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
"microsoft/phi-2",
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
trust_remote_code=True,
)
def generate(prompt, length):
inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=False)
input_token_len = len(inputs.tokens())
outputs = model.generate(**inputs, max_length=length if length >= input_token_len else input_token_len
return tokenizer.batch_decode(outputs)[0]
demo = gr.Interface(
fn=generate,
inputs=[
gr.Text(
label="prompt",
value="Write a detailed analogy between mathematics and a lighthouse.",
),
gr.Number(value=100, label="max length", maximum=500),
],
outputs="text",
examples=[
[
"Write a detailed analogy between mathematics and a lighthouse.",
75,
],
[
"Instruct: Write a detailed analogy between mathematics and a lighthouse.\nOutput:",
75,
],
[
"Alice: I don't know why, I'm struggling to maintain focus while studying. Any suggestions?\n\nBob: ",
150,
],
[
'''def print_prime(n):
"""
Print all primes between 1 and n
"""\n''',
100,
],
],
title="Microsoft Phi-2",
description="Unofficial demo of Microsoft Phi-2, a high performing model with only 2.7B parameters.",
)
if __name__ == "__main__":
demo.launch(show_api=False)
|