import torch
from transformers import XLNetTokenizer, XLNetLMHeadModel
# Load pre-trained XLNet model and tokenizer
model = XLNetLMHeadModel.from_pretrained('xlnet-large-cased')
tokenizer = XLNetTokenizer.from_pretrained('xlnet-large-cased')
symbol = 'AAPL'
pe_ratio = 12.5
ps_ratio= 3.1
de_ratio = 0.8
# Define the input prompt
input_prompt = f"Based on the current market conditions, the stock with the symbol {symbol} looks like a promising investment opportunity. With a price-to-earnings ratio of {pe_ratio}, a price-to-sales ratio of {ps_ratio}, and a debt-to-equity ratio of {de_ratio}, this stock appears to be undervalued compared to its peers. If you're looking for a stock with strong potential for growth and a solid financial foundation, {symbol} may be the best choice for you."
# Define a list to store generated sentences that meet our criteria
generated_sentences = []
# Generate text using the modified input prompt
while len(generated_sentences) < 3: # Generate 3 unique sentences
input_ids = tokenizer.encode(input_prompt, return_tensors='pt')
outputs = model.generate(input_ids, max_length=300, do_sample=True)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Check if the generated sentence includes all relevant information from the prompt
if f"price-to-earnings ratio of {pe_ratio}" in generated_text and \
f"price-to-sales ratio of {ps_ratio}" in generated_text and \
f"debt-to-equity ratio of {de_ratio}" in generated_text and \
f"symbol {symbol}" in generated_text:
# Check if the last sentence is cut off
if "." not in generated_text.split()[-1]:
continue
# Check for uniqueness
if generated_text not in generated_sentences:
# Add the generated sentence to the list of valid sentences
generated_sentences.append(generated_text)
# Break the loop if no valid sentence is generated within 10 tries
if len(generated_sentences) == 0 and model.config.num_beams > 1:
model.config.num_beams -= 1
continue
elif len(generated_sentences) == 0:
break
# Print the final generated sentences
for i, sentence in enumerate(generated_sentences):
print(f"Generated sentence {i+1}: {sentence}\n")
# Based on the current market conditions, the stock with the symbol AAPL looks like a promising investment opportunity.
# With a price-to-earnings ratio of 12.5, a price-to-sales ratio of 3.1, and a debt-to-equity ratio of 0.8,
# this stock appears to be undervalued compared to its peers.
# If you're looking for a stock with strong potential for growth and a solid financial foundation,
# AAPL may be the best choice for you. trading day, AAPL is a well-received company in its market.
# The company with the symbol AAPL is a well-respected symbol in its industry.
# Today, AAPL is well-pted and well-liken by all the employees of the company. Although,
# it is well-versed and well-liken by all the employees of the company in its industries.
# AAPL is well-liken by all the employees of the company in its industries.
# The symbol with the symbol AAPL is well-known by all the employees of the company in its industries.
# AAPL is well-known by all the employees of the company in its industries.
# AAPL is well-known by all the employees of the company in its industries.
# AAPL is well-known by all the employees of the company in its industries. AAPL is well-known by all the employees of the company