JustForFunn commited on
Commit
8a4cad7
·
1 Parent(s): a8450e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -46
app.py CHANGED
@@ -1,60 +1,38 @@
1
-
2
  import openai
3
- import gradio as gr
4
- from gradio.components import Textbox, Image, Label
 
 
5
 
6
- # Set up OpenAI API credentials
7
- openai.api_key = "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"
 
 
8
 
9
- # Define the OpenAI prompt completion function
10
  def generate_text(prompt):
11
  response = openai.Completion.create(
12
- model= "text-davinci-003",
13
  prompt=prompt,
14
- max_tokens=60,
15
  n=1,
16
  stop=None,
17
- temperature=0.8,
18
  )
19
  return response.choices[0].text.strip()
20
 
21
- # Define the image generation function
22
- model_path = "models/dreamlike-art/dreamlike-photoreal-2.0"
23
- model_prefix = "photoreal style"
24
- model = gr.Interface.load(model_path)
25
-
26
- def generate_image(prompt):
27
- # Generate text using OpenAI's GPT-3 model
28
- text = generate_text(prompt)
29
-
30
- # Generate an image using a DALL-E model trained on photorealistic images
31
- if model is not None:
32
- image_return = model.predict(model_prefix + ": " + text)
33
- else:
34
- image_return = None
35
-
36
- # Return the generated image
37
- return image_return[0] if image_return is not None else None
38
-
39
- # Define the Gradio interface
40
- prompt_input = Textbox("Enter a prompt", lines=3)
41
- text_output = Label("")
42
- image_output = Image()
43
-
44
- iface = gr.Interface(
45
- generate_image,
46
- inputs=prompt_input,
47
- outputs=[text_output, image_output],
48
- title="Image Generator",
49
- description="Generates an image based on a prompt using OpenAI's GPT-3 and DALL-E models."
50
  )
51
 
52
- # Update the output boxes with the generated text and image
53
- def update_text_output(text, image_url):
54
- text_output.text = text
55
- text_output.style['font-size'] = '8px' # Change the font size here
56
-
57
- iface.interface_result_hooks = [update_text_output]
58
-
59
  # Launch the interface
60
- iface.launch()
 
 
1
  import openai
2
+ import gradio
3
+
4
+ # Initialize OpenAI API credentials
5
+ openai.api_key = "YOUR_API_KEY"
6
 
7
+ # Load the text-to-image generation model
8
+ model_path = "models/dreamlike-art/dreamlike-photoreal-2.0"
9
+ model_prefix = "photoreal style"
10
+ model = gradio.Interface.load(model_path)
11
 
12
+ # Define a function to generate text response using OpenAI API
13
  def generate_text(prompt):
14
  response = openai.Completion.create(
15
+ engine="davinci",
16
  prompt=prompt,
17
+ max_tokens=1024,
18
  n=1,
19
  stop=None,
20
+ temperature=0.7,
21
  )
22
  return response.choices[0].text.strip()
23
 
24
+ # Define a function to generate image from text prompt using the loaded model
25
+ def process_prompt(prompt):
26
+ prompt += ", in " + model_prefix
27
+ image_return = model(prompt)
28
+ return image_return
29
+
30
+ # Create a Gradio interface with an input textbox for the prompt and output sections for image and text
31
+ interface = gradio.Interface(
32
+ fn=lambda prompt: (process_prompt(prompt), generate_text(prompt)),
33
+ inputs=gradio.inputs.Textbox(label="Enter Prompt"),
34
+ outputs=[gradio.outputs.Image(label="Generated Image"), gradio.outputs.Textbox(label="Generated Text")]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  )
36
 
 
 
 
 
 
 
 
37
  # Launch the interface
38
+ interface.launch()