awacke1 commited on
Commit
e0a7e1b
Β·
1 Parent(s): 9f0f953

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -17
app.py CHANGED
@@ -1,24 +1,18 @@
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(allow_output_mutation=True)
9
- def load_nltk_data():
10
- nltk.download('gutenberg')
11
- from nltk.corpus import gutenberg
12
 
13
- return gutenberg.words('austen-emma.txt')
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):
@@ -60,8 +54,9 @@ st.write(scoreboard)
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
 
1
  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):
 
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