File size: 2,309 Bytes
f92a51b
 
6eccccd
ec1e4af
f92a51b
1121f22
d8abe74
6eccccd
23a6746
d8abe74
6eccccd
 
c8492bd
1121f22
01868c2
d8abe74
6eccccd
ec1e4af
0758550
ba8bc6a
 
 
 
e177d94
 
ba8bc6a
 
 
fdc9379
ba8bc6a
 
e177d94
 
ba8bc6a
e177d94
 
10954e8
e177d94
 
10954e8
402c0a6
14b0402
f20d184
c2b0606
8fba9f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import gradio as gr
import requests
import json
import os

def dzen(theme, description=""):
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {os.getenv("API_KEY")}'
    }

    payload = {
        'messages': [{'role': 'system', 'content': f'Напиши пожалуйста классную, понятную, подробную, оригинальную, уникальную статью для Яндекс Дзен, без ошибок, опечаток, орфографических ошибок, на тему \"{theme}\"  {description}  Пиши ТОЛЬКО пост (БЕЗ пояснений, БЕЗ markdown, БЕЗ другого текста), текст в посте пиши на языке который используется в описании. Если описание пустое, то сгенерируй интересный пост на любую популярную тему тему.'}],
        'max_tokens': 25000,
        'model': os.getenv("MODEL")
    }

    response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)

    if response.status_code == 200 and response.text:
        try:
            data = response.json()
        except json.decoder.JSONDecodeError as e:
            gr.alert(f'Ошибка при декодировании JSON: {str(e)}')
            return ''
        
        if 'choices' in data and len(data['choices']) > 0:
            command = data['choices'][0]['message']['content'].strip()
            return command
        elif 'error' in data:
            error_message = data['error']['message']
            gr.alert(f'Ошибка: {error_message}')
            return ''
        else:
            gr.alert(f'Не удалось сгенерировать пост. {data}')
            return ''
    else:
        gr.alert(f'Ошибка при получении данных от сервера. Статус код: {response.status_code}')
        return ''

iface = gr.Interface(fn=dzen, inputs=[
    gr.Textbox(label="Тема", placeholder=""),
    gr.Textbox(label="Дополнительный текст (Не обязательно)")
], outputs=gr.Textbox(label="Пост"), title="Генератор постов Яндекс Дзен")
iface.launch()