Spaces:
Sleeping
Sleeping
Commit ·
b997c21
1
Parent(s): 4e7673e
Successfully creating the application that allows for story generation based on the given parameters and image generation (lagging) for now due to overloaded image size, will further optimize
Browse files- app.py +37 -5
- requirements.txt +5 -0
app.py
CHANGED
|
@@ -84,13 +84,12 @@ class VisualNovelGenerator:
|
|
| 84 |
layout="wide",
|
| 85 |
initial_sidebar_state="expanded"
|
| 86 |
)
|
| 87 |
-
|
| 88 |
-
def image_api_request(prompt: str) -> str:
|
| 89 |
"""Send a request to generate an image based on the prompt"""
|
| 90 |
-
image_api_key = st.secrets.get("IMG_API_KEY") or st.session_state.get("
|
| 91 |
client = InferenceClient("black-forest-labs/FLUX.1-dev", token=image_api_key)
|
| 92 |
try:
|
| 93 |
-
image = client.text_to_image(prompt)
|
| 94 |
return image
|
| 95 |
except Exception as e:
|
| 96 |
st.error(f"Image Generation Error: {str(e)}")
|
|
@@ -180,7 +179,6 @@ class VisualNovelGenerator:
|
|
| 180 |
for i in range(num_characters):
|
| 181 |
with st.expander(f"Character {i+1}"):
|
| 182 |
char_name = st.text_input("Name", key=f"char_name_{i}")
|
| 183 |
-
|
| 184 |
# Let's generate character details using AI for each character
|
| 185 |
if char_name:
|
| 186 |
prompt = f"""Return a JSON object for a character named {char_name} in this format exactly:
|
|
@@ -206,6 +204,28 @@ class VisualNovelGenerator:
|
|
| 206 |
char_info = json.loads(json_str)
|
| 207 |
characters[char_name] = char_info
|
| 208 |
st.success(f"Character {char_name} details generated!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
except:
|
| 210 |
st.error(f"Error generating details for {char_name}")
|
| 211 |
|
|
@@ -324,6 +344,18 @@ class VisualNovelGenerator:
|
|
| 324 |
"choices": story_data["dialogue_options"],
|
| 325 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 326 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
except Exception as e:
|
| 329 |
st.error(f"Error generating story: {str(e)}")
|
|
|
|
| 84 |
layout="wide",
|
| 85 |
initial_sidebar_state="expanded"
|
| 86 |
)
|
| 87 |
+
def image_api_request(self, prompt: str) -> str:
|
|
|
|
| 88 |
"""Send a request to generate an image based on the prompt"""
|
| 89 |
+
image_api_key = st.secrets.get("IMG_API_KEY") or st.session_state.get("IMG_API_KEY")
|
| 90 |
client = InferenceClient("black-forest-labs/FLUX.1-dev", token=image_api_key)
|
| 91 |
try:
|
| 92 |
+
image = client.text_to_image(prompt, target_size=(512, 512))
|
| 93 |
return image
|
| 94 |
except Exception as e:
|
| 95 |
st.error(f"Image Generation Error: {str(e)}")
|
|
|
|
| 179 |
for i in range(num_characters):
|
| 180 |
with st.expander(f"Character {i+1}"):
|
| 181 |
char_name = st.text_input("Name", key=f"char_name_{i}")
|
|
|
|
| 182 |
# Let's generate character details using AI for each character
|
| 183 |
if char_name:
|
| 184 |
prompt = f"""Return a JSON object for a character named {char_name} in this format exactly:
|
|
|
|
| 204 |
char_info = json.loads(json_str)
|
| 205 |
characters[char_name] = char_info
|
| 206 |
st.success(f"Character {char_name} details generated!")
|
| 207 |
+
|
| 208 |
+
# Remove any non-JSON text before and after the JSON object
|
| 209 |
+
json_start = response.find("{")
|
| 210 |
+
json_end = response.rfind("}") + 1
|
| 211 |
+
if json_start >= 0 and json_end > json_start:
|
| 212 |
+
json_str = response[json_start:json_end]
|
| 213 |
+
char_info = json.loads(json_str)
|
| 214 |
+
characters[char_name] = char_info
|
| 215 |
+
st.success(f"Character {char_name} details generated!")
|
| 216 |
+
|
| 217 |
+
# Generate character portrait
|
| 218 |
+
portrait_prompt = f"Visual novel style portrait of {char_name}: full face, shoulders up, anime style, high quality render"
|
| 219 |
+
# Generate and save character portrait
|
| 220 |
+
portrait_filename = f"portrait_{char_name.lower().replace(' ', '_')}.png"
|
| 221 |
+
if not Path(portrait_filename).exists():
|
| 222 |
+
portrait = self.image_api_request(portrait_prompt)
|
| 223 |
+
if portrait:
|
| 224 |
+
portrait.save(portrait_filename)
|
| 225 |
+
st.image(portrait_filename, caption=f"Portrait of {char_name}")
|
| 226 |
+
else:
|
| 227 |
+
st.image(portrait_filename, caption=f"Portrait of {char_name}")
|
| 228 |
+
|
| 229 |
except:
|
| 230 |
st.error(f"Error generating details for {char_name}")
|
| 231 |
|
|
|
|
| 344 |
"choices": story_data["dialogue_options"],
|
| 345 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 346 |
})
|
| 347 |
+
|
| 348 |
+
# Generate images for the opening scene and characters
|
| 349 |
+
try:
|
| 350 |
+
# Generate scene image
|
| 351 |
+
scene_prompt = f"Visual novel style, {setting} setting: {story_data['opening_scene']}"
|
| 352 |
+
scene_image = self.image_api_request(scene_prompt)
|
| 353 |
+
if scene_image:
|
| 354 |
+
scene_image.save("temp_scene.png")
|
| 355 |
+
st.image("temp_scene.png", caption="Opening Scene")
|
| 356 |
+
|
| 357 |
+
except Exception as e:
|
| 358 |
+
st.warning(f"Image generation failed: {str(e)}")
|
| 359 |
|
| 360 |
except Exception as e:
|
| 361 |
st.error(f"Error generating story: {str(e)}")
|
requirements.txt
CHANGED
|
@@ -1 +1,6 @@
|
|
| 1 |
streamlit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
streamlit
|
| 2 |
+
huggingface_hub
|
| 3 |
+
re
|
| 4 |
+
requests
|
| 5 |
+
json
|
| 6 |
+
openai
|