Spaces:
Runtime error
Runtime error
Commit ·
1f3cb28
1
Parent(s): a343ac9
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def select_model():
|
| 6 |
+
while True:
|
| 7 |
+
print("\nAvailable GPT-2 Models:")
|
| 8 |
+
print("1. gpt2 (Small)")
|
| 9 |
+
print("2. gpt2-medium (Medium)")
|
| 10 |
+
print("3. gpt2-large (Large)")
|
| 11 |
+
print("4. gpt2-xl (Extra Large)")
|
| 12 |
+
choice = input("Select a model (1/2/3/4): ")
|
| 13 |
+
|
| 14 |
+
if choice == "1":
|
| 15 |
+
return "gpt2"
|
| 16 |
+
elif choice == "2":
|
| 17 |
+
return "gpt2-medium"
|
| 18 |
+
elif choice == "3":
|
| 19 |
+
return "gpt2-large"
|
| 20 |
+
elif choice == "4":
|
| 21 |
+
return "gpt2-xl"
|
| 22 |
+
else:
|
| 23 |
+
print("Invalid choice. Please select a valid model.")
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def enhance_prompt(prompt, model_name, max_length=50, num_return_sequences=1):
|
| 27 |
+
# Load the selected pre-trained GPT-2 model and tokenizer
|
| 28 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
| 29 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
| 30 |
+
|
| 31 |
+
# Tokenize the prompt
|
| 32 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
| 33 |
+
|
| 34 |
+
# Generate text based on the prompt
|
| 35 |
+
output = model.generate(
|
| 36 |
+
input_ids,
|
| 37 |
+
max_length=max_length,
|
| 38 |
+
num_return_sequences=num_return_sequences,
|
| 39 |
+
no_repeat_ngram_size=2, # Avoid repetitive phrases
|
| 40 |
+
top_k=50, # Limit choices to top-k tokens
|
| 41 |
+
top_p=0.95, # Control diversity with nucleus sampling
|
| 42 |
+
temperature=0.7 # Adjusts the randomness of the output
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Decode and return the generated text
|
| 46 |
+
enhanced_prompts = [tokenizer.decode(output_item, skip_special_tokens=True) for output_item in output]
|
| 47 |
+
return enhanced_prompts
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
while True:
|
| 52 |
+
model_name = select_model()
|
| 53 |
+
prompt = input("Enter a prompt (or 'exit' to quit): ")
|
| 54 |
+
if prompt.lower() == "exit":
|
| 55 |
+
break
|
| 56 |
+
|
| 57 |
+
enhanced_prompts = enhance_prompt(prompt, model_name)
|
| 58 |
+
print("\nEnhanced Prompts:")
|
| 59 |
+
for idx, enhanced_prompt in enumerate(enhanced_prompts):
|
| 60 |
+
print(f"Enhanced Prompt {idx + 1}: {enhanced_prompt}\n")
|