Eliott commited on
Commit
e030ae6
1 Parent(s): cf3b21f

update app

Browse files
Files changed (2) hide show
  1. app.py +64 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import transformers
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+ import gradio as gr
5
+ import os
6
+
7
+ model_name = 'eliolio/bart-finetuned-yelpreviews'
8
+
9
+ access_token = os.environ.get('private_token')
10
+
11
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name, use_auth_token=access_token)
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=access_token)
13
+
14
+ def create_prompt(stars, useful, funny, cool):
15
+ return f"Generate review: stars: {stars}, useful: {useful}, funny: {funny}, cool: {cool}"
16
+
17
+ def generate_reviews(stars, useful, funny, cool):
18
+ text = create_prompt(stars, useful, funny, cool)
19
+ inputs = tokenizer(text, return_tensors='pt')
20
+ out = model.generate(
21
+ input_ids=inputs.input_ids,
22
+ attention_mask=inputs.attention_mask,
23
+ num_beams=5,
24
+ num_return_sequences=3
25
+ )
26
+ reviews = []
27
+ for review in out:
28
+ reviews.append(tokenizer.decode(review, skip_special_tokens=True))
29
+
30
+ return reviews[0], reviews[1], reviews[2]
31
+
32
+ css = """
33
+ #ctr {text-align: center;}
34
+ #btn {color: white; background: linear-gradient(90deg, #00d2ff 0%, #3a47d5 100%);}
35
+ """
36
+
37
+
38
+
39
+ md_text = """## Generating Yelp reviews with BART-base ⭐⭐⭐"""
40
+ demo = gr.Blocks(css=css)
41
+ with demo:
42
+ with gr.Row():
43
+ gr.Markdown(md_text, elem_id='ctr')
44
+
45
+ with gr.Row():
46
+ stars = gr.inputs.Slider(minimum=0, maximum=5, step=1, default=0, label="stars")
47
+ useful = gr.inputs.Slider(minimum=0, maximum=5, step=1, default=0, label="useful")
48
+ funny = gr.inputs.Slider(minimum=0, maximum=5, step=1, default=0, label="funny")
49
+ cool = gr.inputs.Slider(minimum=0, maximum=5, step=1, default=0, label="cool")
50
+ with gr.Row():
51
+ button = gr.Button("Generate reviews !", elem_id='btn')
52
+
53
+ with gr.Row():
54
+ output1 = gr.Textbox(label="Review #1")
55
+ output2 = gr.Textbox(label="Review #2")
56
+ output3 = gr.Textbox(label="Review #3")
57
+
58
+ button.click(
59
+ fn=generate_reviews,
60
+ inputs=[stars, useful, funny, cool],
61
+ outputs=[output1, output2, output3]
62
+ )
63
+
64
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ tensorflow
2
+ torch
3
+ transformers