Spaces:
Runtime error
Runtime error
| import openai | |
| import streamlit as st | |
| import json | |
| # Устанавливаем ключ API | |
| openai.api_key = "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" | |
| # Функция для генерации ответа | |
| def generate_answer(prompt, model, max_tokens): | |
| headers = { | |
| "Content-Type": "application/json", | |
| "Authorization": f"Bearer {openai.api_key}", | |
| } | |
| data = { | |
| "prompt": prompt, | |
| "model": model, | |
| "max_tokens": max_tokens, | |
| "n": 1, | |
| "stop": None, | |
| "temperature": 0.7, | |
| } | |
| response = openai.api_requestor.APIRequestor().request( | |
| "POST", | |
| "https://api.openai.com/v1/chat/completions", | |
| headers=headers, | |
| data=json.dumps(data) | |
| ) | |
| message = response["choices"][0]["text"] | |
| return message.strip() | |
| # Название страницы | |
| st.set_page_config(page_title="Chatbot with GPT-3.5 Turbo") | |
| # Заголовок | |
| st.title("Chatbot with GPT-3.5 Turbo") | |
| # Предупреждение о конфиденциальности | |
| st.write("This demo uses OpenAI's GPT-3.5 model to generate text. Please keep in mind that the model may generate inappropriate or offensive content.") | |
| # Интерфейс ввода сообщения | |
| message = st.text_input("You: ", "") | |
| # Если пользователь отправил сообщение | |
| if message: | |
| # Генерация ответа | |
| response = generate_answer( | |
| prompt=message, | |
| model="gpt-3.5-turbo", | |
| max_tokens=150, | |
| ) | |
| # Отображение ответа | |
| st.text_area("Bot: ", value=response, height=200, max_chars=None, key=None) | |
| # Инструкции | |
| st.write("Enter your message in the text box above and press Enter to start a conversation with the chatbot. The chatbot will use GPT-3.5 to generate responses to your messages. Have fun chatting!") | |