Spaces:
Sleeping
Sleeping
Update app.py
Browse filesforgot to put main in quotes, added code to create embeddings are select genres. themes, and tones that have a closer cosine similarity to avoid picking opposites from each category
app.py
CHANGED
@@ -2,28 +2,83 @@
|
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
4 |
import random
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# lists for random gen, not the best format here but it runs fine, might add these as json or txt files later
|
7 |
book_genres = ["Adventure", "Romance","Mystery", "Science Fiction","Fantasy","Thriller","Horror","Historical Fiction","Biography","Autobiography","Self-Help","Non-Fiction","Science","Cooking","Travel","Dystopian","Young Adult","Children's","Poetry","Classic","Graphic Novel","Humor","Crime","Western","Memoir","Religion","Psychology","Philosophy","Business","Finance","Parenting","Health","Fitness","Art","Music","Sports","Politics","Education","Technology","Science Fiction Fantasy","Steampunk","Drama","Historical Non-Fiction","Biographical Fiction","Mythology","Anthology","Short Stories","Essays","Fairy Tales","Magic Realism","True Crime","Satire","Romantic Suspense","Paranormal","Urban Fantasy","War","Epic Fantasy","Contemporary Fiction","Legal Thriller","Espionage","Post-Apocalyptic","Time Travel","Cultural","Medical","Environmental","Artificial Intelligence","Cyberpunk","Space Opera","Alternate History","Historical Romance","Science Fiction Romance","Young Adult Fantasy","Adventure Fantasy","Superhero","Graphic Memoir","Travel Memoir","Political Thriller","Economic","Psychological Thriller","Nature","True Adventure","Historical Mystery","Social Science","Science Biography","Space Exploration","Pop Culture","Art History","Culinary","Nature Writing","Family Drama","Classic Literature","Cultural History","Political Science","Economics","Essays and Criticism","Art Criticism","Criminal Justice","Historical Biography","Personal Development","Cookbook","Fashion","Crafts and Hobbies","Memoir","Essays","Graphic Non-Fiction", "Fantasy Romance"]
|
8 |
book_themes = ["Love and Relationships","Friendship","Family","Coming of Age","Identity and Self-discovery","Adventure and Exploration","Mystery and Intrigue","Science and Technology","Fantasy Worlds","Historical Events","War and Conflict","Survival","Good vs. Evil","Justice and Morality","Revenge","Betrayal","Hope and Resilience","Isolation and Loneliness","Social Justice","Environmental Conservation","Political Corruption","Human Rights","Dystopia","Utopia","Alien Encounters","Time Travel","Art and Creativity","Death and Mortality","Cultural Identity","Personal Growth","Addiction","Education and Knowledge","Freedom and Liberation","Equality and Inequality","Society and Class","Legacy and Inheritance","Religion and Spirituality","Grief and Loss","Ambition","Transformation","Humor and Satire","Survival of the Fittest","Dreams and Aspirations","Change and Adaptation","Forgiveness","Nature and the Environment","Exploration of the Unknown","Conflict Resolution","Fate and Destiny","Artificial Intelligence","Cybersecurity","Space Exploration","Parallel Universes","Economic Struggles","Social Media and Technology","Innovation and Invention","Psychological Thrills","Philosophical Contemplation","Ancient Mythology","Modern Mythology","Epic Journeys","The Power of Imagination","Unrequited Love","Secrets and Hidden Truths","Warriors and Heroes","Surviving Adversity","Dreams and Nightmares","Rivalry and Competition","Alien Worlds","Conspiracy","Apocalyptic Scenarios","Conformity vs. Individuality","Legacy and Heritage","Nature vs. Nurture","Moral Dilemmas","Adventure and Discovery","Journey of Self-Discovery","Unlikely Friendships","Struggle for Power","Exploration of Fear","The Supernatural","Cultural Clashes","Identity Crisis","The Quest for Knowledge","The Human Condition","Hidden Agendas","Escapism","The Pursuit of Happiness","Redemption","Rebellion","Feminism and Gender Issues","Exploration of Dreams","Innocence vs. Experience","Chaos and Order","Exploration of Evil"]
|
9 |
writing_tones = ["Formal","Informal","Humorous","Serious","Sarcastic","Satirical","Melancholic","Optimistic","Pessimistic","Cynical","Hopeful","Lighthearted","Dark","Gothic","Whimsical","Mysterious","Eerie","Solemn","Playful","Thoughtful","Reflective","Ironic","Sensual","Nostalgic","Surreal","Dreamy","Awe-Inspiring","Introspective","Confessional","Dramatic","Exuberant","Melodramatic","Hypnotic","Inspirational","Tongue-in-Cheek","Witty","Calm","Passionate","Detached","Frightening","Intense","Calm","Suspenseful","Brave","Desperate","Eloquent","Vivid","Casual","Whispering","Eloquent","Bitter","Tragic","Pensive","Frenzied","Melodious","Resolute","Soothing","Brisk","Lyrical","Objective","Factual","Contemplative","Sardonic","Sympathetic","Objective","Sincere","Wistful","Stoic","Empathetic","Matter-of-fact","Sentimental","Sharp","Understated","Exaggerated","Casual","Bombastic","Poetic","Charming","Apologetic","Defensive","Confrontational","Inquisitive","Candid","Reverent","Matter-of-fact","Amusing","Enthusiastic","Questioning","Reproachful","Hopeless","Despondent","Wry","Sulking","Serene","Detached","Confident","Steadfast","Foolish","Impassioned","Indignant","Self-Deprecating","Wandering","Inspiring","Bewildered"]
|
10 |
|
|
|
11 |
# initialize client
|
12 |
client = InferenceClient(
|
13 |
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
14 |
)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
# helper function to format the prompt appropriately.
|
17 |
-
# For this creative writing tool, the user doesn't design the prompt itself
|
|
|
18 |
def format_prompt(message, history, genres, tones, themes):
|
19 |
-
# pick random ones if user leaves it blank
|
20 |
-
# TODO: use NLP or somethign so that there aren't contrasting ideas in the same prompt (ex. western and cyberpunk genres or dark and lighthearted tones)
|
21 |
if not genres:
|
22 |
-
|
23 |
if not tones:
|
24 |
-
|
25 |
if not themes:
|
26 |
-
|
27 |
|
28 |
#BOS token
|
29 |
prompt = '<s>'
|
@@ -125,5 +180,5 @@ def launch_interface():
|
|
125 |
|
126 |
iface.launch(show_api=False)
|
127 |
|
128 |
-
if __name__==__main__:
|
129 |
launch_interface()
|
|
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
4 |
import random
|
5 |
+
# for picking random values from the lists that are not antithetical to each other
|
6 |
+
import torch
|
7 |
+
from transformers import AutoTokenizer, AutoModel
|
8 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
9 |
|
10 |
# lists for random gen, not the best format here but it runs fine, might add these as json or txt files later
|
11 |
book_genres = ["Adventure", "Romance","Mystery", "Science Fiction","Fantasy","Thriller","Horror","Historical Fiction","Biography","Autobiography","Self-Help","Non-Fiction","Science","Cooking","Travel","Dystopian","Young Adult","Children's","Poetry","Classic","Graphic Novel","Humor","Crime","Western","Memoir","Religion","Psychology","Philosophy","Business","Finance","Parenting","Health","Fitness","Art","Music","Sports","Politics","Education","Technology","Science Fiction Fantasy","Steampunk","Drama","Historical Non-Fiction","Biographical Fiction","Mythology","Anthology","Short Stories","Essays","Fairy Tales","Magic Realism","True Crime","Satire","Romantic Suspense","Paranormal","Urban Fantasy","War","Epic Fantasy","Contemporary Fiction","Legal Thriller","Espionage","Post-Apocalyptic","Time Travel","Cultural","Medical","Environmental","Artificial Intelligence","Cyberpunk","Space Opera","Alternate History","Historical Romance","Science Fiction Romance","Young Adult Fantasy","Adventure Fantasy","Superhero","Graphic Memoir","Travel Memoir","Political Thriller","Economic","Psychological Thriller","Nature","True Adventure","Historical Mystery","Social Science","Science Biography","Space Exploration","Pop Culture","Art History","Culinary","Nature Writing","Family Drama","Classic Literature","Cultural History","Political Science","Economics","Essays and Criticism","Art Criticism","Criminal Justice","Historical Biography","Personal Development","Cookbook","Fashion","Crafts and Hobbies","Memoir","Essays","Graphic Non-Fiction", "Fantasy Romance"]
|
12 |
book_themes = ["Love and Relationships","Friendship","Family","Coming of Age","Identity and Self-discovery","Adventure and Exploration","Mystery and Intrigue","Science and Technology","Fantasy Worlds","Historical Events","War and Conflict","Survival","Good vs. Evil","Justice and Morality","Revenge","Betrayal","Hope and Resilience","Isolation and Loneliness","Social Justice","Environmental Conservation","Political Corruption","Human Rights","Dystopia","Utopia","Alien Encounters","Time Travel","Art and Creativity","Death and Mortality","Cultural Identity","Personal Growth","Addiction","Education and Knowledge","Freedom and Liberation","Equality and Inequality","Society and Class","Legacy and Inheritance","Religion and Spirituality","Grief and Loss","Ambition","Transformation","Humor and Satire","Survival of the Fittest","Dreams and Aspirations","Change and Adaptation","Forgiveness","Nature and the Environment","Exploration of the Unknown","Conflict Resolution","Fate and Destiny","Artificial Intelligence","Cybersecurity","Space Exploration","Parallel Universes","Economic Struggles","Social Media and Technology","Innovation and Invention","Psychological Thrills","Philosophical Contemplation","Ancient Mythology","Modern Mythology","Epic Journeys","The Power of Imagination","Unrequited Love","Secrets and Hidden Truths","Warriors and Heroes","Surviving Adversity","Dreams and Nightmares","Rivalry and Competition","Alien Worlds","Conspiracy","Apocalyptic Scenarios","Conformity vs. Individuality","Legacy and Heritage","Nature vs. Nurture","Moral Dilemmas","Adventure and Discovery","Journey of Self-Discovery","Unlikely Friendships","Struggle for Power","Exploration of Fear","The Supernatural","Cultural Clashes","Identity Crisis","The Quest for Knowledge","The Human Condition","Hidden Agendas","Escapism","The Pursuit of Happiness","Redemption","Rebellion","Feminism and Gender Issues","Exploration of Dreams","Innocence vs. Experience","Chaos and Order","Exploration of Evil"]
|
13 |
writing_tones = ["Formal","Informal","Humorous","Serious","Sarcastic","Satirical","Melancholic","Optimistic","Pessimistic","Cynical","Hopeful","Lighthearted","Dark","Gothic","Whimsical","Mysterious","Eerie","Solemn","Playful","Thoughtful","Reflective","Ironic","Sensual","Nostalgic","Surreal","Dreamy","Awe-Inspiring","Introspective","Confessional","Dramatic","Exuberant","Melodramatic","Hypnotic","Inspirational","Tongue-in-Cheek","Witty","Calm","Passionate","Detached","Frightening","Intense","Calm","Suspenseful","Brave","Desperate","Eloquent","Vivid","Casual","Whispering","Eloquent","Bitter","Tragic","Pensive","Frenzied","Melodious","Resolute","Soothing","Brisk","Lyrical","Objective","Factual","Contemplative","Sardonic","Sympathetic","Objective","Sincere","Wistful","Stoic","Empathetic","Matter-of-fact","Sentimental","Sharp","Understated","Exaggerated","Casual","Bombastic","Poetic","Charming","Apologetic","Defensive","Confrontational","Inquisitive","Candid","Reverent","Matter-of-fact","Amusing","Enthusiastic","Questioning","Reproachful","Hopeless","Despondent","Wry","Sulking","Serene","Detached","Confident","Steadfast","Foolish","Impassioned","Indignant","Self-Deprecating","Wandering","Inspiring","Bewildered"]
|
14 |
|
15 |
+
|
16 |
# initialize client
|
17 |
client = InferenceClient(
|
18 |
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
19 |
)
|
20 |
|
21 |
+
# Load pre-trained mixtral tokenizer model (replace with your desired model if you want)
|
22 |
+
model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
24 |
+
model = AutoModel.from_pretrained(model_id)
|
25 |
+
|
26 |
+
# Function to convert text items into embeddings
|
27 |
+
def get_embeddings(text_items):
|
28 |
+
embeddings = []
|
29 |
+
for item in text_items:
|
30 |
+
inputs = tokenizer(item, return_tensors="pt", padding=True, truncation=True)
|
31 |
+
with torch.no_grad():
|
32 |
+
outputs = model(**inputs)
|
33 |
+
pooled_output = outputs['pooler_output']
|
34 |
+
embeddings.append(pooled_output)
|
35 |
+
return embeddings
|
36 |
+
|
37 |
+
# Helper Function to calculate cosine similarity between two embeddings
|
38 |
+
# since we are using multiple variables, and since we want to compare more than just positive/negative ideas,
|
39 |
+
# cosine similarity works better than euclidean distance as a measure of similarity
|
40 |
+
def calculate_cosine_similarity(embedding1, embedding2):
|
41 |
+
return cosine_similarity(embedding1, embedding2)[0][0]
|
42 |
+
|
43 |
+
# Helper Function to select values with small enough cosine similarity and concatenate them into a string
|
44 |
+
def select_values_with_low_similarity(values, num_values_to_select, max_similarity):
|
45 |
+
selected_values = []
|
46 |
+
selected_indices = set()
|
47 |
+
|
48 |
+
while len(selected_values) < num_values_to_select:
|
49 |
+
index1, index2 = random.sample(range(len(values)), 2)
|
50 |
+
item1, item2 = values[index1], values[index2]
|
51 |
+
|
52 |
+
if index1 != index2 and calculate_cosine_similarity(item1, item2) < max_similarity:
|
53 |
+
if index1 not in selected_indices:
|
54 |
+
selected_values.append(item1)
|
55 |
+
selected_indices.add(index1)
|
56 |
+
if index2 not in selected_indices:
|
57 |
+
selected_values.append(item2)
|
58 |
+
selected_indices.add(index2)
|
59 |
+
|
60 |
+
# Concatenate the selected values into a single string
|
61 |
+
selected_string = ' '.join(selected_values)
|
62 |
+
return selected_string
|
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 |
+
|
71 |
# helper function to format the prompt appropriately.
|
72 |
+
# For this creative writing tool, the user doesn't design the prompt itself
|
73 |
+
#but rather genres, tones, & themes of a book to include
|
74 |
def format_prompt(message, history, genres, tones, themes):
|
75 |
+
# pick random ones if user leaves it blank but make sure they aren't opposites
|
|
|
76 |
if not genres:
|
77 |
+
selected_genres = select_values_with_low_similarity(book_genres, random.randint(3, 5), 0.2) # Adjust threshold as needed
|
78 |
if not tones:
|
79 |
+
selected_tones = select_values_with_low_similarity(writing_tones, random.randint(3, 5), 0.2) # Adjust threshold as needed
|
80 |
if not themes:
|
81 |
+
selected_themes = select_values_with_low_similarity(book_themes, random.randint(3, 5), 0.2) # Adjust threshold as needed
|
82 |
|
83 |
#BOS token
|
84 |
prompt = '<s>'
|
|
|
180 |
|
181 |
iface.launch(show_api=False)
|
182 |
|
183 |
+
if __name__=="__main__":
|
184 |
launch_interface()
|