-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
68 lines (33 loc) · 1.2 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
import os, openai
conversation = []
class ChatGPT:
def __init__(self):
self.api_key = ""
self.messages = conversation
self.model = os.getenv("OPENAI_MODEL", default = "gpt-3.5-turbo")
def save_api_key(self, user_input0):
self.api_key = user_input0
def get_response(self, user_input):
openai.api_key = self.api_key
conversation.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model=self.model,
messages = self.messages
)
conversation.append({"role": "assistant", "content": response['choices'][0]['message']['content']})
print("AI回答內容:")
print(response['choices'][0]['message']['content'].strip())
return response['choices'][0]['message']['content'].strip()
chatgpt = ChatGPT()
def greet(prompt, api_key):
chatgpt.save_api_key(api_key)
reply_text = chatgpt.get_response(prompt)
greeting = f"{reply_text}"
return greeting
demo = gr.Interface(
fn=greet,
inputs=["text", "text"],
outputs=["text"],
)
demo.launch()