Vihang28 commited on
Commit
35fc170
·
1 Parent(s): c29d7aa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def encode_image(image_path):
2
+ with open(image_path, "rb") as image_file:
3
+ return base64.b64encode(image_file.read()).decode('utf-8')
4
+
5
+ def upload_img(image,api_key,img_ques):
6
+
7
+ base64_image = encode_image(image)
8
+
9
+
10
+ headers = {
11
+ "Content-Type": "application/json",
12
+ "Authorization": f"Bearer {api_key}"
13
+ }
14
+ payload = {
15
+ "model": "gpt-4-vision-preview",
16
+ "messages": [
17
+ {
18
+ "role": "user",
19
+ "content": [
20
+ {
21
+ "type": "text",
22
+ "text": img_ques
23
+ },
24
+ {
25
+ "type": "image_url",
26
+ "image_url": {
27
+ "url": f"data:image/jpeg;base64,{base64_image}"
28
+ }
29
+ }
30
+ ]
31
+ }
32
+ ],
33
+ "max_tokens": 300
34
+ }
35
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
36
+ img_preview = response.json()
37
+ return img_preview["choices"][0]["message"]["content"]
38
+
39
+ title = """<h1 align="center">GPT-4 Preview</h1>"""
40
+
41
+ with gr.Blocks() as demo:
42
+ gr.Markdown(title)
43
+
44
+ with gr.Row():
45
+ with gr.Column():
46
+ image_input = gr.Image(type="filepath", label="Upload Image")
47
+ key_input = gr.Textbox(label="Enter API-key")
48
+ upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
49
+ btn_clear = gr.ClearButton([image_input,key_input],[text])
50
+
51
+ with gr.Column():
52
+ text = gr.Textbox(label="Output")
53
+ ques_input = gr.Textbox(label="Enter the question")
54
+ ques_clear = gr.ClearButton([ques_input])
55
+
56
+ upload_button.click(upload_img, inputs=[image_input,key_input,ques_input], outputs=text)
57
+ btn_clear.add(text)
58
+ ques_clear.add(text)
59
+
60
+ demo.launch(share=True)