Spaces:
Runtime error
Runtime error
mahmudunnabi
commited on
Upload 4 files
Browse files- app.py +18 -0
- img_cap_utils.py +41 -0
- img_gen_utils.py +34 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import warnings
|
2 |
+
import gradio as gr
|
3 |
+
warnings.filterwarnings('ignore')
|
4 |
+
|
5 |
+
from img_cap_utils import img_cap_with_markdown
|
6 |
+
from img_gen_utils import img_gen_with_markdown
|
7 |
+
|
8 |
+
# Combine both the app
|
9 |
+
demo = gr.Blocks()
|
10 |
+
with demo:
|
11 |
+
gr.TabbedInterface(
|
12 |
+
[img_cap_with_markdown, img_gen_with_markdown],
|
13 |
+
['Image Captioning', 'Image Generation']
|
14 |
+
)
|
15 |
+
|
16 |
+
|
17 |
+
if __name__ == "__main__":
|
18 |
+
demo.launch()
|
img_cap_utils.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import io
|
3 |
+
import base64
|
4 |
+
import gradio as gr
|
5 |
+
cap_model = pipeline(task = 'image-to-text', model = "Salesforce/blip-image-captioning-base")
|
6 |
+
|
7 |
+
def image_to_base64_str(pil_image):
|
8 |
+
byte_arr = io.BytesIO()
|
9 |
+
pil_image.save(byte_arr, format = 'PNG')
|
10 |
+
byte_arr = byte_arr.getvalue()
|
11 |
+
return str(base64.b64encode(byte_arr).decode('utf-8'))
|
12 |
+
|
13 |
+
def captioner(image):
|
14 |
+
base64_image = image_to_base64_str(image)
|
15 |
+
result = cap_model(base64_image)
|
16 |
+
return result[0]['generated_text']
|
17 |
+
|
18 |
+
gr.close_all()
|
19 |
+
image_captioning = gr.Interface(
|
20 |
+
fn = captioner,
|
21 |
+
inputs = [gr.Image(label = "Upload Image", type = 'pil')],
|
22 |
+
outputs = [gr.Textbox(label = 'Caption')],
|
23 |
+
allow_flagging = 'never'
|
24 |
+
)
|
25 |
+
|
26 |
+
# Add Markdown content
|
27 |
+
markdown_content_img_cap = gr.Markdown(
|
28 |
+
"""
|
29 |
+
<div style='text-align: center; font-family: "Times New Roman";'>
|
30 |
+
<h1 style='color: #FF6347;'>Caption Any Image Using the BLIP model</h1>
|
31 |
+
<h3 style='color: #4682B4;'>Model: Salesforce/blip-image-captioning-base</h3>
|
32 |
+
<h3 style='color: #32CD32;'>Made By: Md. Mahmudun Nabi</h3>
|
33 |
+
</div>
|
34 |
+
"""
|
35 |
+
)
|
36 |
+
|
37 |
+
# Combine the Markdown content and the demo interface
|
38 |
+
img_cap_with_markdown = gr.Blocks()
|
39 |
+
with img_cap_with_markdown:
|
40 |
+
markdown_content_img_cap.render()
|
41 |
+
image_captioning.render()
|
img_gen_utils.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import DiffusionPipeline
|
3 |
+
|
4 |
+
# Load the pipeline
|
5 |
+
gen_model = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
6 |
+
gen_model = gen_model.to('cpu')
|
7 |
+
|
8 |
+
# Define the function
|
9 |
+
def get_completion(prompt):
|
10 |
+
return gen_model(prompt).images[0]
|
11 |
+
|
12 |
+
# Create the Gradio interface
|
13 |
+
img_gen = gr.Interface(
|
14 |
+
fn=get_completion,
|
15 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a prompt here..."),
|
16 |
+
outputs=gr.Image(type="pil"),
|
17 |
+
)
|
18 |
+
|
19 |
+
# Add Markdown content
|
20 |
+
markdown_content_img_gen = gr.Markdown(
|
21 |
+
"""
|
22 |
+
<div style='text-align: center; font-family: "Times New Roman";'>
|
23 |
+
<h1 style='color: #FF6347;'>Image Generation Using Stable Diffusion</h1>
|
24 |
+
<h3 style='color: #4682B4;'>Model: runwayml/stable-diffusion-v1-5</h3>
|
25 |
+
<h3 style='color: #32CD32;'>Made By: Md. Mahmudun Nabi</h3>
|
26 |
+
</div>
|
27 |
+
"""
|
28 |
+
)
|
29 |
+
|
30 |
+
# Combine the Markdown content and the demo interface
|
31 |
+
img_gen_with_markdown = gr.Blocks()
|
32 |
+
with img_gen_with_markdown:
|
33 |
+
markdown_content_img_gen.render()
|
34 |
+
img_gen.render()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
diffusers
|
3 |
+
pybase64
|
4 |
+
transformers
|