Spaces:
Runtime error
Runtime error
Commit
·
3d1a684
1
Parent(s):
540f76d
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,37 @@
|
|
1 |
import openai
|
2 |
-
import re
|
3 |
import gradio as gr
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
openai.api_key = api_key
|
9 |
-
|
10 |
-
# Send the prompt to OpenAI's GPT-3 engine
|
11 |
-
response = openai.Completion.create(
|
12 |
-
engine="davinci",
|
13 |
-
prompt=prompt,
|
14 |
-
max_tokens=100,
|
15 |
-
n=1,
|
16 |
-
stop=None,
|
17 |
-
temperature=0.5,
|
18 |
-
)
|
19 |
|
20 |
-
|
21 |
-
answer = response.choices[0].text.strip()
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
|
28 |
-
# Define the Gradio interface
|
29 |
iface = gr.Interface(
|
30 |
-
fn=
|
31 |
inputs=[
|
32 |
-
gr.inputs.Textbox(label="
|
33 |
-
gr.inputs.Textbox(
|
|
|
34 |
],
|
35 |
-
outputs=gr.outputs.Textbox(label="
|
36 |
-
title="
|
37 |
-
description="
|
38 |
-
live=True,
|
39 |
-
button_text="Calculate"
|
40 |
)
|
41 |
|
42 |
-
|
43 |
-
iface.launch()
|
|
|
1 |
import openai
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
+
def chat(api_key, message, model):
|
5 |
+
if not api_key:
|
6 |
+
return "Please enter a valid API key."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
openai.api_key = api_key
|
|
|
9 |
|
10 |
+
try:
|
11 |
+
response = openai.Completion.create(
|
12 |
+
engine=model,
|
13 |
+
prompt=message,
|
14 |
+
max_tokens=50,
|
15 |
+
n=1,
|
16 |
+
stop=None,
|
17 |
+
temperature=0.5,
|
18 |
+
)
|
19 |
+
return response.choices[0].text.strip()
|
20 |
+
except Exception as e:
|
21 |
+
return f"Error: {str(e)}"
|
22 |
|
23 |
+
models = ["gpt-4", "text-davinci-002", "text-curie-002", "text-babbage-002", "text-ada-002"]
|
24 |
|
|
|
25 |
iface = gr.Interface(
|
26 |
+
fn=chat,
|
27 |
inputs=[
|
28 |
+
gr.inputs.Textbox(lines=1, label="API Key"),
|
29 |
+
gr.inputs.Textbox(lines=5, label="Message"),
|
30 |
+
gr.inputs.Dropdown(choices=models, label="Model"),
|
31 |
],
|
32 |
+
outputs=gr.outputs.Textbox(label="Response"),
|
33 |
+
title="GPT-4 Chat App",
|
34 |
+
description="A simple chat app using OpenAI GPT-4 and Gradio.",
|
|
|
|
|
35 |
)
|
36 |
|
37 |
+
iface.launch()
|
|