|
import streamlit as st |
|
import pandas as pd |
|
from py_thesaurus import Thesaurus |
|
import random |
|
from transformers import GPT2LMHeadModel, GPT2Tokenizer |
|
|
|
|
|
model = GPT2LMHeadModel.from_pretrained("gpt2") |
|
tokenizer = GPT2Tokenizer.from_pretrained("gpt2") |
|
|
|
|
|
def generate_text(prompt): |
|
inputs = tokenizer.encode(prompt, return_tensors="pt") |
|
outputs = model.generate(inputs, max_length=50, num_return_sequences=1) |
|
return tokenizer.decode(outputs[0]) |
|
|
|
|
|
def replace_with_synonym(sentence): |
|
words = sentence.split() |
|
index = random.randint(0, len(words) - 1) |
|
word = words[index] |
|
synonyms = Thesaurus(word).get_synonym() |
|
|
|
if synonyms: |
|
replacement = random.choice(synonyms) |
|
words[index] = replacement |
|
|
|
return ' '.join(words) |
|
|
|
st.title('Joke Parts Voting Game') |
|
st.write('Upvote or downvote the funny joke parts generated below!') |
|
|
|
|
|
thumbs_up = st.button('π') |
|
thumbs_down = st.button('π') |
|
|
|
|
|
upvotes = 0 |
|
downvotes = 0 |
|
|
|
|
|
if thumbs_up: |
|
upvotes += 1 |
|
elif thumbs_down: |
|
downvotes += 1 |
|
|
|
|
|
st.write(f'π {upvotes} | π {downvotes}') |
|
|
|
|
|
scoreboard = pd.DataFrame({'Upvotes': [upvotes], 'Downvotes': [downvotes]}) |
|
st.write(scoreboard) |
|
|
|
|
|
scoreboard.to_csv('output.csv', index=False) |
|
|
|
|
|
prompt = "Tell me a joke:" |
|
original_text = generate_text(prompt) |
|
modified_text = replace_with_synonym(original_text) |
|
|
|
|
|
st.write(f'π€£ {modified_text}') |
|
|