Vihang28 commited on
Commit
bcf23f8
·
verified ·
1 Parent(s): 3dd5f7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -8
app.py CHANGED
@@ -31,6 +31,55 @@ def concate_image(img_input):
31
  im_v = cv2.vconcat([img1, img2])
32
  cv2.imwrite('merge_image.jpg', im_v)
33
  return "merge_image.jpg"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  title = "Compare the Graphs"
36
 
@@ -54,15 +103,14 @@ with block:
54
  with gr.Row():
55
  with gr.Column():
56
  img_input = gr.Text(label="Provide the Image names separated by a comma(,)")
 
57
  show_concat = gr.Button(value="Show Images")
58
  with gr.Column():
59
- img_concat = gr.Image(type="filepath")
60
- show_concat.click(concate_image, inputs=img_input, outputs=img_concat)
 
 
 
 
61
 
62
- # api_input = gr.Textbox(label= "Enter Api-key")
63
- # upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
64
- # with gr.Column():
65
- # chatbot = gr.Chatbot(label="Chat with Image")
66
- # message = gr.Textbox(label="User", placeholder=prompt)
67
- # state = gr.State()
68
  block.launch()
 
31
  im_v = cv2.vconcat([img1, img2])
32
  cv2.imwrite('merge_image.jpg', im_v)
33
  return "merge_image.jpg"
34
+
35
+ def api_calling(image, prompt, api_key):
36
+ base64_image = concate_image(image)
37
+ if len(prompt) == 0:
38
+ prompt = "Provide a brief description of the image."
39
+
40
+
41
+ headers = {
42
+ "Content-Type": "application/json",
43
+ "Authorization": f"Bearer {api_key}"
44
+ }
45
+ payload = {
46
+ "model": "gpt-4-vision-preview",
47
+ "messages": [
48
+ {
49
+ "role": "user",
50
+ "content": [
51
+ {
52
+ "type": "text",
53
+ "text": prompt
54
+ },
55
+ {
56
+ "type": "image_url",
57
+ "image_url": {
58
+ "url": f"data:image/jpeg;base64,{base64_image}"
59
+ }
60
+ }
61
+ ]
62
+ }
63
+ ],
64
+ "max_tokens": 500
65
+ }
66
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
67
+ img_preview = response.json()
68
+ return img_preview["choices"][0]["message"]["content"]
69
+
70
+
71
+ def message_and_history(img_input_name, input, history, api_key):
72
+ history = history or []
73
+ s = list(sum(history, ()))
74
+ s.append(input)
75
+ inp = ' '.join(s)
76
+ output = api_calling(img,inp, api_key)
77
+ if len(input) == 0:
78
+ input = "Brief description of the image."
79
+ history.append((input, output))
80
+ else:
81
+ history.append((input, output))
82
+ return history, history
83
 
84
  title = "Compare the Graphs"
85
 
 
103
  with gr.Row():
104
  with gr.Column():
105
  img_input = gr.Text(label="Provide the Image names separated by a comma(,)")
106
+ api_input = gr.Textbox(label= "Enter Api-key")
107
  show_concat = gr.Button(value="Show Images")
108
  with gr.Column():
109
+ chatbot = gr.Chatbot(label="Chat with Image")
110
+ message = gr.Textbox(label="User", placeholder=prompt)
111
+ state = gr.State()
112
+ upload_button.click(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
113
+ message.submit(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
114
+ message.submit(lambda: None, None, message, queue=False)
115
 
 
 
 
 
 
 
116
  block.launch()