Vihang28 commited on
Commit
a98b175
·
verified ·
1 Parent(s): 11fd341

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import base64
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 = "I want you to act as my friend. I will share images with you and ask about my queries related to the images and you will reply to me with the explanation of the image with accuracy, compassion and in conversational manor. I want you to keep the reply neat, limiting the reply to 200 words in English."
18
+ prompt = "I want you to act as a friendly tile setters who has 10 years of experience with floor tiling. I will share an image of floor with you and you will reply to me with the number of tiles that need cleaning. Reply with accuracy and compassion. I want you to keep the reply neat, limiting the reply to 50 words in English."
19
+
20
+ headers = {
21
+ "Content-Type": "application/json",
22
+ "Authorization": f"Bearer {api_key}"
23
+ }
24
+ payload = {
25
+ "model": "gpt-4-vision-preview",
26
+ "messages": [
27
+ {
28
+ "role": "user",
29
+ "content": [
30
+ {
31
+ "type": "text",
32
+ "text": prompt
33
+ },
34
+ {
35
+ "type": "image_url",
36
+ "image_url": {
37
+ "url": f"data:image/jpeg;base64,{base64_image}",
38
+ "detail": "high"
39
+ }
40
+ }
41
+ ]
42
+ }
43
+ ],
44
+ "max_tokens": 500,
45
+ "temperature": 0.5,
46
+ "n": 1
47
+ }
48
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
49
+ img_preview = response.json()
50
+ return img_preview["choices"][0]["message"]["content"]
51
+
52
+ def message_and_history(img, input, history, api_key):
53
+ history = history or []
54
+ s = list(sum(history, ()))
55
+ s.append(input)
56
+ inp = ' '.join(s)
57
+ output = api_calling(img,inp, api_key)
58
+ if len(input) == 0:
59
+ input = "Brief description of the image."
60
+ history.append((input, output))
61
+ else:
62
+ history.append((input, output))
63
+ return history, history
64
+
65
+ block = gr.Blocks(theme=gr.themes.Glass(primary_hue="slate"))
66
+ with block:
67
+ gr.Markdown("""<h1><center>Visual Explorer - Ask and Learn about Images</center></h1> """)
68
+ with gr.Row():
69
+ with gr.Column(scale=0.5):
70
+ img_input = gr.Image(type="filepath", label="Upload Image")
71
+ api_input = gr.Textbox(label= "Enter Api-key")
72
+ upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
73
+ with gr.Column():
74
+ chatbot = gr.Chatbot(label="Chat with Image")
75
+ message = gr.Textbox(label="User", placeholder=prompt)
76
+ state = gr.State()
77
+
78
+ upload_button.click(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
79
+ message.submit(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
80
+ message.submit(lambda: None, None, message, queue=False)
81
+ block.launch()