-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_gen.py
43 lines (34 loc) · 1.19 KB
/
text_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Use a pipeline as a high-level helper
from transformers import pipeline
def generate_text(prompt, max_length=1000):
"""
Generate text based on a prompt
:param max_length: The maximum length of the generated text
:param prompt: The prompt to generate text from
:return: The generated text
"""
print(f"Generating text for prompt: {prompt}")
messages = [{"role": "user", "content": prompt}]
# Use hugging face pipeline to load model onto GPU
try:
model_group = "TinyLlama"
model_name = "TinyLlama-1.1B-Chat-v1.0"
model = f'{model_group}/{model_name}'
pipe = pipeline("text-generation", model=model, device=0, max_length=max_length,
truncation=True)
except Exception as e:
print(f"Pipeline died: {e}")
raise e
print("Pipeline loaded")
# Generate text
try:
response = pipe(messages)[0]["generated_text"][1]["content"]
except Exception as e:
print(f"Pipeline died: {e}")
raise e
print("Text generated")
return response
if __name__ == "__main__":
# Test the text generation function
text = generate_text("Hello, how are you?")
print(text)