Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from openai import OpenAI
|
3 |
+
|
4 |
+
def ask_gpt(api_key, prompt):
|
5 |
+
client=OpenAI(api_key=api_key)
|
6 |
+
chat_completion=client.chat.completions.create(
|
7 |
+
messages=[{'role': 'user', 'content': prompt}],
|
8 |
+
model='gpt-3.5-turbo'
|
9 |
+
)
|
10 |
+
return chat_completion.choices[0].message.content
|
11 |
+
|
12 |
+
api_key=gr.Textbox(label='輸入 OpenAI 金鑰')
|
13 |
+
prompt=gr.Textbox(label='您的詢問: ')
|
14 |
+
reply=gr.Textbox(label='OpenAI 回答: ')
|
15 |
+
iface=gr.Interface(
|
16 |
+
fn=ask_gpt,
|
17 |
+
inputs=[api_key, prompt],
|
18 |
+
outputs=reply,
|
19 |
+
title='OpenAI API 聊天機器人',
|
20 |
+
allow_flagging='never',
|
21 |
+
)
|
22 |
+
iface.launch()
|