Shankarm08 commited on
Commit
4d21766
·
verified ·
1 Parent(s): e0bc810

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -3
app.py CHANGED
@@ -4,6 +4,8 @@ from dotenv import load_dotenv
4
  from huggingface_hub import InferenceApi
5
  from PIL import Image
6
  from io import BytesIO
 
 
7
 
8
  # Load environment variables from the .env file
9
  load_dotenv()
@@ -28,9 +30,25 @@ if st.button("Generate Image"):
28
  # Make request to the Hugging Face model
29
  output = inference(inputs=prompt)
30
 
31
- # Convert the output to an image
32
- image = Image.open(BytesIO(output))
33
- st.image(image, caption="Generated Image", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  except Exception as e:
35
  st.error(f"Error: {str(e)}")
36
  else:
 
4
  from huggingface_hub import InferenceApi
5
  from PIL import Image
6
  from io import BytesIO
7
+ import requests
8
+ import base64
9
 
10
  # Load environment variables from the .env file
11
  load_dotenv()
 
30
  # Make request to the Hugging Face model
31
  output = inference(inputs=prompt)
32
 
33
+ # Check if the output is a valid PIL image (already in image format)
34
+ if isinstance(output, Image.Image):
35
+ image = output
36
+ # Check if the output contains a base64-encoded string
37
+ elif isinstance(output, dict) and 'generated_image_base64' in output:
38
+ # Decode the base64 string to bytes
39
+ image_data = base64.b64decode(output['generated_image_base64'])
40
+ image = Image.open(BytesIO(image_data))
41
+ # If output contains an image URL
42
+ elif isinstance(output, dict) and 'generated_image_url' in output:
43
+ response = requests.get(output['generated_image_url'])
44
+ image = Image.open(BytesIO(response.content))
45
+ else:
46
+ st.error("Unexpected output format from the inference API.")
47
+ image = None
48
+
49
+ # Display the image
50
+ if image:
51
+ st.image(image, caption="Generated Image", use_column_width=True)
52
  except Exception as e:
53
  st.error(f"Error: {str(e)}")
54
  else: