DreamGenX commited on
Commit
23b3e24
1 Parent(s): a7e31af

Upload /example/interactive.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. example/interactive.py +129 -0
example/interactive.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # python interactive.py
2
+
3
+ # %%
4
+
5
+ import fileinput
6
+
7
+
8
+ from vllm import LLM, SamplingParams
9
+
10
+ from prompt.format import (
11
+ format_opus_v1_prompt,
12
+ OpusV1Character,
13
+ OpusV1Prompt,
14
+ OpusV1StorySystemPrompt,
15
+ OpusV1Turn,
16
+ )
17
+
18
+
19
+ # %%
20
+
21
+
22
+ def main():
23
+ sampling_params = SamplingParams(
24
+ # I usually stay between 0.0 and 1.0, especially for the Yi models I found lower tends to be better.
25
+ # For assistant tasks, I usually use 0.0.
26
+ temperature=0.8,
27
+ min_p=0.05,
28
+ presence_penalty=0.1,
29
+ frequency_penalty=0.1,
30
+ repetition_penalty=1.1,
31
+ max_tokens=200,
32
+ ignore_eos=True,
33
+ skip_special_tokens=False,
34
+ spaces_between_special_tokens=False,
35
+ stop=["<|im_end|>"],
36
+ include_stop_str_in_output=False,
37
+ )
38
+
39
+ # Set max_model_len to fit in memory.
40
+ model = LLM(
41
+ "dreamgen/opus-v1.2-7b",
42
+ max_model_len=2000,
43
+ enforce_eager=True,
44
+ swap_space=0,
45
+ gpu_memory_utilization=0.85,
46
+ )
47
+
48
+ plot_description = """
49
+ This is a fanfiction from the Harry Potter universe. In this alternate reality, Harry Potter is evil and secretly siding with Slytherin.
50
+ Up until now, Harry was pretending to be friends with Hermione and Ron, that changes when he invites Hermione to his chambers where he tricks her to drink Amorentia, the most powerful love potion.
51
+ """
52
+
53
+ char1 = OpusV1Character(
54
+ name="Harry Potter",
55
+ description="""Harry Potter in this fanfiction is secretly a member of Slytherin and is using his powers for evil rather than for good. Up until now, he was pretending to be friends with Hermione and Ron.""",
56
+ )
57
+ char2 = OpusV1Character(
58
+ name="Hermione Granger",
59
+ description="""Hermione appears just like in the original books.""",
60
+ )
61
+
62
+ story_prompt = OpusV1StorySystemPrompt(
63
+ plot_description=plot_description,
64
+ style_description="",
65
+ characters=[char1, char2],
66
+ )
67
+
68
+ turns = [
69
+ OpusV1Turn(
70
+ role="user",
71
+ content="""Harry invites Hermione into his chamber and offers her water, which Hermione happily accepts and drinks.""".strip(),
72
+ ),
73
+ OpusV1Turn(
74
+ role="text",
75
+ names=[char1.name],
76
+ content="""“Come in,” said Harry, waving at the doorway behind Hermione’s back.""".strip(),
77
+ ),
78
+ ]
79
+
80
+ def run():
81
+ turns.append(OpusV1Turn(role="text", content="", names=[char2.name], open=True))
82
+
83
+ prompt = OpusV1Prompt(story=story_prompt, turns=turns)
84
+
85
+ output = model.generate(
86
+ format_opus_v1_prompt(prompt), sampling_params, use_tqdm=False
87
+ )
88
+
89
+ response = OpusV1Turn(
90
+ role="text", content=output[0].outputs[0].text.strip(), names=[char2.name]
91
+ )
92
+ turns.append(response)
93
+ print(pretty_turn(response), flush=True)
94
+ print(f"[{char1.name}]: ", end="", flush=True)
95
+
96
+ print("## Plot description:\n")
97
+ print(plot_description.strip() + "\n\n")
98
+
99
+ for turn in turns:
100
+ print(pretty_turn(turn))
101
+
102
+ run()
103
+
104
+ for line in fileinput.input():
105
+ line = line.strip()
106
+ if line.startswith("/ins"):
107
+ content = line[4:].strip()
108
+ role = "user"
109
+ names = []
110
+ else:
111
+ content = line
112
+ role = "text"
113
+ names = [char1.name]
114
+
115
+ turns.append(OpusV1Turn(role=role, content=content, names=names))
116
+ run()
117
+
118
+
119
+ def pretty_turn(turn):
120
+ if turn.role == "user":
121
+ return f"/ins {turn.content.strip()}"
122
+ else:
123
+ if len(turn.names) > 0:
124
+ return f"[{turn.names[0]}]: {turn.content.strip()}"
125
+ else:
126
+ return turn.content.strip()
127
+
128
+
129
+ main()