tchans123 commited on
Commit
1ce6a12
·
verified ·
1 Parent(s): 2dcb293

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
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 = "openai-api-key"
6
 
7
  def generate_text(prompt, max_length=100, temperature=1.0):
8
- # Generate text using GPT-3
9
- response = openai.Completion.create(
10
- engine="text-davinci-003", # You can also use other engines like "gpt-3.5-turbo"
11
- prompt=prompt,
12
- max_tokens=max_length,
13
- temperature=temperature,
14
- n=1,
15
- stop=None
16
- )
17
- return response.choices[0].text.strip()
 
 
 
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="GPT Text Generator",
29
- description="Enter a prompt and generate text using the GPT model."
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)