Step 1: Install Required Libraries: First, you'll need to install the Hugging Face transformers library and torch for PyTorch support.
pip install transformers torch
Step 2: Load a Pre-trained Model: You can use Hugging Face's bloom-1b7 model, a popular model to generate human-like text across a wide range of languages. You can use it for creative writing, code generation, and more..
Here's a basic script to experiment with it.
promptSetup.py
import os from transformers import BloomForCausalLM, BloomTokenizerFast # Load the BLOOM model and tokenizer model_name = "bigscience/bloom-1b7" model = BloomForCausalLM.from_pretrained(model_name) tokenizer = BloomTokenizerFast.from_pretrained(model_name) def generate_response(prompt): # Tokenize the prompt inputs = tokenizer(prompt, return_tensors="pt") # Ensure input_ids are of type LongTensor (for embedding layer) input_ids = inputs["input_ids"].long() # Explicitly cast to LongTensor # Generate a response with sampling enabled outputs = model.generate(input_ids, max_length=100, num_return_sequences=1, temperature=0.7, # Temperature for sampling do_sample=True) # Enable sampling mode # Decode and return the result return tokenizer.decode(outputs[0], skip_special_tokens=True) # Function to format the conversation output def format_conversation(response): separator = "\n" + "="*50 + "\n" # You can adjust the separator length formatted_conversation = f"BLOOM: {response}\n" return formatted_conversation # Interactive loop print("Welcome to BLOOM-powered conversation!\n" + "="*50 + "\n") while True: user_input = input("You: ") if user_input.lower() == "exit": print("\nEnding the conversation. Goodbye!") break response = generate_response(user_input) print(format_conversation(response)) print("\n" + "-"*50 + "\n") # Adding spacing between conversations
Execute following command to experiment with the Application.
python3 promptSetup.py
Prompt 1: For the input “Once Upon A Time”
You: Once Upon A Time ================================================== BLOOM: Once Upon A Time In Hollywood (1968) - The first time I've seen a movie in my life, which I will never forget. The movie is a good one, but I can't even remember what it was about. I just remember the music. I just remembered the title and the poster. The film is called The Girl from the Sea, which I thought was a good name for the movie. I got in the car, and I was going to go to this movie. I called the theater
Prompt 2: For the input “SQL Stands for Structured”
You: SQL Stands for Structured ================================================== BLOOM: SQL Stands for Structured Query Language. It is a set of SQL statements. SQL is used to access database. SQL is part of Oracle Database. SQL is a language for creating a query. SQL is a combination of the words Structured Query Language and Language. SQL is used to create queries. SQL is used to connect the database and database server. It is a language used to do database operations. The SQL statement is very powerful. It can create a query and connect to the database.
Prompt 3: For the Input “Tell Me a Joke in English”
You: Tell Me a Joke in English ================================================== BLOOM: Tell Me a Joke in English (I don't know how to write that in English, so I don't know if that is a joke) I am a former teacher. I am also a retired teacher. I was a teacher because I wanted to be a teacher. I was a retired teacher because I wanted to be a teacher again. I am also a former teacher because I wanted to be a teacher again. I would like to thank you for your time and for your comments. I have been thinking
No comments:
Post a Comment