mrolando commited on
Commit
351a99e
1 Parent(s): d778c10

trying to fix download button

Browse files
Files changed (3) hide show
  1. app.py +10 -1
  2. img.png +0 -0
  3. test.py +29 -0
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  from dotenv import load_dotenv
3
  from transformers import pipeline
 
 
4
 
5
  # Load environment variables from the .env file de forma local
6
  load_dotenv()
@@ -31,7 +33,14 @@ def get_image(text: str, translate: bool):
31
  prompt=text, n=1, size="512x512"
32
  ) # ,response_format="b64_json"
33
  print(response)
34
- return response["data"][0]["url"] # type: ignore
 
 
 
 
 
 
 
35
 
36
 
37
  with gr.Blocks() as demo:
 
1
  import gradio as gr
2
  from dotenv import load_dotenv
3
  from transformers import pipeline
4
+ import urllib.request
5
+ from PIL import Image
6
 
7
  # Load environment variables from the .env file de forma local
8
  load_dotenv()
 
33
  prompt=text, n=1, size="512x512"
34
  ) # ,response_format="b64_json"
35
  print(response)
36
+
37
+ urllib.request.urlretrieve(response["data"][0]["url"], "img.png") # type: ignore
38
+
39
+ img = Image.open("img.png")
40
+
41
+ return img
42
+
43
+ # return response["data"][0]["url"] # type: ignore
44
 
45
 
46
  with gr.Blocks() as demo:
img.png ADDED
test.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+
4
+ def generate_image(input_text):
5
+ """Generates an image from the input text."""
6
+
7
+ # Generate the image
8
+ image = ...
9
+
10
+ # Convert the image to a base64 string
11
+ base64_string = base64.b64encode(image.tobytes())
12
+
13
+ # Return the base64 string
14
+ return base64_string
15
+
16
+
17
+ def download_image(base64_string):
18
+ """Downloads the output image as a .png file."""
19
+
20
+ filename = "image.png"
21
+ with open(filename, "wb") as f:
22
+ f.write(base64.b64decode(base64_string))
23
+
24
+ # Allow the user to download the file
25
+ gr.File.download(filename)
26
+
27
+
28
+ if __name__ == "__main__":
29
+ gr.Interface(fn=generate_image, inputs="text", outputs="image").launch()