Spaces:
Sleeping
Sleeping
File size: 1,765 Bytes
1f78700 0d6b69d d2f39a7 50116f8 847b0e8 1f78700 0d6b69d 50116f8 847b0e8 50116f8 1f78700 847b0e8 3853cf9 16816bc 847b0e8 1f78700 cba4fd1 0d6b69d 50116f8 0d6b69d 50116f8 1f78700 0d6b69d |
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 |
import gradio as gr
import torch
from transformers import pipeline, AutoTokenizer, AutoModel, LlamaForCausalLM
from peft import PeftModel
#pipe = pipeline("text-generation", model="furquan/opt_2_7_b_prompt_tuned_sentiment_analysis", trust_remote_code=True, cache_dir="/local/home/furquanh/myProjects/week12/").to('cuda')
# tokenizer = AutoTokenizer.from_pretrained("furquan/opt-1-3b-prompt-tuned-sentiment-analysis", trust_remote_code=True)
# model = AutoModel.from_pretrained("furquan/opt-1-3b-prompt-tuned-sentiment-analysis", trust_remote_code=True)
model = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf", token="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")
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
model = PeftModel.from_pretrained(model, "furquan/llama2-sentiment-prompt-tuned")
title = "OPT-1.3B"
description = "This demo uses meta's LLama-2-7b Causal LM as base model that was prompt tuned on the mteb/tweet_sentiment_extraction dataset to only output the sentiment of a given text."
article = "<p style='text-align: center'><a href='https://arxiv.org/pdf/2104.08691.pdf' target='_blank'>The Power of Scale for Parameter-Efficient Prompt Tuning</a></p>"
def sentiment(text):
if text[-1] != ' ':
text = f"{text} "
tokenized = tokenizer(text, return_tensors='pt')
with torch.no_grad():
outputs = model.generate(
input_ids=tokenized["input_ids"], attention_mask=tokenized["attention_mask"], max_new_tokens=1
)
return f"text: {text} Sentiment: {tokenizer.decode(outputs[0], skip_special_tokens=True).split(' ')[-1]}"
iface = gr.Interface(fn=sentiment, inputs="text", outputs="text", title=title,
description=description, article=article)
iface.launch()
|