File size: 2,098 Bytes
f92a51b
 
6eccccd
ec1e4af
f92a51b
1121f22
d8abe74
6eccccd
23a6746
d8abe74
6eccccd
 
1121f22
 
01868c2
d8abe74
6eccccd
ec1e4af
0758550
ba8bc6a
 
 
 
 
 
 
 
 
 
 
 
 
1121f22
10954e8
ba8bc6a
10954e8
1121f22
 
 
 
0758550
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
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:
            return f'Ошибка при декодировании JSON: {str(e)}'
        
        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']
            return f'Ошибка: {error_message}'
        else:
            return f'Не удалось сгенерировать пост. {data}'
    else:
        return f'Ошибка при получении данных от сервера. Статус код: {response.status_code}'

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