Vihang28 commited on
Commit
7e4d0f8
·
1 Parent(s): d77612c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -22
app.py CHANGED
@@ -3,19 +3,23 @@ import base64
3
  import requests
4
  import tempfile
5
  import os
 
 
 
6
 
7
  def encode_image(image_path):
8
  with open(image_path, "rb") as image_file:
9
  return base64.b64encode(image_file.read()).decode('utf-8')
10
 
11
- def upload_img(image,api_key,img_ques):
12
-
13
  base64_image = encode_image(image)
 
 
14
 
15
 
16
  headers = {
17
  "Content-Type": "application/json",
18
- "Authorization": f"Bearer {api_key}"
19
  }
20
  payload = {
21
  "model": "gpt-4-vision-preview",
@@ -25,7 +29,7 @@ def upload_img(image,api_key,img_ques):
25
  "content": [
26
  {
27
  "type": "text",
28
- "text": img_ques
29
  },
30
  {
31
  "type": "image_url",
@@ -36,31 +40,40 @@ def upload_img(image,api_key,img_ques):
36
  ]
37
  }
38
  ],
39
- "max_tokens": 300
40
  }
41
  response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
42
  img_preview = response.json()
43
  return img_preview["choices"][0]["message"]["content"]
44
 
45
- title = """<h1 align="center">GPT-4 Image Preview</h1>"""
46
-
47
- with gr.Blocks() as demo:
48
- gr.Markdown(title)
 
 
 
 
 
 
 
 
49
 
 
 
 
50
  with gr.Row():
51
  with gr.Column(scale=0.5):
52
- image_input = gr.Image(type="filepath", label="Upload Image")
53
- key_input = gr.Textbox(label="Enter API-key")
54
  upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
55
- btn_clear = gr.ClearButton([image_input,key_input])
56
-
57
  with gr.Column():
58
- text = gr.Textbox(label="Output")
59
- ques_input = gr.Textbox(label="Enter the question")
60
- ques_clear = gr.ClearButton([ques_input])
61
-
62
- upload_button.click(upload_img, inputs=[image_input,key_input,ques_input], outputs=text)
63
- btn_clear.add(text)
64
- ques_clear.add(text)
65
-
66
- demo.launch(share=True)
 
3
  import requests
4
  import tempfile
5
  import os
6
+ import openai
7
+
8
+ prompt = "Type and press Enter"
9
 
10
  def encode_image(image_path):
11
  with open(image_path, "rb") as image_file:
12
  return base64.b64encode(image_file.read()).decode('utf-8')
13
 
14
+ def api_calling(image, prompt, api_key):
 
15
  base64_image = encode_image(image)
16
+ if len(prompt) == 0:
17
+ prompt = "Provide a brief description of the image."
18
 
19
 
20
  headers = {
21
  "Content-Type": "application/json",
22
+ "Authorization": f"Bearer {api_key}"
23
  }
24
  payload = {
25
  "model": "gpt-4-vision-preview",
 
29
  "content": [
30
  {
31
  "type": "text",
32
+ "text": prompt
33
  },
34
  {
35
  "type": "image_url",
 
40
  ]
41
  }
42
  ],
43
+ "max_tokens": 500
44
  }
45
  response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
46
  img_preview = response.json()
47
  return img_preview["choices"][0]["message"]["content"]
48
 
49
+ def message_and_history(img, input, history, api_key):
50
+ history = history or []
51
+ s = list(sum(history, ()))
52
+ s.append(input)
53
+ inp = ' '.join(s)
54
+ output = api_calling(img,inp, api_key)
55
+ if len(input) == 0:
56
+ input = "Brief description of the image."
57
+ history.append((input, output))
58
+ else:
59
+ history.append((input, output))
60
+ return history, history
61
 
62
+ block = gr.Blocks(theme=gr.themes.Monochrome())
63
+ with block:
64
+ gr.Markdown("""<h1><center>Visual Explorer - Ask and Learn about Images</center></h1> """)
65
  with gr.Row():
66
  with gr.Column(scale=0.5):
67
+ img_input = gr.Image(type="filepath", label="Upload Image")
68
+ api_input = gr.Textbox(label= "Enter Api-key")
69
  upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
 
 
70
  with gr.Column():
71
+ chatbot = gr.Chatbot(label="Chat with Image")
72
+ message = gr.Textbox(label="User", placeholder=prompt)
73
+ btn_clear = gr.ClearButton([message])
74
+ state = gr.State()
75
+
76
+ upload_button.click(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
77
+ message.submit(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
78
+ btn_clear.add(message)
79
+ block.launch(share = True)