Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,16 +9,15 @@ def load_system_role(role_name):
|
|
9 |
roles = json.load(file)
|
10 |
return roles.get(role_name, "Ты помощник по умолчанию.")
|
11 |
|
12 |
-
# Функция для загрузки названий ролей из JSON файла
|
13 |
def load_role_names():
|
14 |
with open('system_roles.json', 'r', encoding='utf-8') as file:
|
15 |
roles = json.load(file)
|
16 |
return list(roles.keys())
|
17 |
|
18 |
-
# Функция для генерации текста
|
19 |
def generate(description, system_role_name, max_tokens):
|
20 |
if not description:
|
21 |
-
|
|
|
22 |
|
23 |
system_role = load_system_role(system_role_name)
|
24 |
|
@@ -30,28 +29,36 @@ def generate(description, system_role_name, max_tokens):
|
|
30 |
payload = {
|
31 |
'messages': [{'role': 'system', 'content': system_role}, {'role': 'user', 'content': description}],
|
32 |
'max_tokens': max_tokens,
|
33 |
-
'model': "gemini-1.5-pro-latest"
|
|
|
34 |
}
|
35 |
|
36 |
try:
|
37 |
-
response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload, timeout=200)
|
38 |
-
response.raise_for_status()
|
39 |
-
data = response.json() # Используем response.json() вместо json.loads()
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
except requests.exceptions.RequestException as e:
|
53 |
print(f"Ошибка запроса: {e}")
|
54 |
-
|
|
|
|
|
|
|
55 |
|
56 |
# Ссылка на файл CSS
|
57 |
css_url = "https://neurixyufi-aihub.static.hf.space/style.css"
|
|
|
9 |
roles = json.load(file)
|
10 |
return roles.get(role_name, "Ты помощник по умолчанию.")
|
11 |
|
|
|
12 |
def load_role_names():
|
13 |
with open('system_roles.json', 'r', encoding='utf-8') as file:
|
14 |
roles = json.load(file)
|
15 |
return list(roles.keys())
|
16 |
|
|
|
17 |
def generate(description, system_role_name, max_tokens):
|
18 |
if not description:
|
19 |
+
yield None, None
|
20 |
+
return
|
21 |
|
22 |
system_role = load_system_role(system_role_name)
|
23 |
|
|
|
29 |
payload = {
|
30 |
'messages': [{'role': 'system', 'content': system_role}, {'role': 'user', 'content': description}],
|
31 |
'max_tokens': max_tokens,
|
32 |
+
'model': "gemini-1.5-pro-latest",
|
33 |
+
'stream': True
|
34 |
}
|
35 |
|
36 |
try:
|
37 |
+
response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload, stream=True, timeout=200)
|
38 |
+
response.raise_for_status()
|
|
|
39 |
|
40 |
+
full_text = ""
|
41 |
+
for chunk in response.iter_lines():
|
42 |
+
if chunk:
|
43 |
+
try:
|
44 |
+
chunk = chunk.decode('utf-8').replace("data: ", "")
|
45 |
+
if chunk == "[DONE]":
|
46 |
+
break
|
47 |
+
chunk_data = json.loads(chunk)
|
48 |
+
if 'choices' in chunk_data and len(chunk_data['choices']) > 0:
|
49 |
+
text_chunk = chunk_data['choices'][0]['delta'].get('content', "")
|
50 |
+
full_text += text_chunk
|
51 |
+
yield full_text, full_text
|
52 |
+
except json.JSONDecodeError:
|
53 |
+
continue
|
54 |
+
if not full_text:
|
55 |
+
yield "**Не удалось получить ответ от сервера.**", "Не удалось получить ответ от сервера."
|
56 |
except requests.exceptions.RequestException as e:
|
57 |
print(f"Ошибка запроса: {e}")
|
58 |
+
yield f"**Ошибка запроса!**\n\n```\n{e}\n```", f"Ошибка запроса!\n\n{e}"
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Ошибка: {str(e)}")
|
61 |
+
yield "Произошла ошибка при генерации", "Произошла ошибка при генерации"
|
62 |
|
63 |
# Ссылка на файл CSS
|
64 |
css_url = "https://neurixyufi-aihub.static.hf.space/style.css"
|