-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
141 lines (122 loc) · 4 KB
/
demo.py
File metadata and controls
141 lines (122 loc) · 4 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import outlines
import modal
import os
from pydantic import BaseModel, Field
from rich import print
from rich.panel import Panel
class NewSubject(BaseModel):
rewritten_text: str = Field(..., title="The rewritten text after change a subject.")
story_title: str = Field(..., title="The title of the story.")
# Make our app
app = modal.App("fiction-machine")
# What language model will we use?
llm_base = "mistralai/Mistral-Nemo-Base-2407"
llm_instruct = "microsoft/Phi-3.5-mini-instruct"
# Set up the outlines image
modal_image = modal.Image.debian_slim(python_version="3.11").pip_install(
"outlines",
"transformers",
"accelerate",
"sentencepiece",
"bitsandbytes",
"vllm"
)
@app.cls(gpu="H100", image=modal_image, secrets=[modal.Secret.from_dotenv()])
class Model:
@modal.build()
def download_model(self):
import outlines
outlines.models.transformers(
llm_base,
device="cuda",
model_kwargs={
"token": os.environ["HF_TOKEN"],
"trust_remote_code": True,
},
)
@modal.enter()
def setup(self):
import outlines
self.model = outlines.models.transformers(
llm_base,
device="cuda",
)
@modal.method()
def make_story(self, prompt: str, max_tokens: int = 20):
generator = outlines.generate.text(
self.model,
sampler=outlines.samplers.multinomial(temperature=0.3),
)
return generator(prompt, max_tokens)
@app.cls(gpu="H100", image=modal_image, secrets=[modal.Secret.from_dotenv()])
class InstructModel:
@modal.build()
def download_model(self):
import outlines
outlines.models.transformers(
llm_instruct,
device="cuda",
model_kwargs={
"token": os.environ["HF_TOKEN"],
"trust_remote_code": True,
},
)
@modal.enter()
def setup(self):
import outlines
self.model = outlines.models.transformers(
llm_instruct,
device="cuda",
)
@modal.method()
def change_subject(self, prompt: str):
generator = outlines.generate.json(
self.model,
NewSubject,
sampler=outlines.samplers.multinomial(temperature=0.7),
)
return generator(prompt)
examples = [
{"text": "a dog is running", "rewritten_text":"a cat is running"},
{"text": "the boy is playing football", "rewritten_text": "the girl is playing football"},
{"text": "the president gave a speech", "rewritten_text": "the governor gave a speech"},
{"text": "the horse jumped over the fence", "rewritten_text": "the cow jumped over the fence"},
{"text": "a soldier stood guard", "rewritten_text": "a police officer stood guard"}
]
@outlines.prompt
def subject_change_prompt(
story: str,
examples: list = examples,
):
"""
<|system|>
You are a world class storyteller. You have been asked to rewrite a story by just changing one of the subjects of the story.
Examples
--------
{% for example in examples %}
Text: {{ example.text }}
Rewritten Text: {{ example.rewritten_text }}
{% endfor %}
<|end|>
<|user|>
{{ story }}
<|end|>
<|assistant|>
"""
# Local entrypoint
@app.local_entrypoint()
def main():
for i in range(3):
# Set up our story
story = Model().make_story.remote("Once upon a time a dog called", 50)
story_transformation = InstructModel().change_subject.remote(story)
# Set up panel width
panel_width = 60
# Start printing out some information about the story
print(Panel.fit(story,
width=panel_width,
title="Story"))
# Print out the transformed story
print(Panel.fit(story_transformation.rewritten_text,
width=panel_width,
title=f"Transformed Story: {story_transformation.story_title}"))