Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
|
3 |
+
# gr.load("models/meta-llama/Llama-3.1-8B-Instruct").launch()
|
4 |
+
|
5 |
+
import openai
|
6 |
import gradio as gr
|
7 |
|
8 |
+
# 设置 OpenAI API 密钥
|
9 |
+
openai.api_key = "你的_openai_api_key"
|
10 |
+
|
11 |
+
def respond(message, history):
|
12 |
+
# 生成回复
|
13 |
+
response = openai.ChatCompletion.create(
|
14 |
+
model="gpt-3.5-turbo",
|
15 |
+
messages=history + [{"role": "user", "content": message}]
|
16 |
+
)
|
17 |
+
reply = response['choices'][0]['message']['content']
|
18 |
+
history.append({"role": "user", "content": message})
|
19 |
+
history.append({"role": "assistant", "content": reply})
|
20 |
+
return reply, history
|
21 |
+
|
22 |
+
# 创建 Gradio 接口
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=respond,
|
25 |
+
inputs=["text", "state"],
|
26 |
+
outputs=["text", "state"],
|
27 |
+
title="OpenAI 聊天应用",
|
28 |
+
description="与 OpenAI 的聊天模型进行对话"
|
29 |
+
)
|
30 |
+
|
31 |
+
# 启动应用
|
32 |
+
iface.launch()
|