easy-text-gen / app.py
Rooni's picture
Update app.py
5395fc0
raw
history blame
1.01 kB
import gradio as gr
import requests
import json
import os
import time
def generate(description, model, max_tokens):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.getenv("API_KEY")}'
}
payload = {
'messages': [{'role': 'system', 'content': f'{description}'}],
'max_tokens': max_tokens,
'model': model
}
while True:
response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
data = json.loads(response.text)
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']
print(f'Ошибка: {error_message}')
time.sleep(1)
else:
print(f'Не удалось сгенерировать текст. Повторная попытка...')
time.sleep(1)