basic-job-recs / app.py
ashleychen's picture
fix app indent
b5fe008
import torch
import transformers
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification
import gradio as gr
import os
model_name = 'ashleychen/basic-job-recs'
bert_model_name = 'ashleychen/bart-finetuning-job-recs'
access_token = os.environ.get('private_token')
model = AutoModelForSeq2SeqLM.from_pretrained(
model_name, use_auth_token=access_token
)
tokenizer = AutoTokenizer.from_pretrained(
model_name, use_auth_token=access_token
)
bert_tokenizer = AutoTokenizer.from_pretrained(
bert_model_name, use_auth_token=access_token
)
bert_model = AutoModelForSequenceClassification.from_pretrained(
bert_model_name, use_auth_token=access_token
)
def create_prompt(stars, useful, funny, cool):
return f"Generate review: stars: {stars}, useful: {useful}, funny: {funny}, cool: {cool}"
def postprocess(review):
dot = review.rfind('.')
return review[:dot+1]
def generate_reviews(text):
inputs = tokenizer(text, return_tensors='pt')
out = model.generate(
input_ids=inputs.input_ids,
attention_mask=inputs.attention_mask,
max_new_tokens=100,
do_sample=True,
num_return_sequences=3,
temperature=1.2,
top_p=0.5,
)
return out, [tokenizer.decode(out[i], skip_special_tokens=True) for i in range(3)]
css = """
#ctr {text-align: center;}
#btn {color: white; background: linear-gradient( 90deg, rgba(255,166,0,1) 14.7%, rgba(255,99,97,1) 73% );}
"""
md_text = """<h1 style='text-align: center; margin-bottom: 1rem'>Generating Yelp reviews with BART-base ⭐⭐⭐</h1>
This space demonstrates how synthetic data generation can be performed on natural language columns, as found in the Yelp reviews dataset.
| review id | stars | useful | funny | cool | text |
|:---:|:---:|:---:|:---:|:---:|:---:|
| 0 | 5 | 1 | 0 | 1 | "Wow! Yummy, different, delicious. Our favorite is the lamb curry and korma. With 10 different kinds of naan!!! Don't let the outside deter you (because we almost changed our minds)...go in and try something new! You'll be glad you did!"
The model is a fine-tuned version of [facebook/bart-base](https://huggingface.com/facebook/bart-base) on Yelp reviews with the following input-output pairs:
- **Input**: "Generate review: stars: 5, useful: 1, funny: 0, cool: 1"
- **Output**: "Wow! Yummy, different, delicious. Our favorite is the lamb curry and korma. With 10 different kinds of naan!!! Don't let the outside deter you (because we almost changed our minds)...go in and try something new! You'll be glad you did!"
"""
resources = """## Resources
- Code for training: [github repo](https://github.com/EliottZemour/yelp-reviews/)
- The Yelp reviews dataset can be found in json format [here](https://www.yelp.com/dataset)."""
demo = gr.Blocks(css=css)
with demo:
with gr.Row():
gr.Textbox(value="Input prompt here.", label="text")
with gr.Row():
button = gr.Button("Generate job recommendations !", elem_id='btn')
with gr.Row():
output1 = gr.Textbox(label="Review #1")
output2 = gr.Textbox(label="Review #2")
output3 = gr.Textbox(label="Review #3")
button.click(
fn=generate_reviews,
inputs=[text],
outputs=[output1, output2, output3]
)
demo.launch()