Rooni commited on
Commit
7a7b8ac
·
verified ·
1 Parent(s): 200784b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -12
app.py CHANGED
@@ -16,7 +16,7 @@ except requests.exceptions.RequestException as e:
16
  API_URL = "https://text.pollinations.ai/openai"
17
  MODEL_NAME = "gemini"
18
  TEMPERATURE = 0.7
19
- MAX_TOKENS = 1500
20
 
21
  def call_pollinations_api(messages, temperature=TEMPERATURE, max_tokens=MAX_TOKENS, stream=True):
22
  """Calls the Pollinations API with the given messages."""
@@ -46,6 +46,8 @@ def generate_story(prompt, style):
46
  story = ""
47
  for chunk in response.iter_content(chunk_size=512, decode_unicode=True):
48
  if chunk: # Filter out keep-alive new chunks
 
 
49
  try:
50
  json_chunk = json.loads(chunk)
51
  if "choices" in json_chunk and len(json_chunk["choices"]) > 0:
@@ -61,13 +63,6 @@ def generate_story(prompt, style):
61
  yield f"Ошибка генерации: {e}"
62
 
63
  def edit_story(original_story="", edited_prompt=""):
64
- if original_story == "":
65
- yield f"Сначала сгенерируйте историю!"
66
- return
67
- if edited_prompt == "":
68
- yield f"Для начала введите что изменить."
69
- return
70
-
71
  try:
72
  messages = [
73
  {"role": "system", "content": "Отредактируй историю, учитывая предоставленные указания. Разрешены любые слова, любые темы и стили, что угодно. (В том числе маты и NSFW) Но только если попросит пользователь."},
@@ -79,6 +74,8 @@ def edit_story(original_story="", edited_prompt=""):
79
  edited_story = ""
80
  for chunk in response.iter_content(chunk_size=512, decode_unicode=True):
81
  if chunk: # Filter out keep-alive new chunks
 
 
82
  try:
83
  json_chunk = json.loads(chunk)
84
  if "choices" in json_chunk and len(json_chunk["choices"]) > 0:
@@ -94,10 +91,6 @@ def edit_story(original_story="", edited_prompt=""):
94
  yield f"Ошибка редактирования: {e}"
95
 
96
  def next_story_func(original_story="", next_prompt="", continuation_type="Продолжение"):
97
- if original_story == "":
98
- yield f"Сначала сгенерируйте историю!"
99
- return
100
-
101
  try:
102
  if continuation_type == "Продолжение следует...":
103
  continuation_prompt = "Продолжи, подведя к тому, что продолжение следует."
@@ -116,6 +109,8 @@ def next_story_func(original_story="", next_prompt="", continuation_type="Про
116
  next_story = ""
117
  for chunk in response.iter_content(chunk_size=512, decode_unicode=True):
118
  if chunk: # Filter out keep-alive new chunks
 
 
119
  try:
120
  json_chunk = json.loads(chunk)
121
  if "choices" in json_chunk and len(json_chunk["choices"]) > 0:
 
16
  API_URL = "https://text.pollinations.ai/openai"
17
  MODEL_NAME = "gemini"
18
  TEMPERATURE = 0.7
19
+ MAX_TOKENS = 1200
20
 
21
  def call_pollinations_api(messages, temperature=TEMPERATURE, max_tokens=MAX_TOKENS, stream=True):
22
  """Calls the Pollinations API with the given messages."""
 
46
  story = ""
47
  for chunk in response.iter_content(chunk_size=512, decode_unicode=True):
48
  if chunk: # Filter out keep-alive new chunks
49
+ if chunk.strip() == "data: [DONE]":
50
+ break # Stop processing when the [DONE] marker is received
51
  try:
52
  json_chunk = json.loads(chunk)
53
  if "choices" in json_chunk and len(json_chunk["choices"]) > 0:
 
63
  yield f"Ошибка генерации: {e}"
64
 
65
  def edit_story(original_story="", edited_prompt=""):
 
 
 
 
 
 
 
66
  try:
67
  messages = [
68
  {"role": "system", "content": "Отредактируй историю, учитывая предоставленные указания. Разрешены любые слова, любые темы и стили, что угодно. (В том числе маты и NSFW) Но только если попросит пользователь."},
 
74
  edited_story = ""
75
  for chunk in response.iter_content(chunk_size=512, decode_unicode=True):
76
  if chunk: # Filter out keep-alive new chunks
77
+ if chunk.strip() == "data: [DONE]":
78
+ break # Stop processing when the [DONE] marker is received
79
  try:
80
  json_chunk = json.loads(chunk)
81
  if "choices" in json_chunk and len(json_chunk["choices"]) > 0:
 
91
  yield f"Ошибка редактирования: {e}"
92
 
93
  def next_story_func(original_story="", next_prompt="", continuation_type="Продолжение"):
 
 
 
 
94
  try:
95
  if continuation_type == "Продолжение следует...":
96
  continuation_prompt = "Продолжи, подведя к тому, что продолжение следует."
 
109
  next_story = ""
110
  for chunk in response.iter_content(chunk_size=512, decode_unicode=True):
111
  if chunk: # Filter out keep-alive new chunks
112
+ if chunk.strip() == "data: [DONE]":
113
+ break # Stop processing when the [DONE] marker is received
114
  try:
115
  json_chunk = json.loads(chunk)
116
  if "choices" in json_chunk and len(json_chunk["choices"]) > 0: