Spaces:
Runtime error
Runtime error
File size: 934 Bytes
53404ca 2cbbbac a121d15 14e67e3 a121d15 2cbbbac 14e67e3 a121d15 2cbbbac a121d15 |
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 |
import openai
import os
import gradio as gr
# Получаем ключ API из переменной окружения
openai.api_key = os.environ.get("OPENAI_API_KEY")
# Основная функция общения с ChatGPT
def chatgpt_prompt(prompt):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
return response['choices'][0]['message']['content']
except Exception as e:
return f"Ошибка ChatGPT: {str(e)}"
# Интерфейс Gradio
iface = gr.Interface(
fn=chatgpt_prompt,
inputs=gr.Textbox(label="Введите ваш запрос"),
outputs=gr.Textbox(label="Ответ от ChatGPT"),
title="Интерфейс ChatGPT",
description="Пример взаимодействия с API OpenAI через Hugging Face Space"
)
# Запуск
iface.launch()
|