play7284's picture
Update app.py
ee71ca6 verified
raw
history blame
833 Bytes
# import gradio as gr
# gr.load("models/meta-llama/Llama-3.1-8B-Instruct").launch()
import openai
import gradio as gr
# 设置 OpenAI API 密钥
openai.api_key = "你的_openai_api_key"
def respond(message, history):
# 生成回复
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=history + [{"role": "user", "content": message}]
)
reply = response['choices'][0]['message']['content']
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": reply})
return reply, history
# 创建 Gradio 接口
iface = gr.Interface(
fn=respond,
inputs=["text", "state"],
outputs=["text", "state"],
title="OpenAI 聊天应用",
description="与 OpenAI 的聊天模型进行对话"
)
# 启动应用
iface.launch()