awacke1 commited on
Commit
05f6d24
Β·
1 Parent(s): ae0ebbb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import nltk
3
+ import pandas as pd
4
+ from py_thesaurus import Thesaurus
5
+ import random
6
+
7
+ # Cache the NLTK corpora and functions for better performance
8
+ @st.cache
9
+ def load_nltk_data():
10
+ nltk.download('book')
11
+ from nltk.book import text1
12
+
13
+ return text1
14
+
15
+ # Function to generate random text from NLTK corpora
16
+ def generate_text():
17
+ text = load_nltk_data()
18
+ words = [word.lower() for word in text if word.isalpha()]
19
+ random_text = ' '.join(nltk.choice(words) for _ in range(10))
20
+
21
+ return random_text
22
+
23
+ # Function to replace a random word with its synonym
24
+ def replace_with_synonym(sentence):
25
+ words = sentence.split()
26
+ index = random.randint(0, len(words) - 1)
27
+ word = words[index]
28
+ synonyms = Thesaurus(word).get_synonym()
29
+
30
+ if synonyms:
31
+ replacement = random.choice(synonyms)
32
+ words[index] = replacement
33
+
34
+ return ' '.join(words)
35
+
36
+ st.title('Joke Parts Voting Game')
37
+ st.write('Upvote or downvote the funny joke parts generated below!')
38
+
39
+ # Create thumbs up and thumbs down buttons
40
+ thumbs_up = st.button('πŸ‘')
41
+ thumbs_down = st.button('πŸ‘Ž')
42
+
43
+ # Initialize upvote and downvote counts
44
+ upvotes = 0
45
+ downvotes = 0
46
+
47
+ # Increment upvote or downvote count when corresponding button is clicked
48
+ if thumbs_up:
49
+ upvotes += 1
50
+ elif thumbs_down:
51
+ downvotes += 1
52
+
53
+ # Display upvote and downvote count
54
+ st.write(f'πŸ‘ {upvotes} | πŸ‘Ž {downvotes}')
55
+
56
+ # Create scoreboard using pandas and display it
57
+ scoreboard = pd.DataFrame({'Upvotes': [upvotes], 'Downvotes': [downvotes]})
58
+ st.write(scoreboard)
59
+
60
+ # Save scoreboard to output CSV file
61
+ scoreboard.to_csv('output.csv', index=False)
62
+
63
+ # Generate a random text and replace a word with a synonym
64
+ original_text = generate_text()
65
+ modified_text = replace_with_synonym(original_text)
66
+
67
+ # Display the modified text
68
+ st.write(f'🀣 {modified_text}')