not-lain commited on
Commit
94ec186
1 Parent(s): 722ecec

changed image api method

Browse files
Files changed (1) hide show
  1. app.py +22 -30
app.py CHANGED
@@ -87,46 +87,38 @@ def convert_image_to_required_format(image):
87
  def process_image_with_openai(image):
88
  base64_image = convert_image_to_required_format(image)
89
  openai_api_key = os.getenv('OPENAI_API_KEY')
90
- oai_org = os.getenv('OAI_ORG')
91
- if openai_api_key is None:
92
- raise Exception("OPENAI_API_KEY not found in environment variables")
 
 
 
93
 
94
- data_payload = {
95
  "model": "gpt-4-vision-preview",
96
  "messages": [
 
 
 
97
  {
98
- "role": "user",
99
- "content": [
100
- {
101
- "type": "text",
102
- "text": "What's in this image?"
103
- },
104
- {
105
- "type": "image_url",
106
- "image_url" : {
107
- "url": f"data:image/jpeg;base64,{base64_image}"
108
- }
109
- }
110
- ]
111
  }
 
 
112
  ],
113
  "max_tokens": 300
114
  }
115
 
116
- response = requests.post(
117
- "https://api.openai.com/v1/chat/completions",
118
- headers={
119
- "Content-Type": "application/json",
120
- "Authorization": f"Bearer {openai_api_key}",
121
- "OpenAI-Organization": f"{oai_org}"
122
- },
123
- json=data_payload
124
- )
125
 
126
- if response.status_code == 200:
127
- return response.json()['choices'][0]['message']['content']
128
- else:
129
- raise Exception(f"OpenAI Error: {response.status_code}")
130
 
131
 
132
  def query_vectara(text):
 
87
  def process_image_with_openai(image):
88
  base64_image = convert_image_to_required_format(image)
89
  openai_api_key = os.getenv('OPENAI_API_KEY')
90
+ # oai_org = os.getenv('OAI_ORG')
91
+
92
+ headers = {
93
+ "Content-Type": "application/json",
94
+ "Authorization": f"Bearer {openai_api_key}"
95
+ }
96
 
97
+ payload = {
98
  "model": "gpt-4-vision-preview",
99
  "messages": [
100
+ {
101
+ "role": "user",
102
+ "content": [
103
  {
104
+ "type": "text",
105
+ "text": "What's in this image?"
106
+ },
107
+ {
108
+ "type": "image_url",
109
+ "image_url": {
110
+ "url": f"data:image/jpeg;base64,{base64_image}"
111
+ }
 
 
 
 
 
112
  }
113
+ ]
114
+ }
115
  ],
116
  "max_tokens": 300
117
  }
118
 
119
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
 
 
 
 
 
 
 
 
120
 
121
+ return response.json()
 
 
 
122
 
123
 
124
  def query_vectara(text):