designfailure commited on
Commit
86bcfb5
·
1 Parent(s): 272cd06

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ !pip install openai
3
+ !pip install -q gradio
4
+
5
+ import os
6
+ import openai
7
+ from getpass import getpass
8
+ openai.api_key = getpass("sk- ")
9
+
10
+
11
+ import base64
12
+
13
+ # Function to encode the image
14
+ def encode_image(image_path):
15
+ with open(image_path, "rb") as image_file:
16
+ return base64.b64encode(image_file.read()).decode('utf-8')
17
+
18
+ """# Image Captioning AI App with Gradio"""
19
+
20
+ def caption_image(image_path):
21
+ encoded_image = encode_image(image_path)
22
+
23
+ result = openai.chat.completions.create(
24
+ model = "gpt-4-vision-preview",
25
+ messages=[
26
+ {
27
+ "role": "user",
28
+ "content": [
29
+ {"type": "text", "text": "Describe the image. Additionally outline from image the caption objects and items with the attribute recognition. If there are OCR captions, also extract this data from the image.Outline the image description like a plain paragraph text with few sentences.Additional outline in bullets points the KEY ELEMENTS if there is present the:"},
30
+ {"type": "text", "text": "-[item] caption\n-[object] detection\n-[attribute] recognition\n-[image] segmentation\n-[OCR] text and number extraction\n-[adjective] semantic search\n-[subject] from the image\n-[doing action]\n-[mobility]\n-[transportation]\n-[person]\n-[animal]\n-[furniture]\n-[electronics]\n-[house appliances]\n-[landscape]\n-[environment]\n-[liability] context search\n Outline only the captioned KEY ELEMENTS categories as bullet points."},
31
+
32
+ {"type": "image_url",
33
+ "image_url": f"data:image/jpeg;base64,{encoded_image}"},
34
+ ]
35
+ },
36
+ ],
37
+ max_tokens=600,
38
+ )
39
+
40
+ return result.choices[0].message.content
41
+
42
+ import gradio as gr
43
+ import os
44
+
45
+
46
+
47
+ demo = gr.Interface(
48
+ caption_image,
49
+ gr.Image(type="filepath"),
50
+ "text",
51
+ live=True,
52
+ css = """.gradio-container {background-color: cyan;}.btn-primary {background-color: cyan;color: white;border: 2px solid white;}"""
53
+ )
54
+
55
+ if __name__ == "__main__":
56
+ demo.launch()
57
+
58
+ !git clone https://huggingface.co/spaces/designfailure/Insur.Co
59
+
60
+ !git add app.py
61
+ !git commit -m "Add application file"
62
+ !git push