Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,11 +14,11 @@ except requests.exceptions.RequestException as e:
|
|
14 |
css = " h1{text-align:center}"
|
15 |
|
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=
|
22 |
"""Calls the Pollinations API with the given messages."""
|
23 |
headers = {
|
24 |
"Content-Type": "application/json"
|
@@ -30,7 +30,7 @@ def call_pollinations_api(messages, temperature=TEMPERATURE, max_tokens=MAX_TOKE
|
|
30 |
"max_tokens": max_tokens,
|
31 |
"stream": stream
|
32 |
}
|
33 |
-
response = requests.post(API_URL, headers=headers, data=json.dumps(data)
|
34 |
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
35 |
return response
|
36 |
|
@@ -42,20 +42,10 @@ def generate_story(prompt, style):
|
|
42 |
]
|
43 |
|
44 |
response = call_pollinations_api(messages)
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
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:
|
54 |
-
story += json_chunk["choices"][0]["delta"].get("content", "") or ""
|
55 |
-
yield story
|
56 |
-
except json.JSONDecodeError:
|
57 |
-
print(f"Не удалось декодировать JSON-фрагмент: {chunk}")
|
58 |
-
yield f"Ошибка декодирования JSON: {chunk}"
|
59 |
|
60 |
except requests.exceptions.RequestException as e:
|
61 |
yield f"Ошибка API запроса: {e}"
|
@@ -69,21 +59,11 @@ def edit_story(original_story="", edited_prompt=""):
|
|
69 |
{"role": "user", "content": edited_prompt},
|
70 |
{"role": "user", "content": f"История: ```\n{original_story}\n```"}
|
71 |
]
|
72 |
-
response = call_pollinations_api(messages, max_tokens=32760)
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
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:
|
82 |
-
edited_story += json_chunk["choices"][0]["delta"].get("content", "") or ""
|
83 |
-
yield edited_story
|
84 |
-
except json.JSONDecodeError:
|
85 |
-
print(f"Не удалось декодировать JSON-фрагмент: {chunk}")
|
86 |
-
yield f"Ошибка декодирования JSON: {chunk}"
|
87 |
|
88 |
except requests.exceptions.RequestException as e:
|
89 |
yield f"Ошибка API запроса: {e}"
|
@@ -104,21 +84,11 @@ def next_story_func(original_story="", next_prompt="", continuation_type="Про
|
|
104 |
{"role": "user", "content": continuation_prompt},
|
105 |
{"role": "user", "content": f"История: ```\n{original_story}\n```"}
|
106 |
]
|
107 |
-
response = call_pollinations_api(messages)
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
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:
|
117 |
-
next_story += json_chunk["choices"][0]["delta"].get("content", "") or ""
|
118 |
-
yield next_story
|
119 |
-
except json.JSONDecodeError:
|
120 |
-
print(f"Не удалось декодировать JSON-фрагмент: {chunk}")
|
121 |
-
yield f"Ошибка декодирования JSON: {chunk}"
|
122 |
|
123 |
except requests.exceptions.RequestException as e:
|
124 |
yield f"Ошибка API запроса: {e}"
|
|
|
14 |
css = " h1{text-align:center}"
|
15 |
|
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=False): # stream=False
|
22 |
"""Calls the Pollinations API with the given messages."""
|
23 |
headers = {
|
24 |
"Content-Type": "application/json"
|
|
|
30 |
"max_tokens": max_tokens,
|
31 |
"stream": stream
|
32 |
}
|
33 |
+
response = requests.post(API_URL, headers=headers, data=json.dumps(data))
|
34 |
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
35 |
return response
|
36 |
|
|
|
42 |
]
|
43 |
|
44 |
response = call_pollinations_api(messages)
|
45 |
+
full_response = response.json() # Получаем JSON-ответ целиком
|
46 |
+
print("Полный ответ от API:", json.dumps(full_response, indent=2, ensure_ascii=False)) # Выводим отформатированный JSON
|
47 |
+
story = full_response["choices"][0]["message"]["content"] if "choices" in full_response and len(full_response["choices"]) > 0 else "Ответ не получен или структура ответа не соответствует ожидаемой."
|
48 |
+
yield story
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
except requests.exceptions.RequestException as e:
|
51 |
yield f"Ошибка API запроса: {e}"
|
|
|
59 |
{"role": "user", "content": edited_prompt},
|
60 |
{"role": "user", "content": f"История: ```\n{original_story}\n```"}
|
61 |
]
|
62 |
+
response = call_pollinations_api(messages, max_tokens=32760, stream=False)
|
63 |
+
full_response = response.json()
|
64 |
+
print("Полный ответ от API (edit):", json.dumps(full_response, indent=2, ensure_ascii=False))
|
65 |
+
edited_story = full_response["choices"][0]["message"]["content"] if "choices" in full_response and len(full_response["choices"]) > 0 else "Ответ не получен или структура ответа не соответствует ожидаемой."
|
66 |
+
yield edited_story
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
except requests.exceptions.RequestException as e:
|
69 |
yield f"Ошибка API запроса: {e}"
|
|
|
84 |
{"role": "user", "content": continuation_prompt},
|
85 |
{"role": "user", "content": f"История: ```\n{original_story}\n```"}
|
86 |
]
|
87 |
+
response = call_pollinations_api(messages, stream=False)
|
88 |
+
full_response = response.json()
|
89 |
+
print("Полный ответ от API (next):", json.dumps(full_response, indent=2, ensure_ascii=False))
|
90 |
+
next_story = full_response["choices"][0]["message"]["content"] if "choices" in full_response and len(full_response["choices"]) > 0 else "Ответ не получен или структура ответа не соответствует ожидаемой."
|
91 |
+
yield next_story
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
except requests.exceptions.RequestException as e:
|
94 |
yield f"Ошибка API запроса: {e}"
|