Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,52 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
5 |
-
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
|
6 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Streamlit UI
|
13 |
st.title("Waifu Character Generator")
|
@@ -17,13 +56,20 @@ name = st.text_input("Name of the Waifu")
|
|
17 |
hair_color = st.selectbox("Hair Color", ["Blonde", "Brunette", "Red", "Black", "Blue", "Pink"])
|
18 |
personality = st.selectbox("Personality", ["Tsundere", "Yandere", "Kuudere", "Dandere", "Genki", "Normal"])
|
19 |
outfit_style = st.selectbox("Outfit Style", ["School Uniform", "Maid Outfit", "Casual", "Kimono", "Gothic Lolita"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Generate button
|
22 |
if st.button("Generate Waifu"):
|
23 |
-
|
24 |
prompt = f"Create a waifu character named {name} with {hair_color} hair, a {personality} personality, and wearing a {outfit_style}."
|
25 |
-
|
26 |
|
27 |
# Display the generated character
|
28 |
st.subheader("Generated Waifu Character")
|
29 |
-
st.write(
|
|
|
1 |
import streamlit as st
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
|
5 |
+
client = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.1")
|
|
|
|
|
|
|
6 |
|
7 |
+
def format_prompt(message, history, system_prompt=""):
|
8 |
+
prompt = "<s>"
|
9 |
+
if system_prompt:
|
10 |
+
prompt += f"[SYS] {system_prompt} [/SYS] "
|
11 |
+
for user_prompt, bot_response in history:
|
12 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
13 |
+
prompt += f" {bot_response}</s> "
|
14 |
+
prompt += f"[INST] {message} [/INST]"
|
15 |
+
return prompt
|
16 |
+
|
17 |
+
def extract_filename_and_code(bot_response):
|
18 |
+
try:
|
19 |
+
with open("default.py", 'w') as file:
|
20 |
+
file.write(bot_response)
|
21 |
+
st.write("Successfully wrote to default.py!")
|
22 |
+
except Exception as e:
|
23 |
+
st.write(f"Error writing to file: {e}")
|
24 |
+
|
25 |
+
def generate(prompt, history, system_prompt="", temperature=0.9, max_new_tokens=512, top_p=0.95, repetition_penalty=1.0):
|
26 |
+
temperature = float(temperature)
|
27 |
+
if temperature < 1e-2:
|
28 |
+
temperature = 1e-2
|
29 |
+
top_p = float(top_p)
|
30 |
+
|
31 |
+
generate_kwargs = dict(
|
32 |
+
temperature=temperature,
|
33 |
+
max_new_tokens=max_new_tokens,
|
34 |
+
top_p=top_p,
|
35 |
+
repetition_penalty=repetition_penalty,
|
36 |
+
do_sample=True,
|
37 |
+
seed=42,
|
38 |
+
)
|
39 |
+
|
40 |
+
formatted_prompt = format_prompt(prompt, history, system_prompt)
|
41 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
42 |
+
output = ""
|
43 |
+
|
44 |
+
for response in stream:
|
45 |
+
output += response.token.text
|
46 |
+
extract_filename_and_code(output)
|
47 |
+
yield output
|
48 |
+
|
49 |
+
return output
|
50 |
|
51 |
# Streamlit UI
|
52 |
st.title("Waifu Character Generator")
|
|
|
56 |
hair_color = st.selectbox("Hair Color", ["Blonde", "Brunette", "Red", "Black", "Blue", "Pink"])
|
57 |
personality = st.selectbox("Personality", ["Tsundere", "Yandere", "Kuudere", "Dandere", "Genki", "Normal"])
|
58 |
outfit_style = st.selectbox("Outfit Style", ["School Uniform", "Maid Outfit", "Casual", "Kimono", "Gothic Lolita"])
|
59 |
+
system_prompt = st.text_input("Optional System Prompt", "")
|
60 |
+
|
61 |
+
# Advanced settings
|
62 |
+
temperature = st.slider("Temperature", 0.0, 1.0, 0.9, step=0.05)
|
63 |
+
max_new_tokens = st.slider("Max new tokens", 0, 8192, 512, step=64)
|
64 |
+
top_p = st.slider("Top-p (nucleus sampling)", 0.0, 1.0, 0.95, step=0.05)
|
65 |
+
repetition_penalty = st.slider("Repetition penalty", 1.0, 2.0, 1.0, step=0.05)
|
66 |
|
67 |
# Generate button
|
68 |
if st.button("Generate Waifu"):
|
69 |
+
history = []
|
70 |
prompt = f"Create a waifu character named {name} with {hair_color} hair, a {personality} personality, and wearing a {outfit_style}."
|
71 |
+
output = generate(prompt, history, system_prompt, temperature, max_new_tokens, top_p, repetition_penalty)
|
72 |
|
73 |
# Display the generated character
|
74 |
st.subheader("Generated Waifu Character")
|
75 |
+
st.write(next(output))
|