furquan commited on
Commit
1f78700
·
1 Parent(s): 35faf8b

Demo for prompt-tuned opt-2.7b

Browse files
Files changed (1) hide show
  1. app.py +26 -0
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+
4
+ from transformers import pipeline, AutoTokenizer, AutoModel
5
+
6
+ #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')
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained("furquan/opt_2_7_b_prompt_tuned_sentiment_analysis",cache_dir="/local/home/furquanh/myProjects/week12/", trust_remote_code=True)
9
+ model = AutoModel.from_pretrained("furquan/opt_2_7_b_prompt_tuned_sentiment_analysis",cache_dir="/local/home/furquanh/myProjects/week12/", trust_remote_code=True)
10
+
11
+ title = "OPT-2.7B"
12
+ description = "This demo uses meta's opt-2.7b model prompt tuned on the Stanford Sentiment Treebank-5 way dataset to only output the sentiment of a given text."
13
+ 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>"
14
+
15
+
16
+ def sentiment(text):
17
+ tokenized = tokenizer(text, return_tensors='pt')
18
+ with torch.no_grad():
19
+ outputs = model.generate(
20
+ input_ids=tokenized["input_ids"], attention_mask=tokenized["attention_mask"]
21
+ )
22
+ return f"text: {text} Sentiment: {tokenizer.decode(outputs[0][-3:], skip_special_tokens=True)}"
23
+
24
+ iface = gr.Interface(fn=sentiment, inputs="text", outputs="text", title=title,
25
+ description=description, article=article)
26
+ iface.launch()