markllego commited on
Commit
058a98e
1 Parent(s): e76cafc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import the necessary libraries
2
+ import gradio as gr
3
+ import openai
4
+ import base64
5
+ from PIL import Image
6
+ import io
7
+ import requests
8
+ import os
9
+
10
+ # Consider using environment variables or a configuration file for API keys.
11
+ # WARNING: Do not hardcode API keys in your code, especially if sharing or using version control.
12
+ openai.api_key = os.getenv('OPENAI_API_KEY')
13
+ if openai.api_key is None:
14
+ raise ValueError("Please set the OPENAI_API_KEY environment variable.")
15
+
16
+ # Function to encode the image to base64
17
+ def encode_image_to_base64(image):
18
+ buffered = io.BytesIO()
19
+ image.save(buffered, format="JPEG")
20
+ img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
21
+ return img_str
22
+
23
+ # Function to send the image to the OpenAI API and get a response
24
+ def ask_openai_with_image(image):
25
+ # Encode the uploaded image to base64
26
+ base64_image = encode_image_to_base64(image)
27
+
28
+ # Create the payload with the base64 encoded image
29
+ payload = {
30
+ "model": "gpt-4-vision-preview",
31
+ "messages": [
32
+ {
33
+ "role": "user",
34
+ "content": [
35
+ {
36
+ "type": "text",
37
+ "text": "I've uploaded an image and I'd like to know what it depicts and any interesting details you can provide."
38
+ },
39
+ {
40
+ "type": "image_url",
41
+ "image_url": f"data:image/jpeg;base64,{base64_image}"
42
+ }
43
+ ]
44
+ }
45
+ ],
46
+ "max_tokens": 4095
47
+ }
48
+
49
+ # Send the request to the OpenAI API
50
+ response = requests.post(
51
+ "https://api.openai.com/v1/chat/completions",
52
+ headers={"Authorization": f"Bearer {openai.api_key}"},
53
+ json=payload
54
+ )
55
+
56
+ # Check if the request was successful
57
+ if response.status_code == 200:
58
+ response_json = response.json()
59
+ print("Response JSON:", response_json) # Print the raw response JSON
60
+ try:
61
+ # Attempt to extract the content text
62
+ return response_json["choices"][0]["message"]["content"]
63
+ except Exception as e:
64
+ # If there is an error in the JSON structure, print it
65
+ print("Error in JSON structure:", e)
66
+ print("Full JSON response:", response_json)
67
+ return "Error processing the image response."
68
+ else:
69
+ # If an error occurred, return the error message
70
+ return f"Error: {response.text}"
71
+
72
+ # Create a Gradio interface
73
+ iface = gr.Interface(
74
+ fn=ask_openai_with_image,
75
+ inputs=gr.Image(type="pil"),
76
+ outputs="text",
77
+ title="GPT-4 with Vision",
78
+ description="Upload an image and get a description from GPT-4 with Vision."
79
+ )
80
+
81
+ # Launch the app
82
+ iface.launch()