aloobun commited on
Commit
9e3d72e
1 Parent(s): ade2fdb

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +29 -56
README.md CHANGED
@@ -20,62 +20,35 @@ tags:
20
  ## Usage:
21
 
22
  ```
23
- def text_gen_eval_wrapper(model, tokenizer, prompt, model_id=1, show_metrics=True, temp=0.7, max_length=200):
24
- """
25
- A wrapper function for inferencing, evaluating, and logging text generation pipeline.
26
-
27
- Parameters:
28
- model (str or object): The model name or the initialized text generation model.
29
- tokenizer (str or object): The tokenizer name or the initialized tokenizer for the model.
30
- prompt (str): The input prompt text for text generation.
31
- model_id (int, optional): An identifier for the model. Defaults to 1.
32
- show_metrics (bool, optional): Whether to calculate and show evaluation metrics.
33
- Defaults to True.
34
- max_length (int, optional): The maximum length of the generated text sequence.
35
- Defaults to 200.
36
-
37
- Returns:
38
- generated_text (str): The generated text by the model.
39
- metrics (dict): Evaluation metrics for the generated text (if show_metrics is True).
40
- """
41
- # Suppress Hugging Face pipeline logging
42
- logging.set_verbosity(logging.CRITICAL)
43
-
44
- # Initialize the pipeline
45
- pipe = pipeline(task="text-generation",
46
- model=model,
47
- tokenizer=tokenizer,
48
- max_length=max_length,
49
- do_sample=True,
50
- temperature=temp)
51
-
52
- # Generate text using the pipeline
53
- pipe = pipeline(task="text-generation",
54
- model=model,
55
- tokenizer=tokenizer,
56
- max_length=200)
57
- result = pipe(f"<s>[INST] {prompt} [/INST]")
58
- generated_text = result[0]['generated_text']
59
-
60
- # Find the index of "### Assistant" in the generated text
61
- index = generated_text.find("[/INST] ")
62
- if index != -1:
63
- # Extract the substring after "### Assistant"
64
- substring_after_assistant = generated_text[index + len("[/INST] "):].strip()
65
- else:
66
- # If "### Assistant" is not found, use the entire generated text
67
- substring_after_assistant = generated_text.strip()
68
-
69
- if show_metrics:
70
- # Calculate evaluation metrics
71
- metrics = run_metrics(substring_after_assistant, prompt, model_id)
72
-
73
- return substring_after_assistant, metrics
74
- else:
75
- return substring_after_assistant
76
 
 
77
 
78
- prompt = "### Human: Why can camels survive for long without water? ### Assistant:"
79
- generated_text = text_gen_eval_wrapper(model, tokenizer, prompt, show_metrics=False, max_length=250)
80
- print(generated_text)
81
  ```
 
20
  ## Usage:
21
 
22
  ```
23
+ from transformers import AutoTokenizer
24
+ import transformers
25
+ import torch
26
+
27
+ model = "aloobun/llama2-7b-openhermes-15k-mini"
28
+ prompt = "What are large language models?"
29
+
30
+ tokenizer = AutoTokenizer.from_pretrained(model)
31
+ pipeline = transformers.pipeline(
32
+ "text-generation",
33
+ model=model,
34
+ torch_dtype=torch.float16,
35
+ device_map="auto",
36
+ )
37
+
38
+ sequences = pipeline(
39
+ f'[INST] {prompt} [/INST]',
40
+ do_sample=True,
41
+ top_k=10,
42
+ num_return_sequences=1,
43
+ eos_token_id=tokenizer.eos_token_id,
44
+ max_length=200,
45
+ )
46
+ for seq in sequences:
47
+ print(f"Result: {seq['generated_text']}")
48
+ ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
+ ### Output:
51
 
52
+ ```
53
+ Result: [INST] What are large language models? [/INST] Large language models are artificial intelligence systems that can be trained on vast amounts of text to generate human-like language. Libraries of natural language processing (NLP) algorithms like BERT and GPT have allowed these systems to learn and improve their capacity for language understanding and generation. These language models have found applications in natural language translation, text summarization, chatbots, and even creative writing. They can help in tasks like predicting the next word in a sentence or even generating a whole text based on a given topic or prompt. Large language models have the potential to revolutionize many industries, from customer support to content creation and beyond. However, their use and development raise important ethical and societal questions, such as the impact on employment or the potential misuse of generated content. As AI technology continues to advance, the role and capabilities of large language models will continue to evolve.
 
54
  ```