Spaces:
Sleeping
Sleeping
Commit
•
822282c
1
Parent(s):
09262f3
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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=True,
|
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()
|