|
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() |
|
|