Spaces:
Sleeping
Sleeping
Update app.py
Browse filesfixed embeddings bug
app.py
CHANGED
@@ -14,15 +14,19 @@ writing_tones = ["Formal","Informal","Humorous","Serious","Sarcastic","Satirical
|
|
14 |
|
15 |
|
16 |
# initialize client
|
|
|
17 |
client = InferenceClient(
|
18 |
-
#"mistralai/Mixtral-8x7B-Instruct-v0.1" #wont fit onto free version
|
19 |
"v1olet/v1olet_marcoroni-go-bruins-merge-7B"
|
20 |
)
|
21 |
|
22 |
-
# Load pre-trained
|
23 |
-
model_id = "sentence-transformers/all-MiniLM-L6-v2" #
|
24 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
25 |
-
model = AutoModel.from_pretrained(model_id) #
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Function to convert text items into embeddings
|
28 |
def get_embeddings(text_items):
|
@@ -35,40 +39,35 @@ def get_embeddings(text_items):
|
|
35 |
embeddings.append(pooled_output)
|
36 |
return embeddings
|
37 |
|
38 |
-
# Helper Function to calculate cosine similarity between two embeddings
|
39 |
-
# since we are using multiple variables, and since we want to compare more than just positive/negative ideas,
|
40 |
-
# cosine similarity works better than euclidean distance as a measure of similarity
|
41 |
-
def calculate_cosine_similarity(embedding1, embedding2):
|
42 |
-
return cosine_similarity(embedding1, embedding2)[0][0]
|
43 |
-
|
44 |
# Helper Function to select values with small enough cosine similarity and concatenate them into a string
|
45 |
-
def select_values_with_low_similarity(
|
46 |
selected_values = []
|
47 |
selected_indices = set()
|
48 |
|
49 |
while len(selected_values) < num_values_to_select:
|
50 |
-
index1, index2 = random.sample(range(len(
|
51 |
-
|
52 |
|
53 |
-
if index1 != index2 and calculate_cosine_similarity(
|
54 |
if index1 not in selected_indices:
|
55 |
-
selected_values.append(
|
56 |
selected_indices.add(index1)
|
57 |
if index2 not in selected_indices:
|
58 |
-
selected_values.append(
|
59 |
selected_indices.add(index2)
|
60 |
|
61 |
# Concatenate the selected values into a single string
|
62 |
selected_string = ', '.join(selected_values)
|
63 |
return selected_string
|
64 |
|
|
|
65 |
# Convert text items into embeddings
|
66 |
genre_embeddings = get_embeddings(book_genres)
|
67 |
theme_embeddings = get_embeddings(book_themes)
|
68 |
tone_embeddings = get_embeddings(writing_tones)
|
69 |
-
#clear memory
|
70 |
del model
|
71 |
-
#torch.cuda.empty_cache()
|
72 |
|
73 |
# helper function to format the prompt appropriately.
|
74 |
# For this creative writing tool, the user doesn't design the prompt itself
|
@@ -76,11 +75,11 @@ del model
|
|
76 |
def format_prompt(message, genres, tones, themes):
|
77 |
# pick random ones if user leaves it blank but make sure they aren't opposites
|
78 |
if not genres:
|
79 |
-
selected_genres = select_values_with_low_similarity(book_genres, random.randint(3, 5), 0.2) # Adjust threshold as needed
|
80 |
if not tones:
|
81 |
-
selected_tones = select_values_with_low_similarity(writing_tones, random.randint(3, 5), 0.2) # Adjust threshold as needed
|
82 |
if not themes:
|
83 |
-
selected_themes = select_values_with_low_similarity(book_themes, random.randint(3, 5), 0.2) # Adjust threshold as needed
|
84 |
|
85 |
#Alpaca format since we can't use mixtral on free CPU settings
|
86 |
prompt = "Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n"
|
@@ -93,7 +92,7 @@ def format_prompt(message, genres, tones, themes):
|
|
93 |
|
94 |
|
95 |
# main function
|
96 |
-
def generate(genres, themes, tones, system_prompt, temperature=1.25, max_new_tokens=
|
97 |
# check the temperature value, should not be too low, and make sure the values are floats
|
98 |
temperature = float(temperature)
|
99 |
if temperature < 1e-2:
|
@@ -111,7 +110,7 @@ def generate(genres, themes, tones, system_prompt, temperature=1.25, max_new_tok
|
|
111 |
)
|
112 |
|
113 |
formatted_prompt = format_prompt(f"{system_prompt}, '' ", genres, tones, themes)
|
114 |
-
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
115 |
output = ""
|
116 |
|
117 |
for response in stream:
|
@@ -166,21 +165,21 @@ additional_inputs=[
|
|
166 |
|
167 |
def launch_interface():
|
168 |
iface = gr.Interface(
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
)
|
182 |
-
|
183 |
-
iface.launch(
|
184 |
|
185 |
if __name__=="__main__":
|
186 |
launch_interface()
|
|
|
14 |
|
15 |
|
16 |
# initialize client
|
17 |
+
# we could try something larger, I need to check the models
|
18 |
client = InferenceClient(
|
|
|
19 |
"v1olet/v1olet_marcoroni-go-bruins-merge-7B"
|
20 |
)
|
21 |
|
22 |
+
# Load pre-trained tokenizer model (replace with your desired model if you want, but it needs to be small)
|
23 |
+
model_id = "sentence-transformers/all-MiniLM-L6-v2" #small embeddings model
|
24 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
25 |
+
model = AutoModel.from_pretrained(model_id) #cant do this since HF free uses cpu #load_in_4bit=True,
|
26 |
+
|
27 |
+
# Function to calculate cosine similarity between two embeddings
|
28 |
+
def calculate_cosine_similarity(embedding1, embedding2):
|
29 |
+
return cosine_similarity(embedding1, embedding2)[0][0]
|
30 |
|
31 |
# Function to convert text items into embeddings
|
32 |
def get_embeddings(text_items):
|
|
|
39 |
embeddings.append(pooled_output)
|
40 |
return embeddings
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Helper Function to select values with small enough cosine similarity and concatenate them into a string
|
43 |
+
def select_values_with_low_similarity(embeddings, original_values, num_values_to_select, max_similarity):
|
44 |
selected_values = []
|
45 |
selected_indices = set()
|
46 |
|
47 |
while len(selected_values) < num_values_to_select:
|
48 |
+
index1, index2 = random.sample(range(len(embeddings)), 2)
|
49 |
+
embedding1, embedding2 = embeddings[index1], embeddings[index2]
|
50 |
|
51 |
+
if index1 != index2 and calculate_cosine_similarity(embedding1, embedding2) < max_similarity:
|
52 |
if index1 not in selected_indices:
|
53 |
+
selected_values.append(original_values[index1])
|
54 |
selected_indices.add(index1)
|
55 |
if index2 not in selected_indices:
|
56 |
+
selected_values.append(original_values[index2])
|
57 |
selected_indices.add(index2)
|
58 |
|
59 |
# Concatenate the selected values into a single string
|
60 |
selected_string = ', '.join(selected_values)
|
61 |
return selected_string
|
62 |
|
63 |
+
|
64 |
# Convert text items into embeddings
|
65 |
genre_embeddings = get_embeddings(book_genres)
|
66 |
theme_embeddings = get_embeddings(book_themes)
|
67 |
tone_embeddings = get_embeddings(writing_tones)
|
68 |
+
#clear memory
|
69 |
del model
|
70 |
+
#torch.cuda.empty_cache()
|
71 |
|
72 |
# helper function to format the prompt appropriately.
|
73 |
# For this creative writing tool, the user doesn't design the prompt itself
|
|
|
75 |
def format_prompt(message, genres, tones, themes):
|
76 |
# pick random ones if user leaves it blank but make sure they aren't opposites
|
77 |
if not genres:
|
78 |
+
selected_genres = select_values_with_low_similarity(genre_embeddings, book_genres, random.randint(3, 5), 0.2) # Adjust threshold as needed
|
79 |
if not tones:
|
80 |
+
selected_tones = select_values_with_low_similarity(tone_embeddings, writing_tones, random.randint(3, 5), 0.2) # Adjust threshold as needed
|
81 |
if not themes:
|
82 |
+
selected_themes = select_values_with_low_similarity(theme_embeddings, book_themes, random.randint(3, 5), 0.2) # Adjust threshold as needed
|
83 |
|
84 |
#Alpaca format since we can't use mixtral on free CPU settings
|
85 |
prompt = "Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n"
|
|
|
92 |
|
93 |
|
94 |
# main function
|
95 |
+
def generate(genres, themes, tones, system_prompt, temperature=1.25, max_new_tokens=512, top_p=0.95, repetition_penalty=1.15,):
|
96 |
# check the temperature value, should not be too low, and make sure the values are floats
|
97 |
temperature = float(temperature)
|
98 |
if temperature < 1e-2:
|
|
|
110 |
)
|
111 |
|
112 |
formatted_prompt = format_prompt(f"{system_prompt}, '' ", genres, tones, themes)
|
113 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False, load_in_4bit=True, use_flash_attention_2=True)
|
114 |
output = ""
|
115 |
|
116 |
for response in stream:
|
|
|
165 |
|
166 |
def launch_interface():
|
167 |
iface = gr.Interface(
|
168 |
+
fn=generate,
|
169 |
+
inputs=[
|
170 |
+
gr.Textbox("", label="Book Genres (comma-separated, or leave blank!)"),
|
171 |
+
gr.Textbox("", label="Book Themes (comma-separated, or leave blank!)"),
|
172 |
+
gr.Textbox("", label="Writing Tone (comma-separated, or leave blank!)"),
|
173 |
+
],
|
174 |
+
#additional_inputs=additional_inputs,
|
175 |
+
outputs="text",
|
176 |
+
live=False,
|
177 |
+
title="Novel Title and Summary Generator",
|
178 |
+
description='A fun creative writing tool, designed for when I have writer\'s block. Use it to practice building worlds, characters, scenes, etc. Write chapter 1, or a plot outline.' ,
|
179 |
+
theme='ParityError/Interstellar', #)
|
180 |
+
additional_inputs=additional_inputs)
|
181 |
+
|
182 |
+
iface.queue().launch(debug=True)
|
183 |
|
184 |
if __name__=="__main__":
|
185 |
launch_interface()
|