randeom commited on
Commit
74e0f7a
·
verified ·
1 Parent(s): 740a7c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -3
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  from huggingface_hub import InferenceClient
 
3
  import re
4
 
5
  # Load custom CSS
@@ -7,7 +8,8 @@ with open('style.css') as f:
7
  st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
8
 
9
  # Initialize the HuggingFace Inference Client
10
- client = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.1")
 
11
 
12
  def format_prompt_for_description(name, hair_color, personality, outfit_style, hobbies, favorite_food, background_story):
13
  prompt = f"Create a waifu character named {name} with {hair_color} hair, a {personality} personality, and wearing a {outfit_style}. "
@@ -34,7 +36,7 @@ def generate_text(prompt, temperature=0.9, max_new_tokens=512, top_p=0.95, repet
34
  seed=42,
35
  )
36
  try:
37
- stream = client.text_generation(prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
38
  output = ""
39
  for response in stream:
40
  output += response.token.text
@@ -43,6 +45,35 @@ def generate_text(prompt, temperature=0.9, max_new_tokens=512, top_p=0.95, repet
43
  st.error(f"Error generating text: {e}")
44
  return ""
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  def main():
47
  st.title("Enhanced Waifu Character Generator")
48
 
@@ -66,11 +97,13 @@ def main():
66
  top_p = st.slider("Top-p (nucleus sampling)", 0.0, 1.0, 0.95, step=0.05)
67
  repetition_penalty = st.slider("Repetition penalty", 1.0, 2.0, 1.0, step=0.05)
68
 
69
- # Initialize session state for generated text
70
  if "character_description" not in st.session_state:
71
  st.session_state.character_description = ""
72
  if "image_prompt" not in st.session_state:
73
  st.session_state.image_prompt = ""
 
 
74
 
75
  # Generate button
76
  if st.button("Generate Waifu"):
@@ -84,6 +117,9 @@ def main():
84
  # Generate image prompt
85
  st.session_state.image_prompt = generate_text(image_prompt, temperature, max_new_tokens, top_p, repetition_penalty)
86
 
 
 
 
87
  st.success("Waifu character generated!")
88
 
89
  # Display the generated character and image prompt
@@ -93,6 +129,9 @@ def main():
93
  if st.session_state.image_prompt:
94
  st.subheader("Image Prompt")
95
  st.write(st.session_state.image_prompt)
 
 
 
96
 
97
  if __name__ == "__main__":
98
  main()
 
1
  import streamlit as st
2
  from huggingface_hub import InferenceClient
3
+ from gradio_client import Client
4
  import re
5
 
6
  # Load custom CSS
 
8
  st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
9
 
10
  # Initialize the HuggingFace Inference Client
11
+ text_client = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.1")
12
+ image_client = Client("Boboiazumi/animagine-xl-3.1")
13
 
14
  def format_prompt_for_description(name, hair_color, personality, outfit_style, hobbies, favorite_food, background_story):
15
  prompt = f"Create a waifu character named {name} with {hair_color} hair, a {personality} personality, and wearing a {outfit_style}. "
 
36
  seed=42,
37
  )
38
  try:
39
+ stream = text_client.text_generation(prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
40
  output = ""
41
  for response in stream:
42
  output += response.token.text
 
45
  st.error(f"Error generating text: {e}")
46
  return ""
47
 
48
+ def generate_image(prompt):
49
+ try:
50
+ result = image_client.predict(
51
+ prompt=prompt,
52
+ negative_prompt="",
53
+ seed=0,
54
+ custom_width=1024,
55
+ custom_height=1024,
56
+ guidance_scale=7.0,
57
+ num_inference_steps=28,
58
+ sampler="Euler a",
59
+ aspect_ratio_selector="896 x 1152",
60
+ style_selector="(None)",
61
+ quality_selector="Standard v3.1",
62
+ use_upscaler=False,
63
+ upscaler_strength=0.55,
64
+ upscale_by=1.5,
65
+ add_quality_tags=True,
66
+ isImg2Img=False,
67
+ img_path=None,
68
+ img2img_strength=0.65,
69
+ api_name="/run"
70
+ )
71
+ return result[0]['image']
72
+ except Exception as e:
73
+ st.error(f"Error generating image: {e}")
74
+ st.write("Full error details:", e)
75
+ return None
76
+
77
  def main():
78
  st.title("Enhanced Waifu Character Generator")
79
 
 
97
  top_p = st.slider("Top-p (nucleus sampling)", 0.0, 1.0, 0.95, step=0.05)
98
  repetition_penalty = st.slider("Repetition penalty", 1.0, 2.0, 1.0, step=0.05)
99
 
100
+ # Initialize session state for generated text and image prompt
101
  if "character_description" not in st.session_state:
102
  st.session_state.character_description = ""
103
  if "image_prompt" not in st.session_state:
104
  st.session_state.image_prompt = ""
105
+ if "image_path" not in st.session_state:
106
+ st.session_state.image_path = ""
107
 
108
  # Generate button
109
  if st.button("Generate Waifu"):
 
117
  # Generate image prompt
118
  st.session_state.image_prompt = generate_text(image_prompt, temperature, max_new_tokens, top_p, repetition_penalty)
119
 
120
+ # Generate image from image prompt
121
+ st.session_state.image_path = generate_image(st.session_state.image_prompt)
122
+
123
  st.success("Waifu character generated!")
124
 
125
  # Display the generated character and image prompt
 
129
  if st.session_state.image_prompt:
130
  st.subheader("Image Prompt")
131
  st.write(st.session_state.image_prompt)
132
+ if st.session_state.image_path:
133
+ st.subheader("Generated Image")
134
+ st.image(st.session_state.image_path, caption="Generated Waifu Image")
135
 
136
  if __name__ == "__main__":
137
  main()