CoolFace
Apppublic

uripper/AVA

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py145 linesDownload Raw Back to root
1import streamlit as st2import requests3 4BAD_WORD = False5my_api = st.secrets["my_api"]6bad_words = st.secrets["bad_words"]7    8def rev_generate(text, max_length=500, temperature=0.5, top_k=5, do_sample=False, use_cache=True):9    API_URL = "https://api-inference.huggingface.co/models/uripper/ReviewTrainingBot"10    headers = {"Authorization": f"Bearer {my_api}"}11    12    if do_sample:13        use_cache = False14        15    def query(payload):16        response = requests.post(API_URL, headers=headers, json=payload)17        return response.json()18        19    output = query({20        "inputs": f"{text}",21        "parameters": {"max_new_tokens": max_length, "temperature": temperature, "top_p": .95, "do_sample": do_sample, "no_repeat_ngram_size":2},22        "options": {"wait_for_model": True, "use_cache": use_cache},23    })24    return output25    26 27if "persona_chat_history" not in st.session_state:28    st.session_state.persona_chat_history = []29 30if "gordon_chat_history" not in st.session_state:31    st.session_state.gordon_chat_history = []32    33 34def main_page():35 36    CHAT = False37    REVIEW = False38    39 40 41    st.title("AVA")42    st.write("This model generates reviews of films and can be accessed on the drop down menu on the left. \n\nThe model is named after Ava from the movie Ex Machina. To use the model you can enter the name of a movie and generate a review for it or have Ava randomly generate a review. This was created by finetuning a GPT-2 model on a dataset of movie reviews. The dataset was created via scraping around 500,000 letterboxd reviews.")43 44    st.title("Limitations and biases")45    st.write("The main limitations of the review feature are that it is unable to find links between the movie title and the review itself, and struggles to determine positive and negative sentiment based on the score that is given. It however gives consistently plausible reviews, if not very plausible. It is unable to determine fact, and cannot give truthful reviews or reliably determine actors/directors for any given movie. Its main, and only, use case is for entertainment.")46    st.write("The review bot also has social biases. Due to its underlying model, it has many of the same biases as GPT-2. These biases can be found here: https://huggingface.co/gpt2. In addition to these biases, it also struggles with some of the unique examples of this training dataset. For a concrete example of this, it is fairly common for a review of a movie with gay or lesbian characters to be described as being 'very gay' on letterboxd.com. This is almost always used as a positive thing, but the bot itself is incapable of determining that this is a positive sentiment, and will describe random films this way in a manner that seems more like a slur. This language can likely be extended to other ways that have not been discovered yet, and the model should be handled with care.") 47 48 49def review():50    BAD_WORD = False51    st.title("Review")52    53    temperature = st.slider("Temperature", 0.1, 1.0, 0.8, 0.01)54    top_k = st.slider("Top K", 1, 100, 15, 1)55    max_length = st.slider("Max Length", 1, 250, 100, 1)56    do_sample = st.checkbox("Do Sample (If unchecked, will use greedy decoding, not recommended for review due to repetition)", True)57 58    st.write("Please enter the name of the movie you would like to review. First generation may take up to a minute or more, as the model is loading. Latter generations should load faster.")59    in_movie = st.text_input("Movie")60    review_button = st.button("Generate Review")61    random_review = st.button("Random Review")62    st.write("Please only press Generate Review or Random Review once, it will take a short amount of time to load during the first generation.")63    if review_button: 64        in_movie = "Movie: " + in_movie + " Score:"65        output = rev_generate(in_movie, max_length=max_length, temperature=temperature, top_k=top_k, do_sample=do_sample)66                67        check_output = output[0]["generated_text"]68        check_output = check_output.split(" ")69        for i in check_output:70            for j in bad_words:71                if i.lower() is j:72                    BAD_WORD =True73                    74                75        print(output)76        output = output[0]["generated_text"]77 78        if BAD_WORD == True:79 80            st.write("The bot generated a slur, please try again.")81            BAD_WORD = False82        else:83            out_movie =output.split("Score:")[0]84            out_movie = out_movie.replace("Movie: ", "").replace("|","")85            score = output.split("Review:")[0]86            score = score.split("Score:")[1]87            score = score.replace("|","")88            review = output.split("Review:")[1] 89            90            review = review.replace("…", ".")91            review = review.replace("...", ".").replace("|","")92            review = review.replace("<br/>", "/n").replace("br/>","").replace("br","").replace("<","").replace(">","")93            94 95            st.write("Movie:")96            st.write(out_movie)97            st.write("Score:")98            st.write(score)99            st.write("Review:")100            st.write(review)101    102    if random_review:103        output = rev_generate("Movie:", max_length=max_length, temperature=temperature, top_k=top_k, do_sample=do_sample)     104        check_output = output[0]["generated_text"]105        check_output = check_output.split(" ")106        for i in check_output:107            for j in bad_words:108                if i.lower() is j:109                    BAD_WORD =True110        print(output)111        output = output[0]["generated_text"]112        if BAD_WORD == True:113            st.write(i)114            st.write("The bot generated a slur, please try again.")115            BAD_WORD = False116        else:117            out_movie =output.split("Score:")[0]118            out_movie = out_movie.replace("Movie: ", "").replace("|","")119            score = output.split("Review:")[0]120            score = score.split("Score:")[1]121            score = score.replace("|","")122            review = output.split("Review:")[1] 123            124            review = review.replace("…", ".")125            review = review.replace("...", ".").replace("|","")126            review = review.replace("<br/>", "/n").replace("br/>","").replace("br","").replace("<","").replace(">","")127 128            st.write("Movie:")129            st.write(out_movie)130            st.write("Score:")131            st.write(score)132            st.write("Review:")133            st.write(review)134        135 136 137page_names_to_funcs = {138    "Main Page": main_page,139    "Ava": review,140}141 142selected_page = st.sidebar.selectbox("Select a page", page_names_to_funcs.keys())143page_names_to_funcs[selected_page]()144 145