SimrusDenuvo commited on
Commit
a121d15
·
verified ·
1 Parent(s): 6c23302

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -1,14 +1,29 @@
1
  import openai
2
  import os
 
3
 
 
4
  openai.api_key = os.environ.get("OPENAI_API_KEY")
5
 
 
6
  def chatgpt_prompt(prompt):
7
- response = openai.ChatCompletion.create(
8
- model="gpt-3.5-turbo",
9
- messages=[{"role": "user", "content": prompt}]
10
- )
11
- return response['choices'][0]['message']['content']
12
-
13
- iface = gr.Interface(fn=hello, inputs="text", outputs="text")
14
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import openai
2
  import os
3
+ import gradio as gr
4
 
5
+ # Получаем ключ API из переменной окружения
6
  openai.api_key = os.environ.get("OPENAI_API_KEY")
7
 
8
+ # Основная функция общения с ChatGPT
9
  def chatgpt_prompt(prompt):
10
+ try:
11
+ response = openai.ChatCompletion.create(
12
+ model="gpt-3.5-turbo",
13
+ messages=[{"role": "user", "content": prompt}]
14
+ )
15
+ return response['choices'][0]['message']['content']
16
+ except Exception as e:
17
+ return f"Ошибка ChatGPT: {str(e)}"
18
+
19
+ # Интерфейс Gradio
20
+ iface = gr.Interface(
21
+ fn=chatgpt_prompt,
22
+ inputs=gr.Textbox(label="Введите ваш запрос"),
23
+ outputs=gr.Textbox(label="Ответ от ChatGPT"),
24
+ title="Интерфейс ChatGPT",
25
+ description="Пример взаимодействия с API OpenAI через Hugging Face Space"
26
+ )
27
+
28
+ # Запуск
29
+ iface.launch()