Spaces:
Sleeping
Sleeping
Update app.py
Browse fileschanged code to use gr interface and vicuna 13B
app.py
CHANGED
@@ -1,90 +1,122 @@
|
|
1 |
# imports
|
2 |
import gradio as gr
|
3 |
-
from
|
4 |
-
import requests
|
5 |
-
import random
|
6 |
-
import torch
|
7 |
-
import os
|
8 |
|
9 |
# lists for random gen, not the best format here but it runs fine
|
10 |
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","Middle Grade","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"]
|
11 |
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"]
|
12 |
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"]
|
13 |
|
14 |
-
model_id = "Nexusflow/NexusRaven-V2-13B"
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
# Load the tokenizer
|
20 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
21 |
-
API_KEY = HF_TOKEN = os.environ.get("API_KEY")
|
22 |
-
|
23 |
-
#main function
|
24 |
-
def generate_novel_title_and_summary(genres, moods, themes, temperature=1.5, max_length=512, context_length=1024):
|
25 |
|
|
|
26 |
if not genres:
|
27 |
genres = ", ".join(random.sample(book_genres, random.randint(3, 5)))
|
28 |
if not moods:
|
29 |
moods = ", ".join(random.sample(writing_tones, random.randint(3, 5)))
|
30 |
if not themes:
|
31 |
themes = ", ".join(random.sample(book_themes, random.randint(3, 5)))
|
32 |
-
|
33 |
prompt = f"Write a simple novel title and interesting summary for a book that falls under the following \n genres: {genres}, \n themes: {themes}, \n and moods: {moods}. \n Title:"
|
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 |
def launch_interface():
|
71 |
iface = gr.Interface(
|
72 |
-
fn=
|
73 |
inputs=[
|
74 |
gr.Textbox("", label="Book Genres (comma-separated, or leave blank!)"),
|
75 |
gr.Textbox("", label="Book Themes (comma-separated, or leave blank!)"),
|
76 |
gr.Textbox("", label="Writing Tone (comma-separated, or leave blank!)"),
|
77 |
gr.Slider(0.1, 10.0, 1.3, label="Temperature (Creativity)"),
|
78 |
-
gr.Slider(32, 512, 300, label="Max Token Length (Length of output)"),
|
79 |
-
gr.Slider(32, 1024, 256, label="Context Length (Memory)")
|
80 |
],
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
85 |
)
|
86 |
|
87 |
-
|
88 |
|
89 |
if __name__ == "__main__":
|
90 |
launch_interface()
|
|
|
1 |
# imports
|
2 |
import gradio as gr
|
3 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# lists for random gen, not the best format here but it runs fine
|
6 |
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","Middle Grade","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"]
|
7 |
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"]
|
8 |
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"]
|
9 |
|
|
|
10 |
|
11 |
+
client = InferenceClient(
|
12 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
13 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
def format_prompt(message, history, genres, moods, themes):
|
16 |
if not genres:
|
17 |
genres = ", ".join(random.sample(book_genres, random.randint(3, 5)))
|
18 |
if not moods:
|
19 |
moods = ", ".join(random.sample(writing_tones, random.randint(3, 5)))
|
20 |
if not themes:
|
21 |
themes = ", ".join(random.sample(book_themes, random.randint(3, 5)))
|
22 |
+
|
23 |
prompt = f"Write a simple novel title and interesting summary for a book that falls under the following \n genres: {genres}, \n themes: {themes}, \n and moods: {moods}. \n Title:"
|
24 |
+
for user_prompt, bot_response in history:
|
25 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
26 |
+
prompt += f" {bot_response}</s> "
|
27 |
+
prompt += f"[INST] {message} [/INST]"
|
28 |
+
return prompt
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
def generate(genres, moods, themes, history, system_prompt, temperature=1.25, max_new_tokens=256, top_p=0.95, repetition_penalty=1.15,):
|
33 |
+
|
34 |
+
temperature = float(temperature)
|
35 |
+
if temperature < 1e-2:
|
36 |
+
temperature = 1e-2
|
37 |
+
top_p = float(top_p)
|
38 |
+
|
39 |
+
generate_kwargs = dict(
|
40 |
+
temperature=temperature,
|
41 |
+
max_new_tokens=max_new_tokens,
|
42 |
+
top_p=top_p,
|
43 |
+
repetition_penalty=repetition_penalty,
|
44 |
+
do_sample=True,
|
45 |
+
seed=42,
|
46 |
+
)
|
47 |
+
|
48 |
+
formatted_prompt = format_prompt(f"{system_prompt}, '' ", history, genres, moods, themes)
|
49 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
50 |
+
output = ""
|
51 |
+
|
52 |
+
for response in stream:
|
53 |
+
output += response.token.text
|
54 |
+
yield output
|
55 |
+
return output
|
56 |
+
|
57 |
+
|
58 |
+
additional_inputs=[
|
59 |
+
gr.Textbox(
|
60 |
+
label="System Prompt",
|
61 |
+
max_lines=1,
|
62 |
+
interactive=True,
|
63 |
+
),
|
64 |
+
gr.Slider(
|
65 |
+
label="Temperature",
|
66 |
+
value=0.9,
|
67 |
+
minimum=0.0,
|
68 |
+
maximum=1.0,
|
69 |
+
step=0.05,
|
70 |
+
interactive=True,
|
71 |
+
info="Higher values produce more diverse outputs",
|
72 |
+
),
|
73 |
+
gr.Slider(
|
74 |
+
label="Max new tokens",
|
75 |
+
value=256,
|
76 |
+
minimum=0,
|
77 |
+
maximum=1048,
|
78 |
+
step=64,
|
79 |
+
interactive=True,
|
80 |
+
info="The maximum numbers of new tokens",
|
81 |
+
),
|
82 |
+
gr.Slider(
|
83 |
+
label="Top-p (nucleus sampling)",
|
84 |
+
value=0.90,
|
85 |
+
minimum=0.0,
|
86 |
+
maximum=1,
|
87 |
+
step=0.05,
|
88 |
+
interactive=True,
|
89 |
+
info="Higher values sample more low-probability tokens",
|
90 |
+
),
|
91 |
+
gr.Slider(
|
92 |
+
label="Repetition penalty",
|
93 |
+
value=1.2,
|
94 |
+
minimum=1.0,
|
95 |
+
maximum=2.0,
|
96 |
+
step=0.05,
|
97 |
+
interactive=True,
|
98 |
+
info="Penalize repeated tokens",
|
99 |
+
)
|
100 |
+
]
|
101 |
|
102 |
def launch_interface():
|
103 |
iface = gr.Interface(
|
104 |
+
fn=generate,
|
105 |
inputs=[
|
106 |
gr.Textbox("", label="Book Genres (comma-separated, or leave blank!)"),
|
107 |
gr.Textbox("", label="Book Themes (comma-separated, or leave blank!)"),
|
108 |
gr.Textbox("", label="Writing Tone (comma-separated, or leave blank!)"),
|
109 |
gr.Slider(0.1, 10.0, 1.3, label="Temperature (Creativity)"),
|
|
|
|
|
110 |
],
|
111 |
+
additional_inputs=additional_inputs,
|
112 |
+
outputs="text",
|
113 |
+
live=False,
|
114 |
+
title="Novel Title and Summary Generator",
|
115 |
+
layout = 'panel'
|
116 |
+
theme='ParityError/Interstellar',
|
117 |
)
|
118 |
|
119 |
+
iface.launch(show_api=False)
|
120 |
|
121 |
if __name__ == "__main__":
|
122 |
launch_interface()
|