mookkanvas commited on
Commit
725566b
1 Parent(s): d3e1bc0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -44
app.py CHANGED
@@ -1,50 +1,24 @@
1
- import streamlit as st
2
- from dotenv import load_dotenv
3
- import os
4
- import openai
5
- from diffusers import StableDiffusionPipeline
6
- import torch
7
 
8
- load_dotenv()
9
- openai.api_key = os.getenv("OPENAI_API_KEY")
10
 
11
- #function to generate AI based images using OpenAI Dall-E
12
- def generate_images_using_openai(text):
13
- response = openai.Image.create(prompt= text, n=1, size="512x512")
14
- image_url = response['data'][0]['url']
15
- return image_url
16
 
 
 
17
 
18
- #function to generate AI based images using Huggingface Diffusers
19
- def generate_images_using_huggingface_diffusers(text):
20
- pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
21
- pipe = pipe.to("cuda")
22
- prompt = text
23
- image = pipe(prompt).images[0]
24
- return image
25
 
26
- #Streamlit Code
27
- choice = st.sidebar.selectbox("Select your choice", ["Home", "DALL-E", "Huggingface Diffusers"])
28
 
29
- if choice == "Home":
30
- st.title("AI Image Generation App")
31
- with st.expander("About the App"):
32
- st.write("This is a simple image generation app that uses AI to generates images from text prompt.")
33
-
34
- elif choice == "DALL-E":
35
- st.subheader("Image generation using Open AI's DALL-E")
36
- input_prompt = st.text_input("Enter your text prompt")
37
- if input_prompt is not None:
38
- if st.button("Generate Image"):
39
- image_url = generate_images_using_openai(input_prompt)
40
- st.image(image_url, caption="Generated by DALL-E")
41
-
42
- elif choice == "Huggingface Diffusers":
43
- st.subheader("Image generation using Huggingface Diffusers")
44
- input_prompt = st.text_input("Enter your text prompt")
45
- if input_prompt is not None:
46
- if st.button("Generate Image"):
47
- image_output = generate_images_using_huggingface_diffusers(input_prompt)
48
- st.info("Generating image.....")
49
- st.success("Image Generated Successfully")
50
- st.image(image_output, caption="Generated by Huggingface Diffusers")
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from transformers import pipeline
 
 
 
4
 
5
+ # Create an image captioning pipeline using Hugging Face Transformers
6
+ image_captioner = pipeline("image-captioning")
7
 
8
+ # Streamlit app header
9
+ st.title("Hugging Face Transformer Image-to-Text App")
 
 
 
10
 
11
+ # File uploader for image
12
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
13
 
14
+ if uploaded_image:
15
+ # Display the uploaded image
16
+ image = Image.open(uploaded_image)
17
+ st.image(image, caption="Uploaded Image", use_column_width=True)
 
 
 
18
 
19
+ # Perform image captioning when an image is uploaded
20
+ caption = image_captioner(uploaded_image)[0]["caption"]
21
 
22
+ # Display the generated caption
23
+ st.subheader("Generated Caption:")
24
+ st.write(caption)