Spaces:
Sleeping
Sleeping
Commit
•
53a9fc0
1
Parent(s):
4f62f50
Stream test #2
Browse files
app.py
CHANGED
@@ -1,24 +1,31 @@
|
|
1 |
-
import openai
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
# Getting responses using the OpenAI API
|
5 |
-
def response_chatgpt(api_key, message):
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
chatbot = gr.Interface(
|
20 |
-
fn=response_chatgpt,
|
21 |
inputs=["text", "text"],
|
22 |
outputs="text",
|
23 |
)
|
24 |
-
chatbot.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import openai
|
3 |
+
|
4 |
|
5 |
# Getting responses using the OpenAI API
|
6 |
+
def response_chatgpt(api_key, message: str):
|
7 |
+
# OPENAI API KEY
|
8 |
+
openai.api_key = api_key
|
9 |
+
response = openai.Completion.create(
|
10 |
+
engine="text-davinci-003",
|
11 |
+
prompt=message,
|
12 |
+
max_tokens=1024,
|
13 |
+
stream=False,
|
14 |
+
)
|
15 |
+
# Choosing best conversation
|
16 |
+
completion_text = ''
|
17 |
+
# iterate through the stream of events
|
18 |
+
for event in response:
|
19 |
+
event_text = event['choices'][0]['text'] # extract the text
|
20 |
+
completion_text += event_text # append the text
|
21 |
+
|
22 |
+
return completion_text
|
23 |
+
|
24 |
+
|
25 |
+
# User input and web interface
|
26 |
chatbot = gr.Interface(
|
27 |
+
fn=response_chatgpt,
|
28 |
inputs=["text", "text"],
|
29 |
outputs="text",
|
30 |
)
|
31 |
+
chatbot.launch()
|