OmParkashPandeY commited on
Commit
3969d04
1 Parent(s): 2555049

Upload 6 files

Browse files
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .venv/
2
+ .env
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+ from IPython.display import Image
4
+ from PIL import Image
5
+ import base64
6
+ import gradio as gr
7
+
8
+ from dotenv import load_dotenv, find_dotenv
9
+ _ = load_dotenv(find_dotenv()) # read local .env file
10
+ hf_api_key = os.environ['HF_API_KEY']
11
+
12
+
13
+ #### Helper function
14
+ import requests, json
15
+
16
+ #Here we are going to call multiple endpoints!
17
+ def get_completion(inputs, parameters=None, ENDPOINT_URL=""):
18
+ headers = {
19
+ "Authorization": f"Bearer {hf_api_key}",
20
+ "Content-Type": "application/json"
21
+ }
22
+ data = { "inputs": inputs }
23
+ if parameters is not None:
24
+ data.update({"parameters": parameters})
25
+ response = requests.request("POST",
26
+ ENDPOINT_URL,
27
+ headers=headers,
28
+ data=json.dumps(data))
29
+ return json.loads(response.content.decode("utf-8"))
30
+
31
+
32
+ #Here we are going to call multiple endpoints!
33
+ def image_completion(inputs, parameters=None, ENDPOINT_URL=""):
34
+ headers = {
35
+ "Authorization": f"Bearer {hf_api_key}",
36
+ "Content-Type": "application/json"
37
+ }
38
+ data = { "inputs": inputs }
39
+ if parameters is not None:
40
+ data.update({"parameters": parameters})
41
+ response = requests.request("POST",
42
+ ENDPOINT_URL,
43
+ headers=headers,
44
+ data=json.dumps(data))
45
+ return response.content
46
+
47
+
48
+ #text-to-image
49
+ TTI_ENDPOINT ="https://api-inference.huggingface.co/models/cloudqi/cqi_text_to_image_pt_v0"
50
+ #image-to-text
51
+ ITT_ENDPOINT = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-base"
52
+
53
+
54
+ #Bringing the functions from lessons 3 and 4!
55
+ def image_to_base64_str(pil_image):
56
+ byte_arr = io.BytesIO()
57
+ pil_image.save(byte_arr, format='PNG')
58
+ byte_arr = byte_arr.getvalue()
59
+ return str(base64.b64encode(byte_arr).decode('utf-8'))
60
+
61
+ def base64_to_pil(img_base64):
62
+ base64_decoded = base64.b64decode(img_base64)
63
+ byte_stream = io.BytesIO(base64_decoded)
64
+ pil_image = Image.open(byte_stream)
65
+ return pil_image
66
+
67
+ def captioner(image):
68
+ base64_image = image_to_base64_str(image)
69
+ result = get_completion(base64_image, None, ITT_ENDPOINT)
70
+ return result[0]['generated_text']
71
+
72
+ def generate(prompt):
73
+ output = image_completion(prompt, None, TTI_ENDPOINT)
74
+ result_image = Image.open(io.BytesIO(output))
75
+ print(result_image)
76
+ return result_image
77
+
78
+
79
+ def caption_and_generate(image):
80
+ caption = captioner(image)
81
+ image = generate(caption)
82
+ return [caption, image]
83
+
84
+ def loadGUI():
85
+ with gr.Blocks() as demo:
86
+ gr.Markdown("# Describe-and-Generate game 🖍️")
87
+ image_upload = gr.Image(label="Your first image",type="pil")
88
+ btn_all = gr.Button("Caption and generate")
89
+ caption = gr.Textbox(label="Generated caption")
90
+ image_output = gr.Image(label="Generated Image")
91
+
92
+ btn_all.click(fn=caption_and_generate, inputs=[image_upload], outputs=[caption, image_output])
93
+
94
+ gr.close_all()
95
+ demo.launch(share=True)
96
+
97
+
98
+ def main():
99
+ loadGUI()
100
+
101
+
102
+ if __name__ == "__main__":
103
+ main()
104
+
images/helicopter.jpg ADDED
images/maxresdefault.jpg ADDED
images/police-heli.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==0.25.0
2
+ aiofiles==23.2.1
3
+ altair==5.2.0
4
+ annotated-types==0.6.0
5
+ anyio==3.7.1
6
+ attrs==23.1.0
7
+ certifi==2023.11.17
8
+ charset-normalizer==3.3.2
9
+ click==8.1.7
10
+ colorama==0.4.6
11
+ contourpy==1.2.0
12
+ cycler==0.12.1
13
+ fastapi==0.104.1
14
+ ffmpy==0.3.1
15
+ filelock==3.13.1
16
+ fonttools==4.46.0
17
+ fsspec==2023.12.1
18
+ gradio==4.8.0
19
+ gradio_client==0.7.1
20
+ h11==0.14.0
21
+ httpcore==1.0.2
22
+ httpx==0.25.2
23
+ huggingface-hub==0.19.4
24
+ idna==3.6
25
+ importlib-resources==6.1.1
26
+ Jinja2==3.1.2
27
+ jsonschema==4.20.0
28
+ jsonschema-specifications==2023.11.2
29
+ kiwisolver==1.4.5
30
+ markdown-it-py==3.0.0
31
+ MarkupSafe==2.1.3
32
+ matplotlib==3.8.2
33
+ mdurl==0.1.2
34
+ mpmath==1.3.0
35
+ networkx==3.2.1
36
+ numpy==1.26.2
37
+ orjson==3.9.10
38
+ packaging==23.2
39
+ pandas==2.1.4
40
+ Pillow==10.1.0
41
+ psutil==5.9.6
42
+ pydantic==2.5.2
43
+ pydantic_core==2.14.5
44
+ pydub==0.25.1
45
+ Pygments==2.17.2
46
+ pyparsing==3.1.1
47
+ python-dateutil==2.8.2
48
+ python-dotenv==1.0.0
49
+ python-multipart==0.0.6
50
+ pytz==2023.3.post1
51
+ PyYAML==6.0.1
52
+ referencing==0.32.0
53
+ regex==2023.10.3
54
+ requests==2.31.0
55
+ rich==13.7.0
56
+ rpds-py==0.13.2
57
+ safetensors==0.4.1
58
+ semantic-version==2.10.0
59
+ shellingham==1.5.4
60
+ six==1.16.0
61
+ sniffio==1.3.0
62
+ starlette==0.27.0
63
+ sympy==1.12
64
+ tokenizers==0.15.0
65
+ tomlkit==0.12.0
66
+ toolz==0.12.0
67
+ torch==2.1.1
68
+ tqdm==4.66.1
69
+ transformers==4.35.2
70
+ typer==0.9.0
71
+ typing_extensions==4.8.0
72
+ tzdata==2023.3
73
+ urllib3==2.1.0
74
+ uvicorn==0.24.0.post1
75
+ websockets==11.0.3
76
+ text-generation==0.6.1