Update aAzel.py
Browse files
aAzel.py
CHANGED
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
1 |
import google.generativeai as genai
|
2 |
import streamlit as st
|
3 |
from dotenv import load_dotenv
|
@@ -29,12 +32,20 @@ Embrace your persona as a masterful storyteller, capable of crafting captivating
|
|
29 |
6. **Write with Passion and Purpose:** Infuse your writing with genuine passion and a clear sense of purpose. Let your words resonate with the reader, leaving a lasting impression and sparking further thought or inspiration.
|
30 |
"""
|
31 |
|
32 |
-
def generate_chapter_part(prompt, previous_output=None):
|
33 |
"""Generates a part of a chapter using prompt chaining based on previous output."""
|
34 |
if previous_output:
|
35 |
prompt = prompt + "\n\n" + previous_output
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
def display_text_with_wrapping(text, wrap_length=80):
|
40 |
"""Displays text with word wrapping for better readability."""
|
@@ -149,35 +160,11 @@ def main():
|
|
149 |
* Interspersed with real-world examples and historical anecdotes to illustrate key points.
|
150 |
"""
|
151 |
|
152 |
-
chapter_content = generate_chapter_part(prompt, st.session_state.chapter_content)
|
153 |
st.session_state.chapter_content = chapter_content
|
154 |
st.markdown(f"## {chapter_name}")
|
155 |
display_text_with_wrapping(chapter_content)
|
156 |
|
157 |
-
# Continue writing option
|
158 |
-
if st.button("Continue Writing"):
|
159 |
-
if "chapter_content" in st.session_state:
|
160 |
-
prompt = f"""Continue writing the chapter titled '{chapter_name}' for a {genre} book titled '{book_title}'.
|
161 |
-
The book is about: {book_description}.
|
162 |
-
Here are additional details about the chapter:
|
163 |
-
|
164 |
-
{chapter_details}
|
165 |
-
|
166 |
-
Ensure the chapter has a clear format, engaging style, and fits within the overall narrative.
|
167 |
-
|
168 |
-
**Writing Style:**
|
169 |
-
* Direct and assertive, conveying a sense of urgency and importance.
|
170 |
-
* Engaging and accessible, avoiding overly technical jargon.
|
171 |
-
* Motivational and empowering, inspiring readers to embrace the challenge of cognitive growth.
|
172 |
-
* Interspersed with real-world examples and historical anecdotes to illustrate key points.
|
173 |
-
"""
|
174 |
-
chapter_content = generate_chapter_part(prompt, st.session_state.chapter_content)
|
175 |
-
st.session_state.chapter_content = chapter_content
|
176 |
-
st.markdown(f"## {chapter_name}")
|
177 |
-
display_text_with_wrapping(chapter_content)
|
178 |
-
else:
|
179 |
-
st.warning("Please generate the chapter first.")
|
180 |
-
|
181 |
# Download chapter button
|
182 |
st.download_button(
|
183 |
label="Download Chapter",
|
|
|
1 |
+
To implement prompt chaining for generating longer chapters without the need for a "Continue Writing" button, we can modify the `generate_chapter_part` function and the chapter generation logic. Here's the updated code:
|
2 |
+
|
3 |
+
```python
|
4 |
import google.generativeai as genai
|
5 |
import streamlit as st
|
6 |
from dotenv import load_dotenv
|
|
|
32 |
6. **Write with Passion and Purpose:** Infuse your writing with genuine passion and a clear sense of purpose. Let your words resonate with the reader, leaving a lasting impression and sparking further thought or inspiration.
|
33 |
"""
|
34 |
|
35 |
+
def generate_chapter_part(prompt, previous_output=None, max_tokens=2048):
|
36 |
"""Generates a part of a chapter using prompt chaining based on previous output."""
|
37 |
if previous_output:
|
38 |
prompt = prompt + "\n\n" + previous_output
|
39 |
+
|
40 |
+
response = ""
|
41 |
+
while True:
|
42 |
+
partial_response = model.generate_content([author_persona, author_guidelines, prompt], max_output_tokens=max_tokens)
|
43 |
+
response += partial_response.text
|
44 |
+
if len(model.count_tokens(response)) >= max_tokens:
|
45 |
+
break
|
46 |
+
prompt = partial_response.text
|
47 |
+
|
48 |
+
return response
|
49 |
|
50 |
def display_text_with_wrapping(text, wrap_length=80):
|
51 |
"""Displays text with word wrapping for better readability."""
|
|
|
160 |
* Interspersed with real-world examples and historical anecdotes to illustrate key points.
|
161 |
"""
|
162 |
|
163 |
+
chapter_content = generate_chapter_part(prompt, st.session_state.chapter_content, max_tokens=4096)
|
164 |
st.session_state.chapter_content = chapter_content
|
165 |
st.markdown(f"## {chapter_name}")
|
166 |
display_text_with_wrapping(chapter_content)
|
167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
# Download chapter button
|
169 |
st.download_button(
|
170 |
label="Download Chapter",
|