awacke1 commited on
Commit
8d26781
Β·
1 Parent(s): aa149a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -47
app.py CHANGED
@@ -2,62 +2,57 @@ import streamlit as st
2
  import pandas as pd
3
  from py_thesaurus import Thesaurus
4
  import random
5
- from transformers import GPT2LMHeadModel, GPT2Tokenizer
6
 
7
- # Load GPT-2 model and tokenizer
8
- model = GPT2LMHeadModel.from_pretrained("gpt2")
9
- tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
 
 
 
 
10
 
11
- # Function to generate random text using GPT-2
12
- def generate_text(prompt):
13
- inputs = tokenizer.encode(prompt, return_tensors="pt")
14
- outputs = model.generate(inputs, max_length=50, num_return_sequences=1)
15
- return tokenizer.decode(outputs[0])
16
-
17
- # Function to replace a random word with its synonym
18
  def replace_with_synonym(sentence):
19
  words = sentence.split()
20
  index = random.randint(0, len(words) - 1)
21
  word = words[index]
22
  synonyms = Thesaurus(word).get_synonym()
23
-
24
  if synonyms:
25
  replacement = random.choice(synonyms)
26
  words[index] = replacement
27
-
28
  return ' '.join(words)
29
 
30
- st.title('Joke Parts Voting Game')
31
- st.write('Upvote or downvote the funny joke parts generated below!')
32
-
33
- # Create thumbs up and thumbs down buttons
34
- thumbs_up = st.button('πŸ‘')
35
- thumbs_down = st.button('πŸ‘Ž')
36
-
37
- # Initialize upvote and downvote counts
38
- upvotes = 0
39
- downvotes = 0
40
-
41
- # Increment upvote or downvote count when corresponding button is clicked
42
- if thumbs_up:
43
- upvotes += 1
44
- elif thumbs_down:
45
- downvotes += 1
46
-
47
- # Display upvote and downvote count
48
- st.write(f'πŸ‘ {upvotes} | πŸ‘Ž {downvotes}')
49
-
50
- # Create scoreboard using pandas and display it
51
- scoreboard = pd.DataFrame({'Upvotes': [upvotes], 'Downvotes': [downvotes]})
52
- st.write(scoreboard)
53
-
54
- # Save scoreboard to output CSV file
55
- scoreboard.to_csv('output.csv', index=False)
56
-
57
- # Generate a random text using GPT-2
58
- prompt = "Tell me a joke:"
59
- original_text = generate_text(prompt)
60
- modified_text = replace_with_synonym(original_text)
61
-
62
- # Display the modified text
63
- st.write(f'🀣 {modified_text}')
 
2
  import pandas as pd
3
  from py_thesaurus import Thesaurus
4
  import random
5
+ import os.path
6
 
7
+ def generate_sentence():
8
+ words = ["apple", "banana", "grape", "orange", "watermelon", "pineapple", "cherry", "strawberry", "blueberry", "mango"]
9
+ random_words = random.sample(words, 3)
10
+ question = f"What did the {random_words[0]} say to the {random_words[1]}?"
11
+ answer = f"The {random_words[0]} said, 'Let's hang out with the {random_words[2]}!'"
12
+ context = f"In the context of a fruit gathering, the {random_words[0]}, {random_words[1]}, and {random_words[2]} were having fun."
13
+ return f"{question} {answer} {context}"
14
 
 
 
 
 
 
 
 
15
  def replace_with_synonym(sentence):
16
  words = sentence.split()
17
  index = random.randint(0, len(words) - 1)
18
  word = words[index]
19
  synonyms = Thesaurus(word).get_synonym()
 
20
  if synonyms:
21
  replacement = random.choice(synonyms)
22
  words[index] = replacement
 
23
  return ' '.join(words)
24
 
25
+ def load_or_create_scoreboard(filename):
26
+ if os.path.isfile(filename):
27
+ return pd.read_csv(filename)
28
+ else:
29
+ scoreboard = pd.DataFrame({'Upvotes': [0], 'Downvotes': [0]})
30
+ scoreboard.to_csv(filename, index=False)
31
+ return scoreboard
32
+
33
+ def update_scoreboard(scoreboard, thumbs_up, thumbs_down):
34
+ if thumbs_up:
35
+ scoreboard.loc[0, 'Upvotes'] += 1
36
+ elif thumbs_down:
37
+ scoreboard.loc[0, 'Downvotes'] += 1
38
+ return scoreboard
39
+
40
+ def main():
41
+ filename = 'output.csv'
42
+ scoreboard = load_or_create_scoreboard(filename)
43
+ st.title('Joke Parts Voting Game')
44
+ thumbs_up = st.button('πŸ‘')
45
+ thumbs_down = st.button('πŸ‘Ž')
46
+ scoreboard = update_scoreboard(scoreboard, thumbs_up, thumbs_down)
47
+ scoreboard.to_csv(filename, index=False)
48
+ col1, col2 = st.beta_columns(2)
49
+ with col1:
50
+ st.write(f'πŸ‘ {scoreboard.loc[0, "Upvotes"]}')
51
+ with col2:
52
+ st.write(f'πŸ‘Ž {scoreboard.loc[0, "Downvotes"]}')
53
+ original_text = generate_sentence()
54
+ modified_text = replace_with_synonym(original_text)
55
+ st.write(f'🀣 {modified_text}')
56
+
57
+ if __name__ == "__main__":
58
+ main()