Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
|
|
|
| 3 |
|
| 4 |
-
# Initialize the OpenAI API with your API key
|
| 5 |
-
openai.api_key = "
|
| 6 |
|
| 7 |
def generate_text(prompt, max_length=100, temperature=1.0):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Create a Gradio interface
|
| 20 |
iface = gr.Interface(
|
|
@@ -25,9 +29,9 @@ iface = gr.Interface(
|
|
| 25 |
gr.Slider(minimum=0.1, maximum=1.5, value=1.0, label="Temperature"),
|
| 26 |
],
|
| 27 |
outputs=gr.Textbox(label="Generated Text"),
|
| 28 |
-
title="
|
| 29 |
-
description="Enter a prompt and generate text using
|
| 30 |
)
|
| 31 |
|
| 32 |
# Launch the interface
|
| 33 |
-
iface.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Initialize the OpenAI API with your API key from environment variables
|
| 6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
def generate_text(prompt, max_length=100, temperature=1.0):
|
| 9 |
+
try:
|
| 10 |
+
# Generate text using OpenAI's GPT-3 (text-davinci-003)
|
| 11 |
+
response = openai.Completion.create(
|
| 12 |
+
engine="text-davinci-003", # Specify the engine
|
| 13 |
+
prompt=prompt,
|
| 14 |
+
max_tokens=max_length,
|
| 15 |
+
temperature=temperature,
|
| 16 |
+
n=1,
|
| 17 |
+
stop=None
|
| 18 |
+
)
|
| 19 |
+
return response.choices[0].text.strip()
|
| 20 |
+
except openai.error.OpenAIError as e:
|
| 21 |
+
return f"An error occurred: {str(e)}"
|
| 22 |
|
| 23 |
# Create a Gradio interface
|
| 24 |
iface = gr.Interface(
|
|
|
|
| 29 |
gr.Slider(minimum=0.1, maximum=1.5, value=1.0, label="Temperature"),
|
| 30 |
],
|
| 31 |
outputs=gr.Textbox(label="Generated Text"),
|
| 32 |
+
title="GP Text Generator",
|
| 33 |
+
description="Enter a prompt and generate text using OpenAI's GPT (text-davinci-003)."
|
| 34 |
)
|
| 35 |
|
| 36 |
# Launch the interface
|
| 37 |
+
iface.launch(share=True)
|