heaversm commited on
Commit
dfa75b2
1 Parent(s): 5fa41d5

check for pw in order to generate image

Browse files
Files changed (2) hide show
  1. app.py +25 -8
  2. env-sample +2 -1
app.py CHANGED
@@ -8,16 +8,32 @@ from dotenv import load_dotenv
8
  load_dotenv()
9
 
10
  openai_key = os.getenv("OPENAI_API_KEY")
 
11
 
12
  if openai_key == "<YOUR_OPENAI_API_KEY>":
13
  openai_key = ""
14
 
 
 
 
 
 
 
15
  if openai_key == "":
16
  sys.exit("Please Provide Your OpenAI API Key")
17
 
18
 
19
- def generate_image(text, model, quality, size):
 
 
 
 
 
 
 
20
  try:
 
 
21
  client = OpenAI(api_key=openai_key)
22
 
23
  response = client.images.generate(
@@ -29,22 +45,23 @@ def generate_image(text, model, quality, size):
29
  )
30
  except Exception as error:
31
  print(str(error))
32
- raise gr.Error("An error occurred while generating speech. Please check your API key and come back try again.")
33
 
34
  return response.data[0].url
35
 
36
 
37
  with gr.Blocks() as demo:
38
  gr.Markdown("# <center> Prompt de Resistance Image Generator</center>")
39
- gr.Markdown("**Instructions**: Generate an image from the text prompt below, then click the download arrow from the top right of the image to save it.")
40
-
 
41
  text = gr.Textbox(label="What do you want to create?",
42
- placeholder="Enter your text and then click on the \"Image Generate\" button, "
43
- "or simply press the Enter key.")
44
  btn = gr.Button("Generate Image")
45
  output_image = gr.Image(label="Image Output")
46
 
47
- text.submit(fn=generate_image, inputs=[text], outputs=output_image, api_name="generate_image")
48
- btn.click(fn=generate_image, inputs=[text], outputs=output_image, api_name=False)
49
 
50
  demo.launch(share=True)
 
8
  load_dotenv()
9
 
10
  openai_key = os.getenv("OPENAI_API_KEY")
11
+ pw_key = os.getenv("PW")
12
 
13
  if openai_key == "<YOUR_OPENAI_API_KEY>":
14
  openai_key = ""
15
 
16
+ if pw_key == "<YOUR_PW>":
17
+ pw_key = ""
18
+
19
+ if pw_key == "":
20
+ sys.exit("Please Provide A Password in the Environment Variables")
21
+
22
  if openai_key == "":
23
  sys.exit("Please Provide Your OpenAI API Key")
24
 
25
 
26
+
27
+ def generate_image(text, pw):
28
+ # add a conditional to check for a valid password
29
+
30
+ if pw != os.getenv("PW"):
31
+ # output an error message to the user in the gradio interface if password is invalid
32
+ raise gr.Error("Invalid password. Please try again.")
33
+
34
  try:
35
+
36
+
37
  client = OpenAI(api_key=openai_key)
38
 
39
  response = client.images.generate(
 
45
  )
46
  except Exception as error:
47
  print(str(error))
48
+ raise gr.Error("An error occurred while generating the image.")
49
 
50
  return response.data[0].url
51
 
52
 
53
  with gr.Blocks() as demo:
54
  gr.Markdown("# <center> Prompt de Resistance Image Generator</center>")
55
+ gr.Markdown("**Instructions**: To use this service, please enter the password. Then generate an image from the prompt field below, then click the download arrow from the top right of the image to save it.")
56
+ pw = gr.Textbox(label="Password", type="password",
57
+ placeholder="Enter the password to unlock the service")
58
  text = gr.Textbox(label="What do you want to create?",
59
+ placeholder="Enter your text and then click on the \"Image Generate\" button, "
60
+ "or simply press the Enter key.")
61
  btn = gr.Button("Generate Image")
62
  output_image = gr.Image(label="Image Output")
63
 
64
+ text.submit(fn=generate_image, inputs=[text,pw], outputs=output_image, api_name="generate_image")
65
+ btn.click(fn=generate_image, inputs=[text,pw], outputs=output_image, api_name=False)
66
 
67
  demo.launch(share=True)
env-sample CHANGED
@@ -1 +1,2 @@
1
- OPENAI_API_KEY = YOUR_API_KEY
 
 
1
+ OPENAI_API_KEY = <YOUR_OPENAI_API_KEY>
2
+ PW = <YOUR_PW>